push e1d8a1293d44015bb0894687d02c5c53339996f7
[wine/hacks.git] / dlls / user32 / menu.c
blobab473eab5b8ceab50be3ef1ae361cb803c02cb6c
1 /*
2 * Menu functions
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.
35 * TODO:
36 * implements styles :
37 * - MNS_AUTODISMISS
38 * - MNS_DRAGDROP
39 * - MNS_MODELESS
42 #include "config.h"
43 #include "wine/port.h"
45 #include <stdarg.h>
46 #include <string.h>
48 #define OEMRESOURCE
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winnls.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wownt32.h"
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/exception.h"
60 #include "win.h"
61 #include "controls.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 */
74 typedef struct {
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
90 * bitmap */
91 } MENUITEM;
93 /* Popup menu structure */
94 typedef struct {
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
124 typedef struct
126 UINT trackFlags;
127 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
128 HMENU hTopMenu; /* initial menu */
129 HWND hOwnerWnd; /* where notifications are sent */
130 POINT pt;
131 } MTRACKER;
133 #define MENU_MAGIC 0x554d /* 'MU' */
135 #define ITEM_PREV -1
136 #define ITEM_NEXT 1
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) \
216 do { \
217 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
218 } while (0)
220 static void do_debug_print_menuitem(const char *prefix, const MENUITEM *mp,
221 const char *postfix)
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);
228 if (mp) {
229 UINT flags = mp->fType;
230 TRACE( "{ ID=0x%lx", mp->wID);
231 if ( mp->hSubMenu)
232 TRACE( ", Sub=%p", mp->hSubMenu);
233 if (flags) {
234 int count = 0;
235 TRACE( ", fType=");
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 */
246 if (flags)
247 TRACE( "+0x%x", flags);
249 flags = mp->fState;
250 if (flags) {
251 int count = 0;
252 TRACE( ", State=");
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");
260 if (flags)
261 TRACE( "+0x%x", flags);
263 if (mp->hCheckBit)
264 TRACE( ", Chk=%p", mp->hCheckBit);
265 if (mp->hUnCheckBit)
266 TRACE( ", Unc=%p", mp->hUnCheckBit);
267 if (mp->text)
268 TRACE( ", Text=%s", debugstr_w(mp->text));
269 if (mp->dwItemData)
270 TRACE( ", ItemData=0x%08lx", mp->dwItemData);
271 if (mp->hbmpItem)
273 if( IS_MAGIC_BITMAP(mp->hbmpItem))
274 TRACE( ", hbitmap=%s", hbmmenus[ (INT_PTR)mp->hbmpItem + 1]);
275 else
276 TRACE( ", hbitmap=%p", mp->hbmpItem);
278 TRACE( " }");
279 } else
280 TRACE( "NULL");
281 TRACE(" %s\n", postfix);
284 #undef MENUOUT
285 #undef MENUFLAG
288 /***********************************************************************
289 * MENU_GetMenu
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);
299 menu = NULL;
301 return menu;
304 /***********************************************************************
305 * get_win_sys_menu
307 * Get the system menu of a window
309 static HMENU get_win_sys_menu( HWND hwnd )
311 HMENU ret = 0;
312 WND *win = WIN_GetPtr( hwnd );
313 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
315 ret = win->hSysMenu;
316 WIN_ReleasePtr( win );
318 return ret;
321 /***********************************************************************
322 * get_menu_font
324 static HFONT get_menu_font( BOOL bold )
326 static HFONT hMenuFont, hMenuFontBold;
328 HFONT ret = bold ? hMenuFontBold : hMenuFont;
330 if (!ret)
332 NONCLIENTMETRICSW ncm;
333 HFONT prev;
335 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
336 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0);
338 if (bold)
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),
345 ret, NULL );
346 if (prev)
348 /* another thread beat us to it */
349 DeleteObject( ret );
350 ret = prev;
353 return ret;
356 /***********************************************************************
357 * get_arrow_bitmap
359 static HBITMAP get_arrow_bitmap(void)
361 static HBITMAP arrow_bitmap;
363 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_MNARROW));
364 return arrow_bitmap;
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));
375 return arrow_bitmap;
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));
386 return arrow_bitmap;
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));
397 return arrow_bitmap;
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));
408 return arrow_bitmap;
411 /***********************************************************************
412 * MENU_CopySysPopup
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);
421 if( hMenu ) {
422 MENUINFO minfo;
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);
443 else
444 ERR("Unable to load default system menu\n" );
446 TRACE("returning %p.\n", hMenu );
448 return hMenu;
452 /**********************************************************************
453 * MENU_GetSysMenu
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 )
463 HMENU hMenu;
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);
473 if (!hPopupMenu)
474 hPopupMenu = MENU_CopySysPopup();
476 if (hPopupMenu)
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 );
489 return hMenu;
491 DestroyMenu( hMenu );
493 ERR("failed to load system menu!\n");
494 return 0;
498 /***********************************************************************
499 * MENU_InitSysMenuPopup
501 * Grey the appropriate items in System menu.
503 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
505 BOOL gray;
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 */
520 if(gray)
521 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
525 /******************************************************************************
527 * UINT MENU_GetStartOfNextColumn(
528 * HMENU hMenu )
530 *****************************************************************************/
532 static UINT MENU_GetStartOfNextColumn(
533 HMENU hMenu )
535 POPUPMENU *menu = MENU_GetMenu(hMenu);
536 UINT i;
538 if(!menu)
539 return NO_SELECTED_ITEM;
541 i = menu->FocusedItem + 1;
542 if( i == NO_SELECTED_ITEM )
543 return i;
545 for( ; i < menu->nItems; ++i ) {
546 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
547 return i;
550 return NO_SELECTED_ITEM;
554 /******************************************************************************
556 * UINT MENU_GetStartOfPrevColumn(
557 * HMENU hMenu )
559 *****************************************************************************/
561 static UINT MENU_GetStartOfPrevColumn(
562 HMENU hMenu )
564 POPUPMENU *menu = MENU_GetMenu(hMenu);
565 UINT i;
567 if( !menu )
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));
577 --i); /* empty */
579 if(i == 0)
580 return NO_SELECTED_ITEM;
582 for(--i; i != 0; --i) {
583 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
584 break;
587 TRACE("ret %d.\n", i );
589 return i;
594 /***********************************************************************
595 * MENU_FindItem
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 )
602 POPUPMENU *menu;
603 MENUITEM *fallback = NULL;
604 UINT fallback_pos = 0;
605 UINT i;
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];
613 else
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 );
622 if (subitem)
624 *hmenu = hsubmenu;
625 return subitem;
627 else if (item->wID == *nPos)
629 /* fallback to this item if nothing else found */
630 fallback_pos = i;
631 fallback = item;
634 else if (item->wID == *nPos)
636 *nPos = i;
637 return item;
642 if (fallback)
643 *nPos = fallback_pos;
645 return fallback;
648 /***********************************************************************
649 * MENU_FindSubMenu
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 )
657 POPUPMENU *menu;
658 UINT i;
659 MENUITEM *item;
660 if (((*hmenu)==(HMENU)0xffff) ||
661 (!(menu = MENU_GetMenu(*hmenu))))
662 return NO_SELECTED_ITEM;
663 item = menu->items;
664 for (i = 0; i < menu->nItems; i++, item++) {
665 if(!(item->fType & MF_POPUP)) continue;
666 if (item->hSubMenu == hSubTarget) {
667 return i;
669 else {
670 HMENU hsubmenu = item->hSubMenu;
671 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
672 if (pos != NO_SELECTED_ITEM) {
673 *hmenu = hsubmenu;
674 return pos;
678 return NO_SELECTED_ITEM;
681 /***********************************************************************
682 * MENU_FreeItemData
684 static void MENU_FreeItemData( MENUITEM* item )
686 /* delete text */
687 HeapFree( GetProcessHeap(), 0, item->text );
690 /***********************************************************************
691 * MENU_AdjustMenuItemRect
693 * Adjust menu item rectangle according to scrolling state.
695 static void
696 MENU_AdjustMenuItemRect(const POPUPMENU *menu, LPRECT rect)
698 if (menu->bScrolling)
700 UINT arrow_bitmap_height;
701 BITMAP bmp;
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 )
721 MENUITEM *item;
722 UINT i;
723 RECT rect;
725 if (!GetWindowRect(menu->hWnd, &rect)) return NULL;
726 pt.x -= rect.left;
727 pt.y -= rect.top;
728 item = menu->items;
729 for (i = 0; i < menu->nItems; i++, item++)
731 rect = item->rect;
732 MENU_AdjustMenuItemRect(menu, &rect);
733 if (PtInRect(&rect, pt))
735 if (pos) *pos = i;
736 return item;
739 return NULL;
743 /***********************************************************************
744 * MENU_FindItemByKey
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);
756 if (hmenu)
758 POPUPMENU *menu = MENU_GetMenu( hmenu );
759 MENUITEM *item = menu->items;
760 LRESULT menuchar;
762 if( !forceMenuChar )
764 UINT i;
766 for (i = 0; i < menu->nItems; i++, item++)
768 if( item->text)
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);
785 return (UINT)(-1);
789 /***********************************************************************
790 * MENU_GetBitmapItemSize
792 * Get the size of a bitmap item.
794 static void MENU_GetBitmapItemSize( MENUITEM *lpitem, SIZE *size,
795 HWND hwndOwner)
797 BITMAP bm;
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;
809 measItem.CtlID = 0;
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;
817 return;
819 break;
820 case (INT_PTR)HBMMENU_SYSTEM:
821 if (lpitem->dwItemData)
823 bmp = (HBITMAP)lpitem->dwItemData;
824 break;
826 /* fall through */
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;
833 size->cy = size->cx;
834 return;
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);
841 return;
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)
858 BITMAP bm;
859 DWORD rop;
860 HDC hdcMem;
861 HBITMAP bmp;
862 int w = rect->right - rect->left;
863 int h = rect->bottom - rect->top;
864 int bmp_xoffset = 0;
865 int left, top;
866 HBITMAP hbmToDraw = lpitem->hbmpItem;
867 bmp = hbmToDraw;
869 /* Check if there is a magic menu item associated with this item */
870 if (IS_MAGIC_BITMAP(hbmToDraw))
872 UINT flags = 0;
873 WCHAR bmchr = 0;
874 RECT r;
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;
884 else
886 static HBITMAP hBmpSysMenu;
888 if (!hBmpSysMenu) hBmpSysMenu = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE));
889 bmp = hBmpSysMenu;
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;
895 goto got_bitmap;
896 case (INT_PTR)HBMMENU_MBAR_RESTORE:
897 flags = DFCS_CAPTIONRESTORE;
898 break;
899 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
900 flags = DFCS_CAPTIONMIN;
901 break;
902 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
903 flags = DFCS_CAPTIONMIN | DFCS_INACTIVE;
904 break;
905 case (INT_PTR)HBMMENU_MBAR_CLOSE:
906 flags = DFCS_CAPTIONCLOSE;
907 break;
908 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
909 flags = DFCS_CAPTIONCLOSE | DFCS_INACTIVE;
910 break;
911 case (INT_PTR)HBMMENU_CALLBACK:
913 DRAWITEMSTRUCT drawItem;
914 drawItem.CtlType = ODT_MENU;
915 drawItem.CtlID = 0;
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;
924 drawItem.hDC = hdc;
925 drawItem.itemData = lpitem->dwItemData;
926 drawItem.rcItem = *rect;
927 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&drawItem);
928 return;
930 break;
931 case (INT_PTR)HBMMENU_POPUP_CLOSE:
932 bmchr = 0x72;
933 break;
934 case (INT_PTR)HBMMENU_POPUP_RESTORE:
935 bmchr = 0x32;
936 break;
937 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
938 bmchr = 0x31;
939 break;
940 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
941 bmchr = 0x30;
942 break;
943 default:
944 FIXME("Magic %p not implemented\n", hbmToDraw);
945 return;
947 if (bmchr)
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);
963 else
965 r = *rect;
966 InflateRect( &r, -1, -1 );
967 if (lpitem->fState & MF_HILITE) flags |= DFCS_PUSHED;
968 DrawFrameControl( hdc, &r, DFC_CAPTION, flags );
970 return;
973 if (!bmp || !GetObjectW( bmp, sizeof(bm), &bm )) return;
975 got_bitmap:
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;
981 left=rect->left;
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 );
986 DeleteDC( hdcMem );
990 /***********************************************************************
991 * MENU_CalcItemSize
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 )
998 WCHAR *p;
999 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1000 UINT arrow_bitmap_width;
1001 BITMAP bm;
1002 INT itemheight;
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;
1025 mis.CtlID = 0;
1026 mis.itemID = lpitem->wID;
1027 mis.itemData = lpitem->dwItemData;
1028 mis.itemHeight = ODitemheight;
1029 mis.itemWidth = 0;
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;
1035 if (menuBar) {
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);
1039 } else
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);
1045 return;
1048 if (lpitem->fType & MF_SEPARATOR)
1050 lpitem->rect.bottom += GetSystemMetrics( SM_CYMENUSIZE)/2;
1051 if( !menuBar)
1052 lpitem->rect.right += arrow_bitmap_width + menucharsize.cx;
1053 return;
1056 itemheight = 0;
1057 lpitem->xTab = 0;
1059 if (!menuBar) {
1060 if (lpitem->hbmpItem) {
1061 SIZE size;
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 */
1078 SIZE size;
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) );
1096 if (menuBar) {
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;
1103 } else {
1104 if ((p = strchrW( lpitem->text, '\t' )) != NULL) {
1105 RECT tmprc = rc;
1106 LONG tmpheight;
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 */
1121 } else {
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
1143 static UINT
1144 MENU_GetMaxPopupHeight(const POPUPMENU *lppop)
1146 if (lppop->cyMax)
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 )
1159 MENUITEM *lpitem;
1160 HDC hdc;
1161 UINT start, i;
1162 int orgX, orgY, maxX, maxTab, maxTabWidth, maxHeight;
1164 lppop->Width = lppop->Height = 0;
1165 if (lppop->nItems == 0) return;
1166 hdc = GetDC( 0 );
1168 SelectObject( hdc, get_menu_font(FALSE));
1170 start = 0;
1171 maxX = 2 + 1;
1173 lppop->maxBmpSize.cx = 0;
1174 lppop->maxBmpSize.cy = 0;
1176 while (start < lppop->nItems)
1178 lpitem = &lppop->items[start];
1179 orgX = maxX;
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++)
1188 if ((i != start) &&
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;
1217 lppop->Width += 2;
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;
1227 else
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 )
1248 MENUITEM *lpitem;
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;
1256 lppop->Height = 0;
1257 maxY = lprect->top+1;
1258 start = 0;
1259 helpPos = ~0U;
1260 lppop->maxBmpSize.cx = 0;
1261 lppop->maxBmpSize.cy = 0;
1262 while (start < lppop->nItems)
1264 lpitem = &lppop->items[start];
1265 orgX = lprect->left;
1266 orgY = maxY;
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;
1272 if ((i != start) &&
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.
1316 static void
1317 MENU_DrawScrollArrows(const POPUPMENU *lppop, HDC hdc)
1319 HDC hdcMem = CreateCompatibleDC(hdc);
1320 HBITMAP hOrigBitmap;
1321 UINT arrow_bitmap_width, arrow_bitmap_height;
1322 BITMAP bmp;
1323 RECT rect;
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());
1332 else
1333 hOrigBitmap = SelectObject(hdcMem, get_up_arrow_inactive_bitmap());
1334 rect.left = 0;
1335 rect.top = 0;
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());
1346 else
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);
1352 DeleteDC(hdcMem);
1356 /***********************************************************************
1357 * draw_popup_arrow
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 );
1373 DeleteDC( hdcMem );
1375 /***********************************************************************
1376 * MENU_DrawMenuItem
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 )
1383 RECT rect;
1384 BOOL flat_menu = FALSE;
1385 int bkgnd;
1386 UINT arrow_bitmap_width = 0, arrow_bitmap_height = 0;
1387 POPUPMENU *menu = MENU_GetMenu(hmenu);
1388 RECT bmprc;
1390 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1392 if (!menuBar) {
1393 BITMAP bmp;
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) );
1403 return;
1406 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1407 bkgnd = (menuBar && flat_menu) ? COLOR_MENUBAR : COLOR_MENU;
1409 /* Setup colors */
1411 if (lpitem->fState & MF_HILITE)
1413 if(menuBar && !flat_menu) {
1414 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1415 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1416 } else {
1417 if(lpitem->fState & MF_GRAYED)
1418 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1419 else
1420 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1421 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1424 else
1426 if (lpitem->fState & MF_GRAYED)
1427 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1428 else
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.
1448 DRAWITEMSTRUCT dis;
1450 dis.CtlType = ODT_MENU;
1451 dis.CtlID = 0;
1452 dis.itemID = lpitem->wID;
1453 dis.itemData = lpitem->dwItemData;
1454 dis.itemState = 0;
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;
1460 dis.hDC = hdc;
1461 dis.rcItem = rect;
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);
1471 return;
1474 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1476 if (lpitem->fState & MF_HILITE)
1478 if (flat_menu)
1480 InflateRect (&rect, -1, -1);
1481 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENUHILIGHT));
1482 InflateRect (&rect, 1, 1);
1483 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1485 else
1487 if(menuBar)
1488 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1489 else
1490 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1493 else
1494 FillRect( hdc, &rect, GetSysColorBrush(bkgnd) );
1496 SetBkMode( hdc, TRANSPARENT );
1498 /* vertical separator */
1499 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1501 HPEN oldPen;
1502 RECT rc = rect;
1504 rc.left -= MENU_COL_SPACE / 2 + 1;
1505 rc.top = 3;
1506 rc.bottom = height - 3;
1507 if (flat_menu)
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 );
1514 else
1515 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1518 /* horizontal separator */
1519 if (lpitem->fType & MF_SEPARATOR)
1521 HPEN oldPen;
1522 RECT rc = rect;
1524 rc.left++;
1525 rc.right--;
1526 rc.top = ( rc.top + rc.bottom) / 2;
1527 if (flat_menu)
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 );
1534 else
1535 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1536 return;
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 */
1549 if( menuBar) {
1550 if( lpitem->hbmpItem == HBMMENU_CALLBACK)
1551 bmprc.left = 3;
1552 else
1553 bmprc.left = lpitem->text ? menucharsize.cx : 0;
1555 else if (menu->dwStyle & MNS_NOCHECK)
1556 bmprc.left = 4;
1557 else if (menu->dwStyle & MNS_CHECKORBMP)
1558 bmprc.left = 2;
1559 else
1560 bmprc.left = 4 + GetSystemMetrics(SM_CXMENUCHECK);
1561 bmprc.right = bmprc.left + lpitem->bmpsize.cx;
1562 if( menuBar && !(lpitem->hbmpItem == HBMMENU_CALLBACK))
1563 bmprc.top = 0;
1564 else
1565 bmprc.top = (rect.bottom - rect.top -
1566 lpitem->bmpsize.cy) / 2;
1567 bmprc.bottom = bmprc.top + lpitem->bmpsize.cy;
1570 if (!menuBar)
1572 HBITMAP bm;
1573 INT y = rect.top + rect.bottom;
1574 RECT rc = rect;
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
1580 * FIXME:
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 );
1594 DeleteDC( hdcMem );
1595 checked = TRUE;
1597 else if (lpitem->fState & MF_CHECKED) /* standard bitmaps */
1599 RECT r;
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 );
1611 DeleteDC( hdcMem );
1612 DeleteObject( bm );
1613 checked = TRUE;
1616 if( lpitem->hbmpItem &&
1617 !( checked && (menu->dwStyle & MNS_CHECKORBMP))) {
1618 POINT origorg;
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,
1622 odaction, FALSE);
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);
1629 rect.left += 4;
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 */
1636 POINT origorg;
1638 SetViewportOrgEx( hdc, rect.left, rect.top, &origorg);
1639 MENU_DrawBitmapItem( hdc, lpitem, &bmprc, hmenu, hwndOwner,
1640 odaction, menuBar);
1641 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1643 /* process text if present */
1644 if (lpitem->text)
1646 register int i;
1647 HFONT hfontOld = 0;
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) );
1661 if (menuBar) {
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'))
1671 break;
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;
1695 else
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 );
1715 if (hfontOld)
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;
1729 RECT rect;
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))))
1738 HPEN hPrevPen;
1740 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1742 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1743 if( hPrevPen )
1745 POPUPMENU *menu;
1746 BOOL flat_menu = FALSE;
1748 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1749 if (flat_menu)
1750 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
1751 else
1752 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1754 if( (menu = MENU_GetMenu( hmenu )))
1756 /* draw menu items */
1757 if( menu->nItems)
1759 MENUITEM *item;
1760 UINT u;
1762 item = 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);
1771 } else
1773 SelectObject( hdc, hPrevBrush );
1778 /***********************************************************************
1779 * MENU_DrawMenuBar
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,
1785 BOOL suppress_draw)
1787 LPPOPUPMENU lppop;
1788 HFONT hfontOld = 0;
1789 HMENU hMenu = GetMenu(hwnd);
1791 lppop = MENU_GetMenu( hMenu );
1792 if (lppop == NULL || lprect == NULL)
1794 return GetSystemMetrics(SM_CYMENU);
1797 if (suppress_draw)
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;
1809 else
1810 return DrawMenuBarTemp(hwnd, hDC, lprect, hMenu, NULL);
1814 /***********************************************************************
1815 * MENU_ShowPopup
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 )
1822 POPUPMENU *menu;
1823 INT width, height;
1824 POINT pt;
1825 HMONITOR monitor;
1826 MONITORINFO info;
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 */
1850 pt.x = x;
1851 pt.y = y;
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),
1886 (LPVOID)hmenu );
1887 if( !menu->hWnd ) return FALSE;
1888 if (!top_popup) {
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 );
1897 return TRUE;
1901 /***********************************************************************
1902 * MENU_EnsureMenuItemVisible
1904 static void
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;
1912 RECT rc;
1913 UINT arrow_bitmap_height;
1914 BITMAP bmp;
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 /***********************************************************************
1943 * MENU_SelectItem
1945 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1946 BOOL sendMenuSelect, HMENU topmenu )
1948 LPPOPUPMENU lppop;
1949 HDC hdc;
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);
1959 if (!top_popup) {
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),
1972 ODA_SELECT );
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 );
1986 if (sendMenuSelect)
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) {
1996 if(topmenu){
1997 int pos;
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 )
2020 INT i;
2021 POPUPMENU *menu;
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
2032 ; i += offset)
2033 if (!(menu->items[i].fType & MF_SEPARATOR))
2035 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
2036 return;
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 );
2045 return;
2050 /**********************************************************************
2051 * MENU_SetItemData
2053 * Set an item's flags, id and text ptr. Called by InsertMenu() and
2054 * ModifyMenu().
2056 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT_PTR id,
2057 LPCWSTR str )
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;
2065 if (!str)
2067 flags |= MF_SEPARATOR;
2068 item->text = NULL;
2070 else
2072 LPWSTR text;
2073 /* Item beginning with a backspace is a help item */
2074 if (*str == '\b')
2076 flags |= MF_HELP;
2077 str++;
2079 if (!(text = HeapAlloc( GetProcessHeap(), 0, (strlenW(str)+1) * sizeof(WCHAR) )))
2080 return FALSE;
2081 strcpyW( text, str );
2082 item->text = text;
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 );
2091 item->text = NULL;
2094 if (flags & MF_SEPARATOR) flags |= MF_GRAYED | MF_DISABLED;
2096 if (flags & MF_OWNERDRAW)
2097 item->dwItemData = (DWORD_PTR)str;
2098 else
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;
2108 else
2110 item->wID = 0;
2111 item->hSubMenu = 0;
2112 item->fType = 0;
2113 item->fState = 0;
2114 return FALSE;
2118 item->wID = id;
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, "");
2132 return TRUE;
2136 /**********************************************************************
2137 * MENU_InsertItem
2139 * Insert (allocate) a new item into a menu.
2141 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
2143 MENUITEM *newItems;
2144 POPUPMENU *menu;
2146 if (!(menu = MENU_GetMenu(hMenu)))
2147 return NULL;
2149 /* Find where to insert new item */
2151 if (flags & MF_BYPOSITION) {
2152 if (pos > menu->nItems)
2153 pos = menu->nItems;
2154 } else {
2155 if (!MENU_FindItem( &hMenu, &pos, flags ))
2156 pos = menu->nItems;
2157 else {
2158 if (!(menu = MENU_GetMenu( hMenu )))
2159 return NULL;
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 && (menu->items[pos - 1].fType & MFT_BITMAP) &&
2168 (INT_PTR)menu->items[pos - 1].hbmpItem >= (INT_PTR)HBMMENU_SYSTEM &&
2169 (INT_PTR)menu->items[pos - 1].hbmpItem <= (INT_PTR)HBMMENU_MBAR_CLOSE_D)
2170 pos--;
2172 TRACE("inserting at %u by pos %u\n", pos, flags & MF_BYPOSITION);
2174 /* Create new items array */
2176 newItems = HeapAlloc( GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
2177 if (!newItems)
2179 WARN("allocation failed\n" );
2180 return NULL;
2182 if (menu->nItems > 0)
2184 /* Copy the old array into the new one */
2185 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
2186 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
2187 (menu->nItems-pos)*sizeof(MENUITEM) );
2188 HeapFree( GetProcessHeap(), 0, menu->items );
2190 menu->items = newItems;
2191 menu->nItems++;
2192 memset( &newItems[pos], 0, sizeof(*newItems) );
2193 menu->Height = 0; /* force size recalculate */
2194 return &newItems[pos];
2198 /**********************************************************************
2199 * MENU_ParseResource
2201 * Parse a standard menu resource and add items to the menu.
2202 * Return a pointer to the end of the resource.
2204 * NOTE: flags is equivalent to the mtOption field
2206 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
2208 WORD flags, id = 0;
2209 LPCSTR str;
2210 BOOL end_flag;
2214 flags = GET_WORD(res);
2215 end_flag = flags & MF_END;
2216 /* Remove MF_END because it has the same value as MF_HILITE */
2217 flags &= ~MF_END;
2218 res += sizeof(WORD);
2219 if (!(flags & MF_POPUP))
2221 id = GET_WORD(res);
2222 res += sizeof(WORD);
2224 str = res;
2225 if (!unicode) res += strlen(str) + 1;
2226 else res += (strlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
2227 if (flags & MF_POPUP)
2229 HMENU hSubMenu = CreatePopupMenu();
2230 if (!hSubMenu) return NULL;
2231 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
2232 return NULL;
2233 if (!unicode) AppendMenuA( hMenu, flags, (UINT_PTR)hSubMenu, str );
2234 else AppendMenuW( hMenu, flags, (UINT_PTR)hSubMenu, (LPCWSTR)str );
2236 else /* Not a popup */
2238 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
2239 else AppendMenuW( hMenu, flags, id,
2240 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
2242 } while (!end_flag);
2243 return res;
2247 /**********************************************************************
2248 * MENUEX_ParseResource
2250 * Parse an extended menu resource and add items to the menu.
2251 * Return a pointer to the end of the resource.
2253 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
2255 WORD resinfo;
2256 do {
2257 MENUITEMINFOW mii;
2259 mii.cbSize = sizeof(mii);
2260 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
2261 mii.fType = GET_DWORD(res);
2262 res += sizeof(DWORD);
2263 mii.fState = GET_DWORD(res);
2264 res += sizeof(DWORD);
2265 mii.wID = GET_DWORD(res);
2266 res += sizeof(DWORD);
2267 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
2268 res += sizeof(WORD);
2269 /* Align the text on a word boundary. */
2270 res += (~((UINT_PTR)res - 1)) & 1;
2271 mii.dwTypeData = (LPWSTR) res;
2272 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
2273 /* Align the following fields on a dword boundary. */
2274 res += (~((UINT_PTR)res - 1)) & 3;
2276 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
2277 mii.fType, mii.fState, mii.wID, resinfo, debugstr_w(mii.dwTypeData));
2279 if (resinfo & 1) { /* Pop-up? */
2280 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
2281 res += sizeof(DWORD);
2282 mii.hSubMenu = CreatePopupMenu();
2283 if (!mii.hSubMenu)
2284 return NULL;
2285 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
2286 DestroyMenu(mii.hSubMenu);
2287 return NULL;
2289 mii.fMask |= MIIM_SUBMENU;
2290 mii.fType |= MF_POPUP;
2292 else if(!*mii.dwTypeData && !(mii.fType & MF_SEPARATOR))
2294 WARN("Converting NULL menu item %04x, type %04x to SEPARATOR\n",
2295 mii.wID, mii.fType);
2296 mii.fType |= MF_SEPARATOR;
2298 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2299 } while (!(resinfo & MF_END));
2300 return res;
2304 /***********************************************************************
2305 * MENU_GetSubPopup
2307 * Return the handle of the selected sub-popup menu (if any).
2309 static HMENU MENU_GetSubPopup( HMENU hmenu )
2311 POPUPMENU *menu;
2312 MENUITEM *item;
2314 menu = MENU_GetMenu( hmenu );
2316 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2318 item = &menu->items[menu->FocusedItem];
2319 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2320 return item->hSubMenu;
2321 return 0;
2325 /***********************************************************************
2326 * MENU_HideSubPopups
2328 * Hide the sub-popup menus of this menu.
2330 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2331 BOOL sendMenuSelect, UINT wFlags )
2333 POPUPMENU *menu = MENU_GetMenu( hmenu );
2335 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2337 if (menu && top_popup)
2339 HMENU hsubmenu;
2340 POPUPMENU *submenu;
2341 MENUITEM *item;
2343 if (menu->FocusedItem != NO_SELECTED_ITEM)
2345 item = &menu->items[menu->FocusedItem];
2346 if (!(item->fType & MF_POPUP) ||
2347 !(item->fState & MF_MOUSESELECT)) return;
2348 item->fState &= ~MF_MOUSESELECT;
2349 hsubmenu = item->hSubMenu;
2350 } else return;
2352 if (!(submenu = MENU_GetMenu( hsubmenu ))) return;
2353 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE, wFlags );
2354 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2355 DestroyWindow( submenu->hWnd );
2356 submenu->hWnd = 0;
2358 if (!(wFlags & TPM_NONOTIFY))
2359 SendMessageW( hwndOwner, WM_UNINITMENUPOPUP, (WPARAM)hsubmenu,
2360 MAKELPARAM(0, IS_SYSTEM_MENU(submenu)) );
2365 /***********************************************************************
2366 * MENU_ShowSubPopup
2368 * Display the sub-menu of the selected item of this menu.
2369 * Return the handle of the submenu, or hmenu if no submenu to display.
2371 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2372 BOOL selectFirst, UINT wFlags )
2374 RECT rect;
2375 POPUPMENU *menu;
2376 MENUITEM *item;
2377 HDC hdc;
2379 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, selectFirst);
2381 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2383 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
2385 item = &menu->items[menu->FocusedItem];
2386 if (!(item->fType & MF_POPUP) || (item->fState & (MF_GRAYED | MF_DISABLED)))
2387 return hmenu;
2389 /* message must be sent before using item,
2390 because nearly everything may be changed by the application ! */
2392 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2393 if (!(wFlags & TPM_NONOTIFY))
2394 SendMessageW( hwndOwner, WM_INITMENUPOPUP, (WPARAM)item->hSubMenu,
2395 MAKELPARAM( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2397 item = &menu->items[menu->FocusedItem];
2398 rect = item->rect;
2400 /* correct item if modified as a reaction to WM_INITMENUPOPUP message */
2401 if (!(item->fState & MF_HILITE))
2403 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2404 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2406 SelectObject( hdc, get_menu_font(FALSE));
2408 item->fState |= MF_HILITE;
2409 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2410 ReleaseDC( menu->hWnd, hdc );
2412 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2413 item->rect = rect;
2415 item->fState |= MF_MOUSESELECT;
2417 if (IS_SYSTEM_MENU(menu))
2419 MENU_InitSysMenuPopup(item->hSubMenu,
2420 GetWindowLongW( menu->hWnd, GWL_STYLE ),
2421 GetClassLongW( menu->hWnd, GCL_STYLE));
2423 NC_GetSysPopupPos( menu->hWnd, &rect );
2424 rect.top = rect.bottom;
2425 rect.right = GetSystemMetrics(SM_CXSIZE);
2426 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2428 else
2430 GetWindowRect( menu->hWnd, &rect );
2431 if (menu->wFlags & MF_POPUP)
2433 RECT rc = item->rect;
2435 MENU_AdjustMenuItemRect(menu, &rc);
2437 /* The first item in the popup menu has to be at the
2438 same y position as the focused menu item */
2439 rect.left += rc.right - GetSystemMetrics(SM_CXBORDER);
2440 rect.top += rc.top - MENU_TOP_MARGIN;
2441 rect.right = rc.left - rc.right + GetSystemMetrics(SM_CXBORDER);
2442 rect.bottom = rc.top - rc.bottom - MENU_TOP_MARGIN
2443 - MENU_BOTTOM_MARGIN - GetSystemMetrics(SM_CYBORDER);
2445 else
2447 rect.left += item->rect.left;
2448 rect.top += item->rect.bottom;
2449 rect.right = item->rect.right - item->rect.left;
2450 rect.bottom = item->rect.bottom - item->rect.top;
2454 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem, 0,
2455 rect.left, rect.top, rect.right, rect.bottom );
2456 if (selectFirst)
2457 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2458 return item->hSubMenu;
2463 /**********************************************************************
2464 * MENU_IsMenuActive
2466 HWND MENU_IsMenuActive(void)
2468 return top_popup;
2471 /**********************************************************************
2472 * MENU_EndMenu
2474 * Calls EndMenu() if the hwnd parameter belongs to the menu owner
2476 * Does the (menu stuff) of the default window handling of WM_CANCELMODE
2478 void MENU_EndMenu( HWND hwnd )
2480 POPUPMENU *menu;
2481 menu = top_popup_hmenu ? MENU_GetMenu( top_popup_hmenu ) : NULL;
2482 if (menu && hwnd == menu->hwndOwner) EndMenu();
2485 /***********************************************************************
2486 * MENU_PtMenu
2488 * Walks menu chain trying to find a menu pt maps to.
2490 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2492 POPUPMENU *menu = MENU_GetMenu( hMenu );
2493 UINT item = menu->FocusedItem;
2494 HMENU ret;
2496 /* try subpopup first (if any) */
2497 ret = (item != NO_SELECTED_ITEM &&
2498 (menu->items[item].fType & MF_POPUP) &&
2499 (menu->items[item].fState & MF_MOUSESELECT))
2500 ? MENU_PtMenu(menu->items[item].hSubMenu, pt) : 0;
2502 if (!ret) /* check the current window (avoiding WM_HITTEST) */
2504 INT ht = NC_HandleNCHitTest( menu->hWnd, pt );
2505 if( menu->wFlags & MF_POPUP )
2507 if (ht != HTNOWHERE && ht != HTERROR) ret = hMenu;
2509 else if (ht == HTSYSMENU)
2510 ret = get_win_sys_menu( menu->hWnd );
2511 else if (ht == HTMENU)
2512 ret = GetMenu( menu->hWnd );
2514 return ret;
2517 /***********************************************************************
2518 * MENU_ExecFocusedItem
2520 * Execute a menu item (for instance when user pressed Enter).
2521 * Return the wID of the executed item. Otherwise, -1 indicating
2522 * that no menu item was executed, -2 if a popup is shown;
2523 * Have to receive the flags for the TrackPopupMenu options to avoid
2524 * sending unwanted message.
2527 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2529 MENUITEM *item;
2530 POPUPMENU *menu = MENU_GetMenu( hMenu );
2532 TRACE("%p hmenu=%p\n", pmt, hMenu);
2534 if (!menu || !menu->nItems ||
2535 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2537 item = &menu->items[menu->FocusedItem];
2539 TRACE("hMenu %p wID %08lx hSubMenu %p fType %04x\n", hMenu, item->wID, item->hSubMenu, item->fType);
2541 if (!(item->fType & MF_POPUP))
2543 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2545 /* If TPM_RETURNCMD is set you return the id, but
2546 do not send a message to the owner */
2547 if(!(wFlags & TPM_RETURNCMD))
2549 if( menu->wFlags & MF_SYSMENU )
2550 PostMessageW( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2551 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2552 else
2554 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2555 DWORD dwStyle = menu->dwStyle | (topmenu ? topmenu->dwStyle : 0);
2557 if (dwStyle & MNS_NOTIFYBYPOS)
2558 PostMessageW( pmt->hOwnerWnd, WM_MENUCOMMAND, menu->FocusedItem,
2559 (LPARAM)hMenu);
2560 else
2561 PostMessageW( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2564 return item->wID;
2567 else
2569 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2570 return -2;
2573 return -1;
2576 /***********************************************************************
2577 * MENU_SwitchTracking
2579 * Helper function for menu navigation routines.
2581 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id, UINT wFlags )
2583 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2584 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2586 TRACE("%p hmenu=%p 0x%04x\n", pmt, hPtMenu, id);
2588 if( pmt->hTopMenu != hPtMenu &&
2589 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2591 /* both are top level menus (system and menu-bar) */
2592 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2593 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2594 pmt->hTopMenu = hPtMenu;
2596 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2597 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2601 /***********************************************************************
2602 * MENU_ButtonDown
2604 * Return TRUE if we can go on with menu tracking.
2606 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2608 TRACE("%p hPtMenu=%p\n", pmt, hPtMenu);
2610 if (hPtMenu)
2612 UINT id = 0;
2613 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2614 MENUITEM *item;
2616 if( IS_SYSTEM_MENU(ptmenu) )
2617 item = ptmenu->items;
2618 else
2619 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2621 if( item )
2623 if( ptmenu->FocusedItem != id )
2624 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2626 /* If the popup menu is not already "popped" */
2627 if(!(item->fState & MF_MOUSESELECT ))
2629 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2632 return TRUE;
2634 /* Else the click was on the menu bar, finish the tracking */
2636 return FALSE;
2639 /***********************************************************************
2640 * MENU_ButtonUp
2642 * Return the value of MENU_ExecFocusedItem if
2643 * the selected item was not a popup. Else open the popup.
2644 * A -1 return value indicates that we go on with menu tracking.
2647 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2649 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2651 if (hPtMenu)
2653 UINT id = 0;
2654 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2655 MENUITEM *item;
2657 if( IS_SYSTEM_MENU(ptmenu) )
2658 item = ptmenu->items;
2659 else
2660 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2662 if( item && (ptmenu->FocusedItem == id ))
2664 debug_print_menuitem ("FocusedItem: ", item, "");
2666 if( !(item->fType & MF_POPUP) )
2668 INT executedMenuId = MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2669 if (executedMenuId == -1 || executedMenuId == -2) return -1;
2670 return executedMenuId;
2673 /* If we are dealing with the top-level menu */
2674 /* and this is a click on an already "popped" item: */
2675 /* Stop the menu tracking and close the opened submenus */
2676 if((pmt->hTopMenu == hPtMenu) && ptmenu->bTimeToHide)
2677 return 0;
2679 ptmenu->bTimeToHide = TRUE;
2681 return -1;
2685 /***********************************************************************
2686 * MENU_MouseMove
2688 * Return TRUE if we can go on with menu tracking.
2690 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2692 UINT id = NO_SELECTED_ITEM;
2693 POPUPMENU *ptmenu = NULL;
2695 if( hPtMenu )
2697 ptmenu = MENU_GetMenu( hPtMenu );
2698 if( IS_SYSTEM_MENU(ptmenu) )
2699 id = 0;
2700 else
2701 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2704 if( id == NO_SELECTED_ITEM )
2706 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2707 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2710 else if( ptmenu->FocusedItem != id )
2712 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2713 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2715 return TRUE;
2719 /***********************************************************************
2720 * MENU_DoNextMenu
2722 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2724 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk, UINT wFlags )
2726 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2727 BOOL atEnd = FALSE;
2729 /* When skipping left, we need to do something special after the
2730 first menu. */
2731 if (vk == VK_LEFT && menu->FocusedItem == 0)
2733 atEnd = TRUE;
2735 /* When skipping right, for the non-system menu, we need to
2736 handle the last non-special menu item (ie skip any window
2737 icons such as MDI maximize, restore or close) */
2738 else if ((vk == VK_RIGHT) && !IS_SYSTEM_MENU(menu))
2740 UINT i = menu->FocusedItem + 1;
2741 while (i < menu->nItems) {
2742 if ((menu->items[i].wID >= SC_SIZE &&
2743 menu->items[i].wID <= SC_RESTORE)) {
2744 i++;
2745 } else break;
2747 if (i == menu->nItems) {
2748 atEnd = TRUE;
2751 /* When skipping right, we need to cater for the system menu */
2752 else if ((vk == VK_RIGHT) && IS_SYSTEM_MENU(menu))
2754 if (menu->FocusedItem == (menu->nItems - 1)) {
2755 atEnd = TRUE;
2759 if( atEnd )
2761 MDINEXTMENU next_menu;
2762 HMENU hNewMenu;
2763 HWND hNewWnd;
2764 UINT id = 0;
2766 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2767 next_menu.hmenuNext = 0;
2768 next_menu.hwndNext = 0;
2769 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2771 TRACE("%p [%p] -> %p [%p]\n",
2772 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2774 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2776 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2777 hNewWnd = pmt->hOwnerWnd;
2778 if( IS_SYSTEM_MENU(menu) )
2780 /* switch to the menu bar */
2782 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2784 if( vk == VK_LEFT )
2786 menu = MENU_GetMenu( hNewMenu );
2787 id = menu->nItems - 1;
2789 /* Skip backwards over any system predefined icons,
2790 eg. MDI close, restore etc icons */
2791 while ((id > 0) &&
2792 (menu->items[id].wID >= SC_SIZE &&
2793 menu->items[id].wID <= SC_RESTORE)) id--;
2796 else if (style & WS_SYSMENU )
2798 /* switch to the system menu */
2799 hNewMenu = get_win_sys_menu( hNewWnd );
2801 else return FALSE;
2803 else /* application returned a new menu to switch to */
2805 hNewMenu = next_menu.hmenuNext;
2806 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2808 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2810 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2812 if (style & WS_SYSMENU &&
2813 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2815 /* get the real system menu */
2816 hNewMenu = get_win_sys_menu(hNewWnd);
2818 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2820 /* FIXME: Not sure what to do here;
2821 * perhaps try to track hNewMenu as a popup? */
2823 TRACE(" -- got confused.\n");
2824 return FALSE;
2827 else return FALSE;
2830 if( hNewMenu != pmt->hTopMenu )
2832 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2833 FALSE, 0 );
2834 if( pmt->hCurrentMenu != pmt->hTopMenu )
2835 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2838 if( hNewWnd != pmt->hOwnerWnd )
2840 pmt->hOwnerWnd = hNewWnd;
2841 set_capture_window( pmt->hOwnerWnd, GUI_INMENUMODE, NULL );
2844 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2845 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2847 return TRUE;
2849 return FALSE;
2852 /***********************************************************************
2853 * MENU_SuspendPopup
2855 * The idea is not to show the popup if the next input message is
2856 * going to hide it anyway.
2858 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2860 MSG msg;
2862 msg.hwnd = pmt->hOwnerWnd;
2864 PeekMessageW( &msg, 0, uMsg, uMsg, PM_NOYIELD | PM_REMOVE);
2865 pmt->trackFlags |= TF_SKIPREMOVE;
2867 switch( uMsg )
2869 case WM_KEYDOWN:
2870 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2871 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2873 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2874 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2875 if( msg.message == WM_KEYDOWN &&
2876 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2878 pmt->trackFlags |= TF_SUSPENDPOPUP;
2879 return TRUE;
2882 break;
2885 /* failures go through this */
2886 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2887 return FALSE;
2890 /***********************************************************************
2891 * MENU_KeyEscape
2893 * Handle a VK_ESCAPE key event in a menu.
2895 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2897 BOOL bEndMenu = TRUE;
2899 if (pmt->hCurrentMenu != pmt->hTopMenu)
2901 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2903 if (menu->wFlags & MF_POPUP)
2905 HMENU hmenutmp, hmenuprev;
2907 hmenuprev = hmenutmp = pmt->hTopMenu;
2909 /* close topmost popup */
2910 while (hmenutmp != pmt->hCurrentMenu)
2912 hmenuprev = hmenutmp;
2913 hmenutmp = MENU_GetSubPopup( hmenuprev );
2916 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2917 pmt->hCurrentMenu = hmenuprev;
2918 bEndMenu = FALSE;
2922 return bEndMenu;
2925 /***********************************************************************
2926 * MENU_KeyLeft
2928 * Handle a VK_LEFT key event in a menu.
2930 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2932 POPUPMENU *menu;
2933 HMENU hmenutmp, hmenuprev;
2934 UINT prevcol;
2936 hmenuprev = hmenutmp = pmt->hTopMenu;
2937 menu = MENU_GetMenu( hmenutmp );
2939 /* Try to move 1 column left (if possible) */
2940 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2941 NO_SELECTED_ITEM ) {
2943 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2944 prevcol, TRUE, 0 );
2945 return;
2948 /* close topmost popup */
2949 while (hmenutmp != pmt->hCurrentMenu)
2951 hmenuprev = hmenutmp;
2952 hmenutmp = MENU_GetSubPopup( hmenuprev );
2955 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2956 pmt->hCurrentMenu = hmenuprev;
2958 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2960 /* move menu bar selection if no more popups are left */
2962 if( !MENU_DoNextMenu( pmt, VK_LEFT, wFlags ) )
2963 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2965 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2967 /* A sublevel menu was displayed - display the next one
2968 * unless there is another displacement coming up */
2970 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2971 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2972 pmt->hTopMenu, TRUE, wFlags);
2978 /***********************************************************************
2979 * MENU_KeyRight
2981 * Handle a VK_RIGHT key event in a menu.
2983 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2985 HMENU hmenutmp;
2986 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2987 UINT nextcol;
2989 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2990 pmt->hCurrentMenu,
2991 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2992 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2994 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2996 /* If already displaying a popup, try to display sub-popup */
2998 hmenutmp = pmt->hCurrentMenu;
2999 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
3001 /* if subpopup was displayed then we are done */
3002 if (hmenutmp != pmt->hCurrentMenu) return;
3005 /* Check to see if there's another column */
3006 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
3007 NO_SELECTED_ITEM ) {
3008 TRACE("Going to %d.\n", nextcol );
3009 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
3010 nextcol, TRUE, 0 );
3011 return;
3014 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
3016 if( pmt->hCurrentMenu != pmt->hTopMenu )
3018 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
3019 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
3020 } else hmenutmp = 0;
3022 /* try to move to the next item */
3023 if( !MENU_DoNextMenu( pmt, VK_RIGHT, wFlags ) )
3024 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
3026 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
3027 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
3028 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
3029 pmt->hTopMenu, TRUE, wFlags);
3033 static void CALLBACK release_capture( BOOL __normal )
3035 set_capture_window( 0, GUI_INMENUMODE, NULL );
3038 /***********************************************************************
3039 * MENU_TrackMenu
3041 * Menu tracking code.
3043 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
3044 HWND hwnd, const RECT *lprect )
3046 MSG msg;
3047 POPUPMENU *menu;
3048 BOOL fRemove;
3049 INT executedMenuId = -1;
3050 MTRACKER mt;
3051 BOOL enterIdleSent = FALSE;
3052 HWND capture_win;
3054 mt.trackFlags = 0;
3055 mt.hCurrentMenu = hmenu;
3056 mt.hTopMenu = hmenu;
3057 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
3058 mt.pt.x = x;
3059 mt.pt.y = y;
3061 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p %s\n",
3062 hmenu, wFlags, x, y, hwnd, wine_dbgstr_rect( lprect));
3064 fEndMenu = FALSE;
3065 if (!(menu = MENU_GetMenu( hmenu )))
3067 WARN("Invalid menu handle %p\n", hmenu);
3068 SetLastError(ERROR_INVALID_MENU_HANDLE);
3069 return FALSE;
3072 if (wFlags & TPM_BUTTONDOWN)
3074 /* Get the result in order to start the tracking or not */
3075 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3076 fEndMenu = !fRemove;
3079 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
3081 /* owner may not be visible when tracking a popup, so use the menu itself */
3082 capture_win = (wFlags & TPM_POPUPMENU) ? menu->hWnd : mt.hOwnerWnd;
3083 set_capture_window( capture_win, GUI_INMENUMODE, NULL );
3085 __TRY while (!fEndMenu)
3087 menu = MENU_GetMenu( mt.hCurrentMenu );
3088 if (!menu) /* sometimes happens if I do a window manager close */
3089 break;
3091 /* we have to keep the message in the queue until it's
3092 * clear that menu loop is not over yet. */
3094 for (;;)
3096 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
3098 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
3099 /* remove the message from the queue */
3100 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3102 else
3104 if (!enterIdleSent)
3106 HWND win = menu->wFlags & MF_POPUP ? menu->hWnd : 0;
3107 enterIdleSent = TRUE;
3108 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
3110 WaitMessage();
3114 /* check if EndMenu() tried to cancel us, by posting this message */
3115 if(msg.message == WM_CANCELMODE)
3117 /* we are now out of the loop */
3118 fEndMenu = TRUE;
3120 /* remove the message from the queue */
3121 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3123 /* break out of internal loop, ala ESCAPE */
3124 break;
3127 TranslateMessage( &msg );
3128 mt.pt = msg.pt;
3130 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
3131 enterIdleSent=FALSE;
3133 fRemove = FALSE;
3134 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
3137 * Use the mouse coordinates in lParam instead of those in the MSG
3138 * struct to properly handle synthetic messages. They are already
3139 * in screen coordinates.
3141 mt.pt.x = (short)LOWORD(msg.lParam);
3142 mt.pt.y = (short)HIWORD(msg.lParam);
3144 /* Find a menu for this mouse event */
3145 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
3147 switch(msg.message)
3149 /* no WM_NC... messages in captured state */
3151 case WM_RBUTTONDBLCLK:
3152 case WM_RBUTTONDOWN:
3153 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3154 /* fall through */
3155 case WM_LBUTTONDBLCLK:
3156 case WM_LBUTTONDOWN:
3157 /* If the message belongs to the menu, removes it from the queue */
3158 /* Else, end menu tracking */
3159 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3160 fEndMenu = !fRemove;
3161 break;
3163 case WM_RBUTTONUP:
3164 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3165 /* fall through */
3166 case WM_LBUTTONUP:
3167 /* Check if a menu was selected by the mouse */
3168 if (hmenu)
3170 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
3171 TRACE("executedMenuId %d\n", executedMenuId);
3173 /* End the loop if executedMenuId is an item ID */
3174 /* or if the job was done (executedMenuId = 0). */
3175 fEndMenu = fRemove = (executedMenuId != -1);
3177 /* No menu was selected by the mouse */
3178 /* if the function was called by TrackPopupMenu, continue
3179 with the menu tracking. If not, stop it */
3180 else
3181 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
3183 break;
3185 case WM_MOUSEMOVE:
3186 /* the selected menu item must be changed every time */
3187 /* the mouse moves. */
3189 if (hmenu)
3190 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
3192 } /* switch(msg.message) - mouse */
3194 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
3196 fRemove = TRUE; /* Keyboard messages are always removed */
3197 switch(msg.message)
3199 case WM_KEYDOWN:
3200 case WM_SYSKEYDOWN:
3201 switch(msg.wParam)
3203 case VK_MENU:
3204 fEndMenu = TRUE;
3205 break;
3207 case VK_HOME:
3208 case VK_END:
3209 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
3210 NO_SELECTED_ITEM, FALSE, 0 );
3211 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3212 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
3213 break;
3215 case VK_UP:
3216 case VK_DOWN: /* If on menu bar, pull-down the menu */
3218 menu = MENU_GetMenu( mt.hCurrentMenu );
3219 if (!(menu->wFlags & MF_POPUP))
3220 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
3221 else /* otherwise try to move selection */
3222 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3223 (msg.wParam == VK_UP)? ITEM_PREV : ITEM_NEXT );
3224 break;
3226 case VK_LEFT:
3227 MENU_KeyLeft( &mt, wFlags );
3228 break;
3230 case VK_RIGHT:
3231 MENU_KeyRight( &mt, wFlags );
3232 break;
3234 case VK_ESCAPE:
3235 fEndMenu = MENU_KeyEscape(&mt, wFlags);
3236 break;
3238 case VK_F1:
3240 HELPINFO hi;
3241 hi.cbSize = sizeof(HELPINFO);
3242 hi.iContextType = HELPINFO_MENUITEM;
3243 if (menu->FocusedItem == NO_SELECTED_ITEM)
3244 hi.iCtrlId = 0;
3245 else
3246 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
3247 hi.hItemHandle = hmenu;
3248 hi.dwContextId = menu->dwContextHelpID;
3249 hi.MousePos = msg.pt;
3250 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
3251 break;
3254 default:
3255 break;
3257 break; /* WM_KEYDOWN */
3259 case WM_CHAR:
3260 case WM_SYSCHAR:
3262 UINT pos;
3264 if (msg.wParam == '\r' || msg.wParam == ' ')
3266 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3267 fEndMenu = (executedMenuId != -2);
3269 break;
3272 /* Hack to avoid control chars. */
3273 /* We will find a better way real soon... */
3274 if (msg.wParam < 32) break;
3276 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3277 LOWORD(msg.wParam), FALSE );
3278 if (pos == (UINT)-2) fEndMenu = TRUE;
3279 else if (pos == (UINT)-1) MessageBeep(0);
3280 else
3282 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3283 TRUE, 0 );
3284 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3285 fEndMenu = (executedMenuId != -2);
3288 break;
3289 } /* switch(msg.message) - kbd */
3291 else
3293 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3294 DispatchMessageW( &msg );
3295 continue;
3298 if (!fEndMenu) fRemove = TRUE;
3300 /* finally remove message from the queue */
3302 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3303 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3304 else mt.trackFlags &= ~TF_SKIPREMOVE;
3306 __FINALLY( release_capture )
3308 /* If dropdown is still painted and the close box is clicked on
3309 then the menu will be destroyed as part of the DispatchMessage above.
3310 This will then invalidate the menu handle in mt.hTopMenu. We should
3311 check for this first. */
3312 if( IsMenu( mt.hTopMenu ) )
3314 menu = MENU_GetMenu( mt.hTopMenu );
3316 if( IsWindow( mt.hOwnerWnd ) )
3318 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE, wFlags );
3320 if (menu && (menu->wFlags & MF_POPUP))
3322 DestroyWindow( menu->hWnd );
3323 menu->hWnd = 0;
3325 if (!(wFlags & TPM_NONOTIFY))
3326 SendMessageW( mt.hOwnerWnd, WM_UNINITMENUPOPUP, (WPARAM)mt.hTopMenu,
3327 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3329 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3330 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKEWPARAM(0,0xffff), 0 );
3333 /* Reset the variable for hiding menu */
3334 if( menu ) menu->bTimeToHide = FALSE;
3337 /* The return value is only used by TrackPopupMenu */
3338 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
3339 if (executedMenuId == -1) executedMenuId = 0;
3340 return executedMenuId;
3343 /***********************************************************************
3344 * MENU_InitTracking
3346 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3348 POPUPMENU *menu;
3350 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
3352 HideCaret(0);
3354 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3355 if (!(wFlags & TPM_NONOTIFY))
3356 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3358 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
3360 if (!(wFlags & TPM_NONOTIFY))
3362 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
3363 /* If an app changed/recreated menu bar entries in WM_INITMENU
3364 * menu sizes will be recalculated once the menu created/shown.
3368 /* This makes the menus of applications built with Delphi work.
3369 * It also enables menus to be displayed in more than one window,
3370 * but there are some bugs left that need to be fixed in this case.
3372 if ((menu = MENU_GetMenu( hMenu ))) menu->hWnd = hWnd;
3374 return TRUE;
3376 /***********************************************************************
3377 * MENU_ExitTracking
3379 static BOOL MENU_ExitTracking(HWND hWnd)
3381 TRACE("hwnd=%p\n", hWnd);
3383 SendMessageW( hWnd, WM_EXITMENULOOP, 0, 0 );
3384 ShowCaret(0);
3385 top_popup = 0;
3386 top_popup_hmenu = NULL;
3387 return TRUE;
3390 /***********************************************************************
3391 * MENU_TrackMouseMenuBar
3393 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3395 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3397 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3398 UINT wFlags = TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3400 TRACE("wnd=%p ht=0x%04x %s\n", hWnd, ht, wine_dbgstr_point( &pt));
3402 if (IsMenu(hMenu))
3404 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3405 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3406 MENU_ExitTracking(hWnd);
3411 /***********************************************************************
3412 * MENU_TrackKbdMenuBar
3414 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3416 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3418 UINT uItem = NO_SELECTED_ITEM;
3419 HMENU hTrackMenu;
3420 UINT wFlags = TPM_LEFTALIGN | TPM_LEFTBUTTON;
3422 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3424 /* find window that has a menu */
3426 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3427 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3429 /* check if we have to track a system menu */
3431 hTrackMenu = GetMenu( hwnd );
3432 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3434 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3435 hTrackMenu = get_win_sys_menu( hwnd );
3436 uItem = 0;
3437 wParam |= HTSYSMENU; /* prevent item lookup */
3440 if (!IsMenu( hTrackMenu )) return;
3442 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3444 if( wChar && wChar != ' ' )
3446 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3447 if ( uItem >= (UINT)(-2) )
3449 if( uItem == (UINT)(-1) ) MessageBeep(0);
3450 /* schedule end of menu tracking */
3451 wFlags |= TF_ENDMENU;
3452 goto track_menu;
3456 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3458 if (!(wParam & HTSYSMENU) || wChar == ' ')
3460 if( uItem == NO_SELECTED_ITEM )
3461 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3462 else
3463 PostMessageW( hwnd, WM_KEYDOWN, VK_DOWN, 0L );
3466 track_menu:
3467 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3468 MENU_ExitTracking( hwnd );
3471 /**********************************************************************
3472 * TrackPopupMenuEx (USER32.@)
3474 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3475 HWND hWnd, LPTPMPARAMS lpTpm )
3477 BOOL ret = FALSE;
3479 TRACE("hmenu %p flags %04x (%d,%d) hwnd %p lpTpm %p rect %s\n",
3480 hMenu, wFlags, x, y, hWnd, lpTpm,
3481 lpTpm ? wine_dbgstr_rect( &lpTpm->rcExclude) : "-" );
3483 /* Parameter check */
3484 /* FIXME: this check is performed several times, here and in the called
3485 functions. That could be optimized */
3486 if (!MENU_GetMenu( hMenu ))
3488 SetLastError( ERROR_INVALID_MENU_HANDLE );
3489 return FALSE;
3492 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3494 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3495 if (!(wFlags & TPM_NONOTIFY))
3496 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3498 if (MENU_ShowPopup( hWnd, hMenu, 0, wFlags, x, y, 0, 0 ))
3499 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,
3500 lpTpm ? &lpTpm->rcExclude : NULL );
3501 MENU_ExitTracking(hWnd);
3503 return ret;
3506 /**********************************************************************
3507 * TrackPopupMenu (USER32.@)
3509 * Like the win32 API, the function return the command ID only if the
3510 * flag TPM_RETURNCMD is on.
3513 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3514 INT nReserved, HWND hWnd, const RECT *lpRect )
3516 return TrackPopupMenuEx( hMenu, wFlags, x, y, hWnd, NULL);
3519 /***********************************************************************
3520 * PopupMenuWndProc
3522 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3524 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3526 TRACE("hwnd=%p msg=0x%04x wp=0x%04lx lp=0x%08lx\n", hwnd, message, wParam, lParam);
3528 switch(message)
3530 case WM_CREATE:
3532 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3533 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)cs->lpCreateParams );
3534 return 0;
3537 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3538 return MA_NOACTIVATE;
3540 case WM_PAINT:
3542 PAINTSTRUCT ps;
3543 BeginPaint( hwnd, &ps );
3544 MENU_DrawPopupMenu( hwnd, ps.hdc,
3545 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3546 EndPaint( hwnd, &ps );
3547 return 0;
3549 case WM_ERASEBKGND:
3550 return 1;
3552 case WM_DESTROY:
3553 /* zero out global pointer in case resident popup window was destroyed. */
3554 if (hwnd == top_popup) {
3555 top_popup = 0;
3556 top_popup_hmenu = NULL;
3558 break;
3560 case WM_SHOWWINDOW:
3562 if( wParam )
3564 if (!GetWindowLongPtrW( hwnd, 0 )) ERR("no menu to display\n");
3566 else
3567 SetWindowLongPtrW( hwnd, 0, 0 );
3568 break;
3570 case MM_SETMENUHANDLE:
3571 SetWindowLongPtrW( hwnd, 0, wParam );
3572 break;
3574 case MM_GETMENUHANDLE:
3575 return GetWindowLongPtrW( hwnd, 0 );
3577 default:
3578 return DefWindowProcW( hwnd, message, wParam, lParam );
3580 return 0;
3584 /***********************************************************************
3585 * MENU_GetMenuBarHeight
3587 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3589 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3590 INT orgX, INT orgY )
3592 HDC hdc;
3593 RECT rectBar;
3594 LPPOPUPMENU lppop;
3596 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3598 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3600 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3601 SelectObject( hdc, get_menu_font(FALSE));
3602 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3603 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3604 ReleaseDC( hwnd, hdc );
3605 return lppop->Height;
3609 /*******************************************************************
3610 * ChangeMenuA (USER32.@)
3612 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3613 UINT id, UINT flags )
3615 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3616 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3617 id, data );
3618 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3619 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3620 id, data );
3621 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3622 flags & MF_BYPOSITION ? pos : id,
3623 flags & ~MF_REMOVE );
3624 /* Default: MF_INSERT */
3625 return InsertMenuA( hMenu, pos, flags, id, data );
3629 /*******************************************************************
3630 * ChangeMenuW (USER32.@)
3632 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3633 UINT id, UINT flags )
3635 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3636 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3637 id, data );
3638 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3639 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3640 id, data );
3641 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3642 flags & MF_BYPOSITION ? pos : id,
3643 flags & ~MF_REMOVE );
3644 /* Default: MF_INSERT */
3645 return InsertMenuW( hMenu, pos, flags, id, data );
3649 /*******************************************************************
3650 * CheckMenuItem (USER32.@)
3652 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3654 MENUITEM *item;
3655 DWORD ret;
3657 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3658 ret = item->fState & MF_CHECKED;
3659 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3660 else item->fState &= ~MF_CHECKED;
3661 return ret;
3665 /**********************************************************************
3666 * EnableMenuItem (USER32.@)
3668 BOOL WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3670 UINT oldflags;
3671 MENUITEM *item;
3672 POPUPMENU *menu;
3674 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3676 /* Get the Popupmenu to access the owner menu */
3677 if (!(menu = MENU_GetMenu(hMenu)))
3678 return (UINT)-1;
3680 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3681 return (UINT)-1;
3683 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3684 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3686 /* If the close item in the system menu change update the close button */
3687 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3689 if (menu->hSysMenuOwner != 0)
3691 RECT rc;
3692 POPUPMENU* parentMenu;
3694 /* Get the parent menu to access*/
3695 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3696 return (UINT)-1;
3698 /* Refresh the frame to reflect the change */
3699 GetWindowRect(parentMenu->hWnd, &rc);
3700 MapWindowPoints(0, parentMenu->hWnd, (POINT *)&rc, 2);
3701 rc.bottom = 0;
3702 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3706 return oldflags;
3710 /*******************************************************************
3711 * GetMenuStringA (USER32.@)
3713 INT WINAPI GetMenuStringA(
3714 HMENU hMenu, /* [in] menuhandle */
3715 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3716 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3717 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3718 UINT wFlags /* [in] MF_ flags */
3720 MENUITEM *item;
3722 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3723 if (str && nMaxSiz) str[0] = '\0';
3724 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3725 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3726 return 0;
3728 if (!item->text) return 0;
3729 if (!str || !nMaxSiz) return strlenW(item->text);
3730 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3731 str[nMaxSiz-1] = 0;
3732 TRACE("returning %s\n", debugstr_a(str));
3733 return strlen(str);
3737 /*******************************************************************
3738 * GetMenuStringW (USER32.@)
3740 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3741 LPWSTR str, INT nMaxSiz, UINT wFlags )
3743 MENUITEM *item;
3745 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3746 if (str && nMaxSiz) str[0] = '\0';
3747 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3748 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3749 return 0;
3751 if (!str || !nMaxSiz) return item->text ? strlenW(item->text) : 0;
3752 if( !(item->text)) {
3753 str[0] = 0;
3754 return 0;
3756 lstrcpynW( str, item->text, nMaxSiz );
3757 TRACE("returning %s\n", debugstr_w(str));
3758 return strlenW(str);
3762 /**********************************************************************
3763 * HiliteMenuItem (USER32.@)
3765 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3766 UINT wHilite )
3768 LPPOPUPMENU menu;
3769 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3770 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3771 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3772 if (menu->FocusedItem == wItemID) return TRUE;
3773 MENU_HideSubPopups( hWnd, hMenu, FALSE, 0 );
3774 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3775 return TRUE;
3779 /**********************************************************************
3780 * GetMenuState (USER32.@)
3782 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3784 MENUITEM *item;
3785 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3786 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3787 debug_print_menuitem (" item: ", item, "");
3788 if (item->fType & MF_POPUP)
3790 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3791 if (!menu) return -1;
3792 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3794 else
3796 /* We used to (from way back then) mask the result to 0xff. */
3797 /* I don't know why and it seems wrong as the documented */
3798 /* return flag MF_SEPARATOR is outside that mask. */
3799 return (item->fType | item->fState);
3804 /**********************************************************************
3805 * GetMenuItemCount (USER32.@)
3807 INT WINAPI GetMenuItemCount( HMENU hMenu )
3809 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3810 if (!menu) return -1;
3811 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3812 return menu->nItems;
3816 /**********************************************************************
3817 * GetMenuItemID (USER32.@)
3819 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3821 MENUITEM * lpmi;
3823 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
3824 if (lpmi->fType & MF_POPUP) return -1;
3825 return lpmi->wID;
3830 /*******************************************************************
3831 * InsertMenuW (USER32.@)
3833 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3834 UINT_PTR id, LPCWSTR str )
3836 MENUITEM *item;
3838 if (IS_STRING_ITEM(flags) && str)
3839 TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %s\n",
3840 hMenu, pos, flags, id, debugstr_w(str) );
3841 else TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %p (not a string)\n",
3842 hMenu, pos, flags, id, str );
3844 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3846 if (!(MENU_SetItemData( item, flags, id, str )))
3848 RemoveMenu( hMenu, pos, flags );
3849 return FALSE;
3852 item->hCheckBit = item->hUnCheckBit = 0;
3853 return TRUE;
3857 /*******************************************************************
3858 * InsertMenuA (USER32.@)
3860 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3861 UINT_PTR id, LPCSTR str )
3863 BOOL ret = FALSE;
3865 if (IS_STRING_ITEM(flags) && str)
3867 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3868 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3869 if (newstr)
3871 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3872 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3873 HeapFree( GetProcessHeap(), 0, newstr );
3875 return ret;
3877 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3881 /*******************************************************************
3882 * AppendMenuA (USER32.@)
3884 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3885 UINT_PTR id, LPCSTR data )
3887 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3891 /*******************************************************************
3892 * AppendMenuW (USER32.@)
3894 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3895 UINT_PTR id, LPCWSTR data )
3897 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3901 /**********************************************************************
3902 * RemoveMenu (USER32.@)
3904 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3906 LPPOPUPMENU menu;
3907 MENUITEM *item;
3909 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3910 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3911 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3913 /* Remove item */
3915 MENU_FreeItemData( item );
3917 if (--menu->nItems == 0)
3919 HeapFree( GetProcessHeap(), 0, menu->items );
3920 menu->items = NULL;
3922 else
3924 while(nPos < menu->nItems)
3926 *item = *(item+1);
3927 item++;
3928 nPos++;
3930 menu->items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
3931 menu->nItems * sizeof(MENUITEM) );
3933 return TRUE;
3937 /**********************************************************************
3938 * DeleteMenu (USER32.@)
3940 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3942 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3943 if (!item) return FALSE;
3944 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3945 /* nPos is now the position of the item */
3946 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3947 return TRUE;
3951 /*******************************************************************
3952 * ModifyMenuW (USER32.@)
3954 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3955 UINT_PTR id, LPCWSTR str )
3957 MENUITEM *item;
3959 if (IS_STRING_ITEM(flags))
3960 TRACE("%p %d %04x %04lx %s\n", hMenu, pos, flags, id, debugstr_w(str) );
3961 else
3962 TRACE("%p %d %04x %04lx %p\n", hMenu, pos, flags, id, str );
3964 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3965 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
3966 return MENU_SetItemData( item, flags, id, str );
3970 /*******************************************************************
3971 * ModifyMenuA (USER32.@)
3973 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3974 UINT_PTR id, LPCSTR str )
3976 BOOL ret = FALSE;
3978 if (IS_STRING_ITEM(flags) && str)
3980 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3981 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3982 if (newstr)
3984 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3985 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
3986 HeapFree( GetProcessHeap(), 0, newstr );
3988 return ret;
3990 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3994 /**********************************************************************
3995 * CreatePopupMenu (USER32.@)
3997 HMENU WINAPI CreatePopupMenu(void)
3999 HMENU hmenu;
4000 POPUPMENU *menu;
4002 if (!(hmenu = CreateMenu())) return 0;
4003 menu = MENU_GetMenu( hmenu );
4004 menu->wFlags |= MF_POPUP;
4005 menu->bTimeToHide = FALSE;
4006 return hmenu;
4010 /**********************************************************************
4011 * GetMenuCheckMarkDimensions (USER.417)
4012 * GetMenuCheckMarkDimensions (USER32.@)
4014 DWORD WINAPI GetMenuCheckMarkDimensions(void)
4016 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
4020 /**********************************************************************
4021 * SetMenuItemBitmaps (USER32.@)
4023 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
4024 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
4026 MENUITEM *item;
4028 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
4030 if (!hNewCheck && !hNewUnCheck)
4032 item->fState &= ~MF_USECHECKBITMAPS;
4034 else /* Install new bitmaps */
4036 item->hCheckBit = hNewCheck;
4037 item->hUnCheckBit = hNewUnCheck;
4038 item->fState |= MF_USECHECKBITMAPS;
4040 return TRUE;
4044 /**********************************************************************
4045 * CreateMenu (USER32.@)
4047 HMENU WINAPI CreateMenu(void)
4049 HMENU hMenu;
4050 LPPOPUPMENU menu;
4051 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
4052 menu = USER_HEAP_LIN_ADDR(hMenu);
4054 ZeroMemory(menu, sizeof(POPUPMENU));
4055 menu->wMagic = MENU_MAGIC;
4056 menu->FocusedItem = NO_SELECTED_ITEM;
4057 menu->bTimeToHide = FALSE;
4059 TRACE("return %p\n", hMenu );
4061 return hMenu;
4065 /**********************************************************************
4066 * DestroyMenu (USER32.@)
4068 BOOL WINAPI DestroyMenu( HMENU hMenu )
4070 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
4072 TRACE("(%p)\n", hMenu);
4075 if (!lppop) return FALSE;
4077 lppop->wMagic = 0; /* Mark it as destroyed */
4079 /* DestroyMenu should not destroy system menu popup owner */
4080 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
4082 DestroyWindow( lppop->hWnd );
4083 lppop->hWnd = 0;
4086 if (lppop->items) /* recursively destroy submenus */
4088 int i;
4089 MENUITEM *item = lppop->items;
4090 for (i = lppop->nItems; i > 0; i--, item++)
4092 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
4093 MENU_FreeItemData( item );
4095 HeapFree( GetProcessHeap(), 0, lppop->items );
4097 USER_HEAP_FREE( hMenu );
4098 return TRUE;
4102 /**********************************************************************
4103 * GetSystemMenu (USER32.@)
4105 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4107 WND *wndPtr = WIN_GetPtr( hWnd );
4108 HMENU retvalue = 0;
4110 if (wndPtr == WND_DESKTOP) return 0;
4111 if (wndPtr == WND_OTHER_PROCESS)
4113 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
4115 else if (wndPtr)
4117 if (wndPtr->hSysMenu && bRevert)
4119 DestroyMenu(wndPtr->hSysMenu);
4120 wndPtr->hSysMenu = 0;
4123 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
4124 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, 0 );
4126 if( wndPtr->hSysMenu )
4128 POPUPMENU *menu;
4129 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
4131 /* Store the dummy sysmenu handle to facilitate the refresh */
4132 /* of the close button if the SC_CLOSE item change */
4133 menu = MENU_GetMenu(retvalue);
4134 if ( menu )
4135 menu->hSysMenuOwner = wndPtr->hSysMenu;
4137 WIN_ReleasePtr( wndPtr );
4139 return bRevert ? 0 : retvalue;
4143 /*******************************************************************
4144 * SetSystemMenu (USER32.@)
4146 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4148 WND *wndPtr = WIN_GetPtr( hwnd );
4150 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
4152 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
4153 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
4154 WIN_ReleasePtr( wndPtr );
4155 return TRUE;
4157 return FALSE;
4161 /**********************************************************************
4162 * GetMenu (USER32.@)
4164 HMENU WINAPI GetMenu( HWND hWnd )
4166 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
4167 TRACE("for %p returning %p\n", hWnd, retvalue);
4168 return retvalue;
4171 /**********************************************************************
4172 * GetMenuBarInfo (USER32.@)
4174 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
4176 FIXME( "(%p,0x%08x,0x%08x,%p)\n", hwnd, idObject, idItem, pmbi );
4177 return FALSE;
4180 /**********************************************************************
4181 * MENU_SetMenu
4183 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
4184 * SetWindowPos call that would result if SetMenu were called directly.
4186 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
4188 TRACE("(%p, %p);\n", hWnd, hMenu);
4190 if (hMenu && !IsMenu(hMenu))
4192 WARN("hMenu %p is not a menu handle\n", hMenu);
4193 return FALSE;
4195 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4196 return FALSE;
4198 hWnd = WIN_GetFullHandle( hWnd );
4199 if (GetCapture() == hWnd)
4200 set_capture_window( 0, GUI_INMENUMODE, NULL ); /* release the capture */
4202 if (hMenu != 0)
4204 LPPOPUPMENU lpmenu;
4206 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
4208 lpmenu->hWnd = hWnd;
4209 lpmenu->Height = 0; /* Make sure we recalculate the size */
4211 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
4212 return TRUE;
4216 /**********************************************************************
4217 * SetMenu (USER32.@)
4219 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4221 if(!MENU_SetMenu(hWnd, hMenu))
4222 return FALSE;
4224 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4225 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4226 return TRUE;
4230 /**********************************************************************
4231 * GetSubMenu (USER32.@)
4233 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4235 MENUITEM * lpmi;
4237 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4238 if (!(lpmi->fType & MF_POPUP)) return 0;
4239 return lpmi->hSubMenu;
4243 /**********************************************************************
4244 * DrawMenuBar (USER32.@)
4246 BOOL WINAPI DrawMenuBar( HWND hWnd )
4248 LPPOPUPMENU lppop;
4249 HMENU hMenu = GetMenu(hWnd);
4251 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4252 return FALSE;
4253 if (!hMenu || !(lppop = MENU_GetMenu( hMenu ))) return FALSE;
4255 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4256 lppop->hwndOwner = hWnd;
4257 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4258 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4259 return TRUE;
4262 /***********************************************************************
4263 * DrawMenuBarTemp (USER32.@)
4265 * UNDOCUMENTED !!
4267 * called by W98SE desk.cpl Control Panel Applet
4269 * Not 100% sure about the param names, but close.
4271 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
4273 LPPOPUPMENU lppop;
4274 UINT i,retvalue;
4275 HFONT hfontOld = 0;
4276 BOOL flat_menu = FALSE;
4278 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
4280 if (!hMenu)
4281 hMenu = GetMenu(hwnd);
4283 if (!hFont)
4284 hFont = get_menu_font(FALSE);
4286 lppop = MENU_GetMenu( hMenu );
4287 if (lppop == NULL || lprect == NULL)
4289 retvalue = GetSystemMetrics(SM_CYMENU);
4290 goto END;
4293 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
4295 hfontOld = SelectObject( hDC, hFont);
4297 if (lppop->Height == 0)
4298 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
4300 lprect->bottom = lprect->top + lppop->Height;
4302 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
4304 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
4305 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
4306 LineTo( hDC, lprect->right, lprect->bottom );
4308 if (lppop->nItems == 0)
4310 retvalue = GetSystemMetrics(SM_CYMENU);
4311 goto END;
4314 for (i = 0; i < lppop->nItems; i++)
4316 MENU_DrawMenuItem( hwnd, hMenu, hwnd,
4317 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
4319 retvalue = lppop->Height;
4321 END:
4322 if (hfontOld) SelectObject (hDC, hfontOld);
4323 return retvalue;
4326 /***********************************************************************
4327 * EndMenu (USER.187)
4328 * EndMenu (USER32.@)
4330 BOOL WINAPI EndMenu(void)
4332 /* if we are in the menu code, and it is active */
4333 if (!fEndMenu && top_popup)
4335 /* terminate the menu handling code */
4336 fEndMenu = TRUE;
4338 /* needs to be posted to wakeup the internal menu handler */
4339 /* which will now terminate the menu, in the event that */
4340 /* the main window was minimized, or lost focus, so we */
4341 /* don't end up with an orphaned menu */
4342 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
4344 return fEndMenu;
4348 /***********************************************************************
4349 * LookupMenuHandle (USER.217)
4351 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
4353 HMENU hmenu32 = HMENU_32(hmenu);
4354 UINT id32 = id;
4355 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
4356 else return HMENU_16(hmenu32);
4360 /**********************************************************************
4361 * LoadMenu (USER.150)
4363 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, LPCSTR name )
4365 HRSRC16 hRsrc;
4366 HGLOBAL16 handle;
4367 HMENU16 hMenu;
4369 if (HIWORD(name) && name[0] == '#') name = ULongToPtr(atoi( name + 1 ));
4370 if (!name) return 0;
4372 instance = GetExePtr( instance );
4373 if (!(hRsrc = FindResource16( instance, name, (LPSTR)RT_MENU ))) return 0;
4374 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
4375 hMenu = LoadMenuIndirect16(LockResource16(handle));
4376 FreeResource16( handle );
4377 return hMenu;
4381 /*****************************************************************
4382 * LoadMenuA (USER32.@)
4384 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4386 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
4387 if (!hrsrc) return 0;
4388 return LoadMenuIndirectA( LoadResource( instance, hrsrc ));
4392 /*****************************************************************
4393 * LoadMenuW (USER32.@)
4395 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4397 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
4398 if (!hrsrc) return 0;
4399 return LoadMenuIndirectW( LoadResource( instance, hrsrc ));
4403 /**********************************************************************
4404 * LoadMenuIndirect (USER.220)
4406 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4408 HMENU hMenu;
4409 WORD version, offset;
4410 LPCSTR p = template;
4412 TRACE("(%p)\n", template );
4413 version = GET_WORD(p);
4414 p += sizeof(WORD);
4415 if (version)
4417 WARN("version must be 0 for Win16\n" );
4418 return 0;
4420 offset = GET_WORD(p);
4421 p += sizeof(WORD) + offset;
4422 if (!(hMenu = CreateMenu())) return 0;
4423 if (!MENU_ParseResource( p, hMenu, FALSE ))
4425 DestroyMenu( hMenu );
4426 return 0;
4428 return HMENU_16(hMenu);
4432 /**********************************************************************
4433 * LoadMenuIndirectW (USER32.@)
4435 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4437 HMENU hMenu;
4438 WORD version, offset;
4439 LPCSTR p = template;
4441 version = GET_WORD(p);
4442 p += sizeof(WORD);
4443 TRACE("%p, ver %d\n", template, version );
4444 switch (version)
4446 case 0: /* standard format is version of 0 */
4447 offset = GET_WORD(p);
4448 p += sizeof(WORD) + offset;
4449 if (!(hMenu = CreateMenu())) return 0;
4450 if (!MENU_ParseResource( p, hMenu, TRUE ))
4452 DestroyMenu( hMenu );
4453 return 0;
4455 return hMenu;
4456 case 1: /* extended format is version of 1 */
4457 offset = GET_WORD(p);
4458 p += sizeof(WORD) + offset;
4459 if (!(hMenu = CreateMenu())) return 0;
4460 if (!MENUEX_ParseResource( p, hMenu))
4462 DestroyMenu( hMenu );
4463 return 0;
4465 return hMenu;
4466 default:
4467 ERR("version %d not supported.\n", version);
4468 return 0;
4473 /**********************************************************************
4474 * LoadMenuIndirectA (USER32.@)
4476 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4478 return LoadMenuIndirectW( template );
4482 /**********************************************************************
4483 * IsMenu (USER32.@)
4485 BOOL WINAPI IsMenu(HMENU hmenu)
4487 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4489 if (!menu)
4491 SetLastError(ERROR_INVALID_MENU_HANDLE);
4492 return FALSE;
4494 return TRUE;
4497 /**********************************************************************
4498 * GetMenuItemInfo_common
4501 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4502 LPMENUITEMINFOW lpmii, BOOL unicode)
4504 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos ? MF_BYPOSITION : 0);
4506 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4508 if (!menu) {
4509 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
4510 return FALSE;
4513 if( lpmii->fMask & MIIM_TYPE) {
4514 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4515 WARN("invalid combination of fMask bits used\n");
4516 /* this does not happen on Win9x/ME */
4517 SetLastError( ERROR_INVALID_PARAMETER);
4518 return FALSE;
4520 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4521 if( menu->hbmpItem) lpmii->fType |= MFT_BITMAP;
4522 lpmii->hbmpItem = menu->hbmpItem; /* not on Win9x/ME */
4523 if( lpmii->fType & MFT_BITMAP) {
4524 lpmii->dwTypeData = (LPWSTR) menu->hbmpItem;
4525 lpmii->cch = 0;
4526 } else if( lpmii->fType & (MFT_OWNERDRAW | MFT_SEPARATOR)) {
4527 /* this does not happen on Win9x/ME */
4528 lpmii->dwTypeData = 0;
4529 lpmii->cch = 0;
4533 /* copy the text string */
4534 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING))) {
4535 if( !menu->text ) {
4536 if(lpmii->dwTypeData && lpmii->cch) {
4537 lpmii->cch = 0;
4538 if( unicode)
4539 *((WCHAR *)lpmii->dwTypeData) = 0;
4540 else
4541 *((CHAR *)lpmii->dwTypeData) = 0;
4543 } else {
4544 int len;
4545 if (unicode)
4547 len = strlenW(menu->text);
4548 if(lpmii->dwTypeData && lpmii->cch)
4549 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4551 else
4553 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL,
4554 0, NULL, NULL ) - 1;
4555 if(lpmii->dwTypeData && lpmii->cch)
4556 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4557 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4558 ((LPSTR)lpmii->dwTypeData)[lpmii->cch - 1] = 0;
4560 /* if we've copied a substring we return its length */
4561 if(lpmii->dwTypeData && lpmii->cch)
4562 if (lpmii->cch <= len + 1)
4563 lpmii->cch--;
4564 else
4565 lpmii->cch = len;
4566 else {
4567 /* return length of string */
4568 /* not on Win9x/ME if fType & MFT_BITMAP */
4569 lpmii->cch = len;
4574 if (lpmii->fMask & MIIM_FTYPE)
4575 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4577 if (lpmii->fMask & MIIM_BITMAP)
4578 lpmii->hbmpItem = menu->hbmpItem;
4580 if (lpmii->fMask & MIIM_STATE)
4581 lpmii->fState = menu->fState & MENUITEMINFO_STATE_MASK;
4583 if (lpmii->fMask & MIIM_ID)
4584 lpmii->wID = menu->wID;
4586 if (lpmii->fMask & MIIM_SUBMENU)
4587 lpmii->hSubMenu = menu->hSubMenu;
4588 else {
4589 /* hSubMenu is always cleared
4590 * (not on Win9x/ME ) */
4591 lpmii->hSubMenu = 0;
4594 if (lpmii->fMask & MIIM_CHECKMARKS) {
4595 lpmii->hbmpChecked = menu->hCheckBit;
4596 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4598 if (lpmii->fMask & MIIM_DATA)
4599 lpmii->dwItemData = menu->dwItemData;
4601 return TRUE;
4604 /**********************************************************************
4605 * GetMenuItemInfoA (USER32.@)
4607 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4608 LPMENUITEMINFOA lpmii)
4610 BOOL ret;
4611 MENUITEMINFOA mii;
4612 if( lpmii->cbSize != sizeof( mii) &&
4613 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4614 SetLastError( ERROR_INVALID_PARAMETER);
4615 return FALSE;
4617 memcpy( &mii, lpmii, lpmii->cbSize);
4618 mii.cbSize = sizeof( mii);
4619 ret = GetMenuItemInfo_common (hmenu, item, bypos,
4620 (LPMENUITEMINFOW)&mii, FALSE);
4621 mii.cbSize = lpmii->cbSize;
4622 memcpy( lpmii, &mii, mii.cbSize);
4623 return ret;
4626 /**********************************************************************
4627 * GetMenuItemInfoW (USER32.@)
4629 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4630 LPMENUITEMINFOW lpmii)
4632 BOOL ret;
4633 MENUITEMINFOW mii;
4634 if( lpmii->cbSize != sizeof( mii) &&
4635 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4636 SetLastError( ERROR_INVALID_PARAMETER);
4637 return FALSE;
4639 memcpy( &mii, lpmii, lpmii->cbSize);
4640 mii.cbSize = sizeof( mii);
4641 ret = GetMenuItemInfo_common (hmenu, item, bypos, &mii, TRUE);
4642 mii.cbSize = lpmii->cbSize;
4643 memcpy( lpmii, &mii, mii.cbSize);
4644 return ret;
4648 /* set a menu item text from a ASCII or Unicode string */
4649 static inline void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4651 if (!text)
4652 menu->text = NULL;
4653 else if (unicode)
4655 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4656 strcpyW( menu->text, text );
4658 else
4660 LPCSTR str = (LPCSTR)text;
4661 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4662 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4663 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4668 /**********************************************************************
4669 * SetMenuItemInfo_common
4672 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4673 const MENUITEMINFOW *lpmii,
4674 BOOL unicode)
4676 if (!menu) return FALSE;
4678 debug_print_menuitem("SetMenuItemInfo_common from: ", menu, "");
4680 if (lpmii->fMask & MIIM_TYPE ) {
4681 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4682 WARN("invalid combination of fMask bits used\n");
4683 /* this does not happen on Win9x/ME */
4684 SetLastError( ERROR_INVALID_PARAMETER);
4685 return FALSE;
4688 /* Remove the old type bits and replace them with the new ones */
4689 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4690 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4692 if (IS_STRING_ITEM(menu->fType)) {
4693 HeapFree(GetProcessHeap(), 0, menu->text);
4694 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4695 } else if( (menu->fType) & MFT_BITMAP)
4696 menu->hbmpItem = HBITMAP_32(LOWORD(lpmii->dwTypeData));
4699 if (lpmii->fMask & MIIM_FTYPE ) {
4700 if(( lpmii->fType & MFT_BITMAP)) {
4701 SetLastError( ERROR_INVALID_PARAMETER);
4702 return FALSE;
4704 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4705 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4707 if (lpmii->fMask & MIIM_STRING ) {
4708 /* free the string when used */
4709 HeapFree(GetProcessHeap(), 0, menu->text);
4710 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4713 if (lpmii->fMask & MIIM_STATE)
4715 /* Other menu items having MFS_DEFAULT are not converted
4716 to normal items */
4717 menu->fState = lpmii->fState & MENUITEMINFO_STATE_MASK;
4720 if (lpmii->fMask & MIIM_ID)
4721 menu->wID = lpmii->wID;
4723 if (lpmii->fMask & MIIM_SUBMENU) {
4724 menu->hSubMenu = lpmii->hSubMenu;
4725 if (menu->hSubMenu) {
4726 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4727 if (subMenu) {
4728 subMenu->wFlags |= MF_POPUP;
4729 menu->fType |= MF_POPUP;
4731 else {
4732 SetLastError( ERROR_INVALID_PARAMETER);
4733 return FALSE;
4736 else
4737 menu->fType &= ~MF_POPUP;
4740 if (lpmii->fMask & MIIM_CHECKMARKS)
4742 menu->hCheckBit = lpmii->hbmpChecked;
4743 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4745 if (lpmii->fMask & MIIM_DATA)
4746 menu->dwItemData = lpmii->dwItemData;
4748 if (lpmii->fMask & MIIM_BITMAP)
4749 menu->hbmpItem = lpmii->hbmpItem;
4751 if( !menu->text && !(menu->fType & MFT_OWNERDRAW) && !menu->hbmpItem)
4752 menu->fType |= MFT_SEPARATOR;
4754 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4755 return TRUE;
4758 /**********************************************************************
4759 * SetMenuItemInfoA (USER32.@)
4761 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4762 const MENUITEMINFOA *lpmii)
4764 MENUITEMINFOA mii;
4766 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4768 if( lpmii->cbSize != sizeof( mii) &&
4769 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4770 SetLastError( ERROR_INVALID_PARAMETER);
4771 return FALSE;
4773 memcpy( &mii, lpmii, lpmii->cbSize);
4774 if( lpmii->cbSize != sizeof( mii)) {
4775 mii.cbSize = sizeof( mii);
4776 mii.hbmpItem = NULL;
4778 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4779 (const MENUITEMINFOW *)&mii, FALSE);
4782 /**********************************************************************
4783 * SetMenuItemInfoW (USER32.@)
4785 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4786 const MENUITEMINFOW *lpmii)
4788 MENUITEMINFOW mii;
4790 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4792 if( lpmii->cbSize != sizeof( mii) &&
4793 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4794 SetLastError( ERROR_INVALID_PARAMETER);
4795 return FALSE;
4797 memcpy( &mii, lpmii, lpmii->cbSize);
4798 if( lpmii->cbSize != sizeof( mii)) {
4799 mii.cbSize = sizeof( mii);
4800 mii.hbmpItem = NULL;
4802 return SetMenuItemInfo_common(MENU_FindItem(&hmenu,
4803 &item, bypos? MF_BYPOSITION : 0), &mii, TRUE);
4806 /**********************************************************************
4807 * SetMenuDefaultItem (USER32.@)
4810 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4812 UINT i;
4813 POPUPMENU *menu;
4814 MENUITEM *item;
4816 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4818 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4820 /* reset all default-item flags */
4821 item = menu->items;
4822 for (i = 0; i < menu->nItems; i++, item++)
4824 item->fState &= ~MFS_DEFAULT;
4827 /* no default item */
4828 if ( -1 == uItem)
4830 return TRUE;
4833 item = menu->items;
4834 if ( bypos )
4836 if ( uItem >= menu->nItems ) return FALSE;
4837 item[uItem].fState |= MFS_DEFAULT;
4838 return TRUE;
4840 else
4842 for (i = 0; i < menu->nItems; i++, item++)
4844 if (item->wID == uItem)
4846 item->fState |= MFS_DEFAULT;
4847 return TRUE;
4852 return FALSE;
4855 /**********************************************************************
4856 * GetMenuDefaultItem (USER32.@)
4858 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4860 POPUPMENU *menu;
4861 MENUITEM * item;
4862 UINT i = 0;
4864 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
4866 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4868 /* find default item */
4869 item = menu->items;
4871 /* empty menu */
4872 if (! item) return -1;
4874 while ( !( item->fState & MFS_DEFAULT ) )
4876 i++; item++;
4877 if (i >= menu->nItems ) return -1;
4880 /* default: don't return disabled items */
4881 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4883 /* search rekursiv when needed */
4884 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4886 UINT ret;
4887 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4888 if ( -1 != ret ) return ret;
4890 /* when item not found in submenu, return the popup item */
4892 return ( bypos ) ? i : item->wID;
4897 /**********************************************************************
4898 * InsertMenuItemA (USER32.@)
4900 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4901 const MENUITEMINFOA *lpmii)
4903 MENUITEM *item;
4904 MENUITEMINFOA mii;
4906 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
4908 if( lpmii->cbSize != sizeof( mii) &&
4909 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4910 SetLastError( ERROR_INVALID_PARAMETER);
4911 return FALSE;
4913 memcpy( &mii, lpmii, lpmii->cbSize);
4914 if( lpmii->cbSize != sizeof( mii)) {
4915 mii.cbSize = sizeof( mii);
4916 mii.hbmpItem = NULL;
4919 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4920 return SetMenuItemInfo_common(item, (const MENUITEMINFOW *)&mii, FALSE);
4924 /**********************************************************************
4925 * InsertMenuItemW (USER32.@)
4927 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4928 const MENUITEMINFOW *lpmii)
4930 MENUITEM *item;
4931 MENUITEMINFOW mii;
4933 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
4935 if( lpmii->cbSize != sizeof( mii) &&
4936 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4937 SetLastError( ERROR_INVALID_PARAMETER);
4938 return FALSE;
4940 memcpy( &mii, lpmii, lpmii->cbSize);
4941 if( lpmii->cbSize != sizeof( mii)) {
4942 mii.cbSize = sizeof( mii);
4943 mii.hbmpItem = NULL;
4946 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4947 return SetMenuItemInfo_common(item, &mii, TRUE);
4950 /**********************************************************************
4951 * CheckMenuRadioItem (USER32.@)
4954 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4955 UINT first, UINT last, UINT check,
4956 UINT bypos)
4958 BOOL done = FALSE;
4959 UINT i;
4960 MENUITEM *mi_first = NULL, *mi_check;
4961 HMENU m_first, m_check;
4963 for (i = first; i <= last; i++)
4965 UINT pos = i;
4967 if (!mi_first)
4969 m_first = hMenu;
4970 mi_first = MENU_FindItem(&m_first, &pos, bypos);
4971 if (!mi_first) continue;
4972 mi_check = mi_first;
4973 m_check = m_first;
4975 else
4977 m_check = hMenu;
4978 mi_check = MENU_FindItem(&m_check, &pos, bypos);
4979 if (!mi_check) continue;
4982 if (m_first != m_check) continue;
4983 if (mi_check->fType == MFT_SEPARATOR) continue;
4985 if (i == check)
4987 mi_check->fType |= MFT_RADIOCHECK;
4988 mi_check->fState |= MFS_CHECKED;
4989 done = TRUE;
4991 else
4993 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
4994 mi_check->fState &= ~MFS_CHECKED;
4998 return done;
5002 /**********************************************************************
5003 * GetMenuItemRect (USER32.@)
5005 * ATTENTION: Here, the returned values in rect are the screen
5006 * coordinates of the item just like if the menu was
5007 * always on the upper left side of the application.
5010 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
5011 LPRECT rect)
5013 POPUPMENU *itemMenu;
5014 MENUITEM *item;
5015 HWND referenceHwnd;
5017 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
5019 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
5020 referenceHwnd = hwnd;
5022 if(!hwnd)
5024 itemMenu = MENU_GetMenu(hMenu);
5025 if (itemMenu == NULL)
5026 return FALSE;
5028 if(itemMenu->hWnd == 0)
5029 return FALSE;
5030 referenceHwnd = itemMenu->hWnd;
5033 if ((rect == NULL) || (item == NULL))
5034 return FALSE;
5036 *rect = item->rect;
5038 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
5040 return TRUE;
5043 /**********************************************************************
5044 * SetMenuInfo (USER32.@)
5046 * FIXME
5047 * actually use the items to draw the menu
5048 * (recalculate and/or redraw)
5050 static BOOL menu_SetMenuInfo( HMENU hMenu, LPCMENUINFO lpmi)
5052 POPUPMENU *menu;
5053 if( !(menu = MENU_GetMenu(hMenu))) return FALSE;
5055 if (lpmi->fMask & MIM_BACKGROUND)
5056 menu->hbrBack = lpmi->hbrBack;
5058 if (lpmi->fMask & MIM_HELPID)
5059 menu->dwContextHelpID = lpmi->dwContextHelpID;
5061 if (lpmi->fMask & MIM_MAXHEIGHT)
5062 menu->cyMax = lpmi->cyMax;
5064 if (lpmi->fMask & MIM_MENUDATA)
5065 menu->dwMenuData = lpmi->dwMenuData;
5067 if (lpmi->fMask & MIM_STYLE)
5068 menu->dwStyle = lpmi->dwStyle;
5070 if( lpmi->fMask & MIM_APPLYTOSUBMENUS) {
5071 int i;
5072 MENUITEM *item = menu->items;
5073 for( i = menu->nItems; i; i--, item++)
5074 if( item->fType & MF_POPUP)
5075 menu_SetMenuInfo( item->hSubMenu, lpmi);
5077 return TRUE;
5080 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
5082 TRACE("(%p %p)\n", hMenu, lpmi);
5083 if( lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu_SetMenuInfo( hMenu, lpmi))) {
5084 if( lpmi->fMask & MIM_STYLE) {
5085 if (lpmi->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
5086 if (lpmi->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
5087 if (lpmi->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
5089 return TRUE;
5091 SetLastError( ERROR_INVALID_PARAMETER);
5092 return FALSE;
5095 /**********************************************************************
5096 * GetMenuInfo (USER32.@)
5098 * NOTES
5099 * win98/NT5.0
5102 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
5103 { POPUPMENU *menu;
5105 TRACE("(%p %p)\n", hMenu, lpmi);
5107 if (lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
5110 if (lpmi->fMask & MIM_BACKGROUND)
5111 lpmi->hbrBack = menu->hbrBack;
5113 if (lpmi->fMask & MIM_HELPID)
5114 lpmi->dwContextHelpID = menu->dwContextHelpID;
5116 if (lpmi->fMask & MIM_MAXHEIGHT)
5117 lpmi->cyMax = menu->cyMax;
5119 if (lpmi->fMask & MIM_MENUDATA)
5120 lpmi->dwMenuData = menu->dwMenuData;
5122 if (lpmi->fMask & MIM_STYLE)
5123 lpmi->dwStyle = menu->dwStyle;
5125 return TRUE;
5127 SetLastError( ERROR_INVALID_PARAMETER);
5128 return FALSE;
5132 /**********************************************************************
5133 * SetMenuContextHelpId (USER32.@)
5135 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
5137 LPPOPUPMENU menu;
5139 TRACE("(%p 0x%08x)\n", hMenu, dwContextHelpID);
5141 if ((menu = MENU_GetMenu(hMenu)))
5143 menu->dwContextHelpID = dwContextHelpID;
5144 return TRUE;
5146 return FALSE;
5150 /**********************************************************************
5151 * GetMenuContextHelpId (USER32.@)
5153 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
5155 LPPOPUPMENU menu;
5157 TRACE("(%p)\n", hMenu);
5159 if ((menu = MENU_GetMenu(hMenu)))
5161 return menu->dwContextHelpID;
5163 return 0;
5166 /**********************************************************************
5167 * MenuItemFromPoint (USER32.@)
5169 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
5171 POPUPMENU *menu = MENU_GetMenu(hMenu);
5172 UINT pos;
5174 /*FIXME: Do we have to handle hWnd here? */
5175 if (!menu) return -1;
5176 if (!MENU_FindItemByCoords(menu, ptScreen, &pos)) return -1;
5177 return pos;
5181 /**********************************************************************
5182 * translate_accelerator
5184 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
5185 BYTE fVirt, WORD key, WORD cmd )
5187 INT mask = 0;
5188 UINT mesg = 0;
5190 if (wParam != key) return FALSE;
5192 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
5193 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
5194 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
5196 if (message == WM_CHAR || message == WM_SYSCHAR)
5198 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
5200 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam) & 0xff);
5201 goto found;
5204 else
5206 if(fVirt & FVIRTKEY)
5208 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
5209 wParam, 0xff & HIWORD(lParam));
5211 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
5212 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
5214 else
5216 if (!(lParam & 0x01000000)) /* no special_key */
5218 if ((fVirt & FALT) && (lParam & 0x20000000))
5219 { /* ^^ ALT pressed */
5220 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(wParam) & 0xff);
5221 goto found;
5226 return FALSE;
5228 found:
5229 if (message == WM_KEYUP || message == WM_SYSKEYUP)
5230 mesg = 1;
5231 else
5233 HMENU hMenu, hSubMenu, hSysMenu;
5234 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
5236 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
5237 hSysMenu = get_win_sys_menu( hWnd );
5239 /* find menu item and ask application to initialize it */
5240 /* 1. in the system menu */
5241 hSubMenu = hSysMenu;
5242 nPos = cmd;
5243 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5245 if (GetCapture())
5246 mesg = 2;
5247 if (!IsWindowEnabled(hWnd))
5248 mesg = 3;
5249 else
5251 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
5252 if(hSubMenu != hSysMenu)
5254 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
5255 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
5256 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
5258 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
5261 else /* 2. in the window's menu */
5263 hSubMenu = hMenu;
5264 nPos = cmd;
5265 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5267 if (GetCapture())
5268 mesg = 2;
5269 if (!IsWindowEnabled(hWnd))
5270 mesg = 3;
5271 else
5273 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
5274 if(hSubMenu != hMenu)
5276 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
5277 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
5278 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
5280 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
5285 if (mesg == 0)
5287 if (uSysStat != (UINT)-1)
5289 if (uSysStat & (MF_DISABLED|MF_GRAYED))
5290 mesg=4;
5291 else
5292 mesg=WM_SYSCOMMAND;
5294 else
5296 if (uStat != (UINT)-1)
5298 if (IsIconic(hWnd))
5299 mesg=5;
5300 else
5302 if (uStat & (MF_DISABLED|MF_GRAYED))
5303 mesg=6;
5304 else
5305 mesg=WM_COMMAND;
5308 else
5309 mesg=WM_COMMAND;
5314 if( mesg==WM_COMMAND )
5316 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
5317 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
5319 else if( mesg==WM_SYSCOMMAND )
5321 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
5322 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
5324 else
5326 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
5327 * #0: unknown (please report!)
5328 * #1: for WM_KEYUP,WM_SYSKEYUP
5329 * #2: mouse is captured
5330 * #3: window is disabled
5331 * #4: it's a disabled system menu option
5332 * #5: it's a menu option, but window is iconic
5333 * #6: it's a menu option, but disabled
5335 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
5336 if(mesg==0)
5337 ERR_(accel)(" unknown reason - please report!\n");
5339 return TRUE;
5342 /**********************************************************************
5343 * TranslateAcceleratorA (USER32.@)
5344 * TranslateAccelerator (USER32.@)
5346 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
5348 /* YES, Accel16! */
5349 LPACCEL16 lpAccelTbl;
5350 int i;
5351 WPARAM wParam;
5353 if (!hWnd || !msg) return 0;
5355 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
5357 WARN_(accel)("invalid accel handle=%p\n", hAccel);
5358 return 0;
5361 wParam = msg->wParam;
5363 switch (msg->message)
5365 case WM_KEYDOWN:
5366 case WM_SYSKEYDOWN:
5367 break;
5369 case WM_CHAR:
5370 case WM_SYSCHAR:
5372 char ch = LOWORD(wParam);
5373 WCHAR wch;
5374 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
5375 wParam = MAKEWPARAM(wch, HIWORD(wParam));
5377 break;
5379 default:
5380 return 0;
5383 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5384 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5385 i = 0;
5388 if (translate_accelerator( hWnd, msg->message, wParam, msg->lParam,
5389 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
5390 return 1;
5391 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
5393 return 0;
5396 /**********************************************************************
5397 * TranslateAcceleratorW (USER32.@)
5399 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
5401 /* YES, Accel16! */
5402 LPACCEL16 lpAccelTbl;
5403 int i;
5405 if (!hWnd || !msg) return 0;
5407 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
5409 WARN_(accel)("invalid accel handle=%p\n", hAccel);
5410 return 0;
5413 switch (msg->message)
5415 case WM_KEYDOWN:
5416 case WM_SYSKEYDOWN:
5417 case WM_CHAR:
5418 case WM_SYSCHAR:
5419 break;
5421 default:
5422 return 0;
5425 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5426 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5427 i = 0;
5430 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5431 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
5432 return 1;
5433 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
5435 return 0;