- when sending the WM_DRAWITEM message in case of a menu item with
[wine/multimedia.git] / dlls / user / menu.c
blobb2ddd5b829de7587f99c98bf77e944d2d214f6ac
1 /*
2 * Menu functions
4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
7 * Copyright 2005 Maxime Bellengé
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Note: the style MF_MOUSESELECT is used to mark popup items that
26 * have been selected, i.e. their popup menu is currently displayed.
27 * This is probably not the meaning this style has in MS-Windows.
29 * TODO:
30 * implements styles :
31 * - MNS_AUTODISMISS
32 * - MNS_DRAGDROP
33 * - MNS_MODELESS
34 * - MNS_NOCHECK
35 * - MNS_NOTIFYBYPOS
38 #include "config.h"
39 #include "wine/port.h"
41 #include <stdarg.h>
42 #include <string.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wingdi.h"
47 #include "winnls.h"
48 #include "wine/winbase16.h"
49 #include "wine/winuser16.h"
50 #include "wownt32.h"
51 #include "wine/server.h"
52 #include "wine/unicode.h"
53 #include "win.h"
54 #include "controls.h"
55 #include "user_private.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(menu);
59 WINE_DECLARE_DEBUG_CHANNEL(accel);
61 /* internal popup menu window messages */
63 #define MM_SETMENUHANDLE (WM_USER + 0)
64 #define MM_GETMENUHANDLE (WM_USER + 1)
66 /* Menu item structure */
67 typedef struct {
68 /* ----------- MENUITEMINFO Stuff ----------- */
69 UINT fType; /* Item type. */
70 UINT fState; /* Item state. */
71 UINT_PTR wID; /* Item id. */
72 HMENU hSubMenu; /* Pop-up menu. */
73 HBITMAP hCheckBit; /* Bitmap when checked. */
74 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
75 LPWSTR text; /* Item text or bitmap handle. */
76 DWORD dwItemData; /* Application defined. */
77 DWORD dwTypeData; /* depends on fMask */
78 HBITMAP hbmpItem; /* bitmap in win98 style menus */
79 /* ----------- Wine stuff ----------- */
80 RECT rect; /* Item area (relative to menu window) */
81 UINT xTab; /* X position of text after Tab */
82 } MENUITEM;
84 /* Popup menu structure */
85 typedef struct {
86 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
87 WORD wMagic; /* Magic number */
88 WORD Width; /* Width of the whole menu */
89 WORD Height; /* Height of the whole menu */
90 UINT nItems; /* Number of items in the menu */
91 HWND hWnd; /* Window containing the menu */
92 MENUITEM *items; /* Array of menu items */
93 UINT FocusedItem; /* Currently focused item */
94 HWND hwndOwner; /* window receiving the messages for ownerdraw */
95 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
96 /* ------------ MENUINFO members ------ */
97 DWORD dwStyle; /* Extended mennu style */
98 UINT cyMax; /* max hight of the whole menu, 0 is screen hight */
99 HBRUSH hbrBack; /* brush for menu background */
100 DWORD dwContextHelpID;
101 DWORD dwMenuData; /* application defined value */
102 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
103 SIZE maxBmpSize; /* Maximum size of the bitmap items in MIIM_BITMAP state */
104 } POPUPMENU, *LPPOPUPMENU;
106 /* internal flags for menu tracking */
108 #define TF_ENDMENU 0x0001
109 #define TF_SUSPENDPOPUP 0x0002
110 #define TF_SKIPREMOVE 0x0004
112 typedef struct
114 UINT trackFlags;
115 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
116 HMENU hTopMenu; /* initial menu */
117 HWND hOwnerWnd; /* where notifications are sent */
118 POINT pt;
119 } MTRACKER;
121 #define MENU_MAGIC 0x554d /* 'MU' */
123 #define ITEM_PREV -1
124 #define ITEM_NEXT 1
126 /* Internal MENU_TrackMenu() flags */
127 #define TPM_INTERNAL 0xF0000000
128 #define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
129 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
130 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
132 /* popup menu shade thickness */
133 #define POPUP_XSHADE 4
134 #define POPUP_YSHADE 4
136 /* Space between 2 menu bar items */
137 #define MENU_BAR_ITEMS_SPACE 12
139 /* Minimum width of a tab character */
140 #define MENU_TAB_SPACE 8
142 /* Height of a separator item */
143 #define SEPARATOR_HEIGHT 5
145 /* Space between 2 columns */
146 #define MENU_COL_SPACE 4
148 /* (other menu->FocusedItem values give the position of the focused item) */
149 #define NO_SELECTED_ITEM 0xffff
151 #define MENU_ITEM_TYPE(flags) \
152 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
154 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
155 #define IS_BITMAP_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_BITMAP)
156 #define IS_MAGIC_ITEM(text) (LOWORD((int)text)<12)
158 #define IS_SYSTEM_MENU(menu) \
159 (!((menu)->wFlags & MF_POPUP) && ((menu)->wFlags & MF_SYSMENU))
161 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
162 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
163 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
164 MF_POPUP | MF_SYSMENU | MF_HELP)
165 #define STATE_MASK (~TYPE_MASK)
167 #define WIN_ALLOWED_MENU(style) ((style & (WS_CHILD | WS_POPUP)) != WS_CHILD)
169 /* Dimension of the menu bitmaps */
170 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
172 static HBITMAP hStdMnArrow = 0;
173 static HBITMAP hBmpSysMenu = 0;
175 static HBRUSH hShadeBrush = 0;
176 static HFONT hMenuFont = 0;
177 static HFONT hMenuFontBold = 0;
178 static SIZE menucharsize;
179 static UINT ODitemheight; /* default owner drawn item height */
181 static HMENU MENU_DefSysPopup = 0; /* Default system menu popup */
183 /* Use global popup window because there's no way 2 menus can
184 * be tracked at the same time. */
185 static HWND top_popup;
187 /* Flag set by EndMenu() to force an exit from menu tracking */
188 static BOOL fEndMenu = FALSE;
190 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
192 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont);
194 /*********************************************************************
195 * menu class descriptor
197 const struct builtin_class_descr MENU_builtin_class =
199 POPUPMENU_CLASS_ATOMA, /* name */
200 CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, /* style */
201 NULL, /* procA (winproc is Unicode only) */
202 PopupMenuWndProc, /* procW */
203 sizeof(HMENU), /* extra */
204 IDC_ARROW, /* cursor */
205 (HBRUSH)(COLOR_MENU+1) /* brush */
209 /***********************************************************************
210 * debug_print_menuitem
212 * Print a menuitem in readable form.
215 #define debug_print_menuitem(pre, mp, post) \
216 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
218 #define MENUOUT(text) \
219 DPRINTF("%s%s", (count++ ? "," : ""), (text))
221 #define MENUFLAG(bit,text) \
222 do { \
223 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
224 } while (0)
226 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
227 const char *postfix)
229 TRACE("%s ", prefix);
230 if (mp) {
231 UINT flags = mp->fType;
232 int type = MENU_ITEM_TYPE(flags);
233 DPRINTF( "{ ID=0x%x", mp->wID);
234 if (flags & MF_POPUP)
235 DPRINTF( ", Sub=%p", mp->hSubMenu);
236 if (flags) {
237 int count = 0;
238 DPRINTF( ", Type=");
239 if (type == MFT_STRING)
240 /* Nothing */ ;
241 else if (type == MFT_SEPARATOR)
242 MENUOUT("sep");
243 else if (type == MFT_OWNERDRAW)
244 MENUOUT("own");
245 else if (type == MFT_BITMAP)
246 MENUOUT("bit");
247 else
248 MENUOUT("???");
249 flags -= type;
251 MENUFLAG(MF_POPUP, "pop");
252 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
253 MENUFLAG(MFT_MENUBREAK, "brk");
254 MENUFLAG(MFT_RADIOCHECK, "radio");
255 MENUFLAG(MFT_RIGHTORDER, "rorder");
256 MENUFLAG(MF_SYSMENU, "sys");
257 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
259 if (flags)
260 DPRINTF( "+0x%x", flags);
262 flags = mp->fState;
263 if (flags) {
264 int count = 0;
265 DPRINTF( ", State=");
266 MENUFLAG(MFS_GRAYED, "grey");
267 MENUFLAG(MFS_DEFAULT, "default");
268 MENUFLAG(MFS_DISABLED, "dis");
269 MENUFLAG(MFS_CHECKED, "check");
270 MENUFLAG(MFS_HILITE, "hi");
271 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
272 MENUFLAG(MF_MOUSESELECT, "mouse");
273 if (flags)
274 DPRINTF( "+0x%x", flags);
276 if (mp->hCheckBit)
277 DPRINTF( ", Chk=%p", mp->hCheckBit);
278 if (mp->hUnCheckBit)
279 DPRINTF( ", Unc=%p", mp->hUnCheckBit);
281 if (type == MFT_STRING) {
282 if (mp->text)
283 DPRINTF( ", Text=%s", debugstr_w(mp->text));
284 else
285 DPRINTF( ", Text=Null");
286 } else if (mp->text == NULL)
287 /* Nothing */ ;
288 else
289 DPRINTF( ", Text=%p", mp->text);
290 if (mp->dwItemData)
291 DPRINTF( ", ItemData=0x%08lx", mp->dwItemData);
292 DPRINTF( " }");
293 } else {
294 DPRINTF( "NULL");
297 DPRINTF(" %s\n", postfix);
300 #undef MENUOUT
301 #undef MENUFLAG
304 /***********************************************************************
305 * MENU_GetMenu
307 * Validate the given menu handle and returns the menu structure pointer.
309 static POPUPMENU *MENU_GetMenu(HMENU hMenu)
311 POPUPMENU *menu = USER_HEAP_LIN_ADDR(hMenu);
312 if (!menu || menu->wMagic != MENU_MAGIC)
314 WARN("invalid menu handle=%p, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
315 menu = NULL;
317 return menu;
320 /***********************************************************************
321 * get_win_sys_menu
323 * Get the system menu of a window
325 static HMENU get_win_sys_menu( HWND hwnd )
327 HMENU ret = 0;
328 WND *win = WIN_GetPtr( hwnd );
329 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
331 ret = win->hSysMenu;
332 WIN_ReleasePtr( win );
334 return ret;
337 /***********************************************************************
338 * MENU_CopySysPopup
340 * Return the default system menu.
342 static HMENU MENU_CopySysPopup(void)
344 static const WCHAR sysmenuW[] = {'S','Y','S','M','E','N','U',0};
345 HMENU hMenu = LoadMenuW(user32_module, sysmenuW);
347 if( hMenu ) {
348 POPUPMENU* menu = MENU_GetMenu(hMenu);
349 menu->wFlags |= MF_SYSMENU | MF_POPUP;
350 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
352 else
353 ERR("Unable to load default system menu\n" );
355 TRACE("returning %p.\n", hMenu );
357 return hMenu;
361 /**********************************************************************
362 * MENU_GetSysMenu
364 * Create a copy of the system menu. System menu in Windows is
365 * a special menu bar with the single entry - system menu popup.
366 * This popup is presented to the outside world as a "system menu".
367 * However, the real system menu handle is sometimes seen in the
368 * WM_MENUSELECT parameters (and Word 6 likes it this way).
370 HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
372 HMENU hMenu;
374 TRACE("loading system menu, hWnd %p, hPopupMenu %p\n", hWnd, hPopupMenu);
375 if ((hMenu = CreateMenu()))
377 POPUPMENU *menu = MENU_GetMenu(hMenu);
378 menu->wFlags = MF_SYSMENU;
379 menu->hWnd = WIN_GetFullHandle( hWnd );
380 TRACE("hWnd %p (hMenu %p)\n", menu->hWnd, hMenu);
382 if (hPopupMenu == (HMENU)(-1))
383 hPopupMenu = MENU_CopySysPopup();
384 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
386 if (hPopupMenu)
388 if (GetClassLongW(hWnd, GCL_STYLE) & CS_NOCLOSE)
389 DeleteMenu(hPopupMenu, SC_CLOSE, MF_BYCOMMAND);
391 InsertMenuW( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION,
392 (UINT_PTR)hPopupMenu, NULL );
394 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
395 menu->items[0].fState = 0;
396 if ((menu = MENU_GetMenu(hPopupMenu))) menu->wFlags |= MF_SYSMENU;
398 TRACE("hMenu=%p (hPopup %p)\n", hMenu, hPopupMenu );
399 return hMenu;
401 DestroyMenu( hMenu );
403 ERR("failed to load system menu!\n");
404 return 0;
408 /***********************************************************************
409 * MENU_Init
411 * Menus initialisation.
413 BOOL MENU_Init(void)
415 HBITMAP hBitmap;
416 NONCLIENTMETRICSW ncm;
418 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
419 0x55, 0, 0xAA, 0,
420 0x55, 0, 0xAA, 0,
421 0x55, 0, 0xAA, 0 };
423 /* Load menu bitmaps */
424 hStdMnArrow = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_MNARROW));
425 /* Load system buttons bitmaps */
426 hBmpSysMenu = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE));
428 if (hStdMnArrow)
430 BITMAP bm;
431 GetObjectW( hStdMnArrow, sizeof(bm), &bm );
432 arrow_bitmap_width = bm.bmWidth;
433 arrow_bitmap_height = bm.bmHeight;
434 } else
435 return FALSE;
437 if (! (hBitmap = CreateBitmap( 8, 8, 1, 1, shade_bits)))
438 return FALSE;
440 if(!(hShadeBrush = CreatePatternBrush( hBitmap )))
441 return FALSE;
443 DeleteObject( hBitmap );
444 if (!(MENU_DefSysPopup = MENU_CopySysPopup()))
445 return FALSE;
447 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
448 if (!(SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0)))
449 return FALSE;
451 if (!(hMenuFont = CreateFontIndirectW( &ncm.lfMenuFont )))
452 return FALSE;
454 ncm.lfMenuFont.lfWeight += 300;
455 if ( ncm.lfMenuFont.lfWeight > 1000)
456 ncm.lfMenuFont.lfWeight = 1000;
458 if (!(hMenuFontBold = CreateFontIndirectW( &ncm.lfMenuFont )))
459 return FALSE;
461 return TRUE;
464 /***********************************************************************
465 * MENU_InitSysMenuPopup
467 * Grey the appropriate items in System menu.
469 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
471 BOOL gray;
473 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
474 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
475 gray = ((style & WS_MAXIMIZE) != 0);
476 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
477 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
478 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
479 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
480 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
481 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
482 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
483 gray = (clsStyle & CS_NOCLOSE) != 0;
485 /* The menu item must keep its state if it's disabled */
486 if(gray)
487 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
491 /******************************************************************************
493 * UINT MENU_GetStartOfNextColumn(
494 * HMENU hMenu )
496 *****************************************************************************/
498 static UINT MENU_GetStartOfNextColumn(
499 HMENU hMenu )
501 POPUPMENU *menu = MENU_GetMenu(hMenu);
502 UINT i;
504 if(!menu)
505 return NO_SELECTED_ITEM;
507 i = menu->FocusedItem + 1;
508 if( i == NO_SELECTED_ITEM )
509 return i;
511 for( ; i < menu->nItems; ++i ) {
512 if (menu->items[i].fType & MF_MENUBARBREAK)
513 return i;
516 return NO_SELECTED_ITEM;
520 /******************************************************************************
522 * UINT MENU_GetStartOfPrevColumn(
523 * HMENU hMenu )
525 *****************************************************************************/
527 static UINT MENU_GetStartOfPrevColumn(
528 HMENU hMenu )
530 POPUPMENU *menu = MENU_GetMenu(hMenu);
531 UINT i;
533 if( !menu )
534 return NO_SELECTED_ITEM;
536 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
537 return NO_SELECTED_ITEM;
539 /* Find the start of the column */
541 for(i = menu->FocusedItem; i != 0 &&
542 !(menu->items[i].fType & MF_MENUBARBREAK);
543 --i); /* empty */
545 if(i == 0)
546 return NO_SELECTED_ITEM;
548 for(--i; i != 0; --i) {
549 if (menu->items[i].fType & MF_MENUBARBREAK)
550 break;
553 TRACE("ret %d.\n", i );
555 return i;
560 /***********************************************************************
561 * MENU_FindItem
563 * Find a menu item. Return a pointer on the item, and modifies *hmenu
564 * in case the item was in a sub-menu.
566 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
568 POPUPMENU *menu;
569 UINT i;
571 if ((*hmenu == (HMENU)0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
572 if (wFlags & MF_BYPOSITION)
574 if (*nPos >= menu->nItems) return NULL;
575 return &menu->items[*nPos];
577 else
579 MENUITEM *item = menu->items;
580 for (i = 0; i < menu->nItems; i++, item++)
582 if (item->wID == *nPos)
584 *nPos = i;
585 return item;
587 else if (item->fType & MF_POPUP)
589 HMENU hsubmenu = item->hSubMenu;
590 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
591 if (subitem)
593 *hmenu = hsubmenu;
594 return subitem;
599 return NULL;
602 /***********************************************************************
603 * MENU_FindSubMenu
605 * Find a Sub menu. Return the position of the submenu, and modifies
606 * *hmenu in case it is found in another sub-menu.
607 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
609 UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
611 POPUPMENU *menu;
612 UINT i;
613 MENUITEM *item;
614 if (((*hmenu)==(HMENU)0xffff) ||
615 (!(menu = MENU_GetMenu(*hmenu))))
616 return NO_SELECTED_ITEM;
617 item = menu->items;
618 for (i = 0; i < menu->nItems; i++, item++) {
619 if(!(item->fType & MF_POPUP)) continue;
620 if (item->hSubMenu == hSubTarget) {
621 return i;
623 else {
624 HMENU hsubmenu = item->hSubMenu;
625 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
626 if (pos != NO_SELECTED_ITEM) {
627 *hmenu = hsubmenu;
628 return pos;
632 return NO_SELECTED_ITEM;
635 /***********************************************************************
636 * MENU_FreeItemData
638 static void MENU_FreeItemData( MENUITEM* item )
640 /* delete text */
641 if (IS_STRING_ITEM(item->fType) && item->text)
642 HeapFree( GetProcessHeap(), 0, item->text );
645 /***********************************************************************
646 * MENU_FindItemByCoords
648 * Find the item at the specified coordinates (screen coords). Does
649 * not work for child windows and therefore should not be called for
650 * an arbitrary system menu.
652 static MENUITEM *MENU_FindItemByCoords( const POPUPMENU *menu,
653 POINT pt, UINT *pos )
655 MENUITEM *item;
656 UINT i;
657 RECT wrect;
659 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
660 pt.x -= wrect.left;pt.y -= wrect.top;
661 item = menu->items;
662 for (i = 0; i < menu->nItems; i++, item++)
664 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
665 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
667 if (pos) *pos = i;
668 return item;
671 return NULL;
675 /***********************************************************************
676 * MENU_FindItemByKey
678 * Find the menu item selected by a key press.
679 * Return item id, -1 if none, -2 if we should close the menu.
681 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
682 WCHAR key, BOOL forceMenuChar )
684 TRACE("\tlooking for '%c' (0x%02x) in [%p]\n", (char)key, key, hmenu );
686 if (!IsMenu( hmenu )) hmenu = GetSubMenu( get_win_sys_menu(hwndOwner), 0);
688 if (hmenu)
690 POPUPMENU *menu = MENU_GetMenu( hmenu );
691 MENUITEM *item = menu->items;
692 LRESULT menuchar;
694 if( !forceMenuChar )
696 UINT i;
698 for (i = 0; i < menu->nItems; i++, item++)
700 if (IS_STRING_ITEM(item->fType) && item->text)
702 WCHAR *p = item->text - 2;
705 p = strchrW (p + 2, '&');
707 while (p != NULL && p [1] == '&');
708 if (p && (toupperW(p[1]) == toupperW(key))) return i;
712 menuchar = SendMessageW( hwndOwner, WM_MENUCHAR,
713 MAKEWPARAM( key, menu->wFlags ), (LPARAM)hmenu );
714 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
715 if (HIWORD(menuchar) == 1) return (UINT)(-2);
717 return (UINT)(-1);
721 /***********************************************************************
722 * MENU_GetBitmapItemSize
724 * Get the size of a bitmap item.
726 static void MENU_GetBitmapItemSize( UINT id, DWORD data, SIZE *size )
728 BITMAP bm;
729 HBITMAP bmp = (HBITMAP)id;
731 size->cx = size->cy = 0;
733 /* check if there is a magic menu item associated with this item */
734 if (id && IS_MAGIC_ITEM( id ))
736 switch(LOWORD(id))
738 case (INT_PTR)HBMMENU_SYSTEM:
739 if (data)
741 bmp = (HBITMAP)data;
742 break;
744 /* fall through */
745 case (INT_PTR)HBMMENU_MBAR_RESTORE:
746 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
747 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
748 case (INT_PTR)HBMMENU_MBAR_CLOSE:
749 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
750 size->cx = GetSystemMetrics( SM_CYMENU ) - 4;
751 size->cy = size->cx;
752 return;
753 case (INT_PTR)HBMMENU_CALLBACK:
754 case (INT_PTR)HBMMENU_POPUP_CLOSE:
755 case (INT_PTR)HBMMENU_POPUP_RESTORE:
756 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
757 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
758 default:
759 FIXME("Magic 0x%08x not implemented\n", id);
760 return;
763 if (GetObjectW(bmp, sizeof(bm), &bm ))
765 size->cx = bm.bmWidth;
766 size->cy = bm.bmHeight;
770 /***********************************************************************
771 * MENU_DrawBitmapItem
773 * Draw a bitmap item.
774 * drawhbmbitmap : True to draw the hbmbitmap(MIIM_BITMAP)/False to draw the MF_BITMAP
776 static void MENU_DrawBitmapItem( HDC hdc, MENUITEM *lpitem, const RECT *rect, BOOL menuBar, BOOL drawhbmbitmap )
778 BITMAP bm;
779 DWORD rop;
780 HDC hdcMem;
781 HBITMAP bmp = (HBITMAP)lpitem->text;
782 int w = rect->right - rect->left;
783 int h = rect->bottom - rect->top;
784 int bmp_xoffset = 0;
785 int left, top;
786 HBITMAP hbmToDraw = (drawhbmbitmap)?lpitem->hbmpItem:(HBITMAP)lpitem->text;
788 /* Check if there is a magic menu item associated with this item */
789 if (hbmToDraw && IS_MAGIC_ITEM(hbmToDraw))
791 UINT flags = 0;
792 RECT r;
794 switch(LOWORD(hbmToDraw))
796 case (INT_PTR)HBMMENU_SYSTEM:
797 if (lpitem->dwItemData)
799 bmp = (HBITMAP)lpitem->dwItemData;
800 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
802 else
804 bmp = hBmpSysMenu;
805 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
806 /* only use right half of the bitmap */
807 bmp_xoffset = bm.bmWidth / 2;
808 bm.bmWidth -= bmp_xoffset;
810 goto got_bitmap;
811 case (INT_PTR)HBMMENU_MBAR_RESTORE:
812 flags = DFCS_CAPTIONRESTORE;
813 break;
814 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
815 flags = DFCS_CAPTIONMIN;
816 break;
817 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
818 flags = DFCS_CAPTIONMIN | DFCS_INACTIVE;
819 break;
820 case (INT_PTR)HBMMENU_MBAR_CLOSE:
821 flags = DFCS_CAPTIONCLOSE;
822 break;
823 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
824 flags = DFCS_CAPTIONCLOSE | DFCS_INACTIVE;
825 break;
826 case (INT_PTR)HBMMENU_CALLBACK:
827 case (INT_PTR)HBMMENU_POPUP_CLOSE:
828 case (INT_PTR)HBMMENU_POPUP_RESTORE:
829 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
830 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
831 default:
832 FIXME("Magic 0x%08x not implemented\n", LOWORD(hbmToDraw));
833 return;
835 r = *rect;
836 InflateRect( &r, -1, -1 );
837 if (lpitem->fState & MF_HILITE) flags |= DFCS_PUSHED;
838 DrawFrameControl( hdc, &r, DFC_CAPTION, flags );
839 return;
842 if (!bmp || !GetObjectW( bmp, sizeof(bm), &bm )) return;
844 got_bitmap:
845 hdcMem = CreateCompatibleDC( hdc );
846 SelectObject( hdcMem, bmp );
848 /* handle fontsize > bitmap_height */
849 top = (h>bm.bmHeight) ? rect->top+(h-bm.bmHeight)/2 : rect->top;
850 left=rect->left;
851 rop=((lpitem->fState & MF_HILITE) && !IS_MAGIC_ITEM(hbmToDraw)) ? NOTSRCCOPY : SRCCOPY;
852 if ((lpitem->fState & MF_HILITE) && IS_BITMAP_ITEM(lpitem->fType))
853 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
854 BitBlt( hdc, left, top, w, h, hdcMem, bmp_xoffset, 0, rop );
855 DeleteDC( hdcMem );
859 /***********************************************************************
860 * MENU_CalcItemSize
862 * Calculate the size of the menu item and store it in lpitem->rect.
864 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
865 INT orgX, INT orgY, BOOL menuBar, POPUPMENU* lppop )
867 WCHAR *p;
868 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
870 TRACE("dc=%p owner=%p (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
871 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
872 (menuBar ? " (MenuBar)" : ""));
874 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
876 if (lpitem->fType & MF_OWNERDRAW)
878 MEASUREITEMSTRUCT mis;
879 /* not done in Menu_Init: GetDialogBaseUnits() breaks there */
880 if( !menucharsize.cx ) {
881 DIALOG_GetCharSize( hdc, hMenuFont, &menucharsize );
882 /* Win95/98/ME will use menucharsize.cy here. Testing is possible
883 * but it is unlikely an application will depend on that */
884 ODitemheight = HIWORD( GetDialogBaseUnits());
886 mis.CtlType = ODT_MENU;
887 mis.CtlID = 0;
888 mis.itemID = lpitem->wID;
889 mis.itemData = (DWORD)lpitem->dwItemData;
890 mis.itemHeight = ODitemheight;
891 mis.itemWidth = 0;
892 SendMessageW( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
893 /* Tests reveal that Windows ( Win95 thru WinXP) adds twice the average
894 * width of a menufont character to the width of an owner-drawn menu.
896 lpitem->rect.right += mis.itemWidth + 2 * menucharsize.cx;
897 if (menuBar) {
898 /* under at least win95 you seem to be given a standard
899 height for the menu and the height value is ignored */
900 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENUSIZE);
901 } else
902 lpitem->rect.bottom += mis.itemHeight;
904 TRACE("id=%04x size=%ldx%ld\n",
905 lpitem->wID, lpitem->rect.right-lpitem->rect.left,
906 lpitem->rect.bottom-lpitem->rect.top);
907 return;
910 if (lpitem->fType & MF_SEPARATOR)
912 lpitem->rect.bottom += SEPARATOR_HEIGHT;
913 return;
916 if (!menuBar)
918 /* New style MIIM_BITMAP */
919 if (lpitem->hbmpItem)
921 if (lpitem->hbmpItem == HBMMENU_CALLBACK)
923 MEASUREITEMSTRUCT measItem;
924 measItem.CtlType = ODT_MENU;
925 measItem.CtlID = 0;
926 measItem.itemID = lpitem->wID;
927 measItem.itemWidth = lpitem->rect.right - lpitem->rect.left;
928 measItem.itemHeight = lpitem->rect.bottom - lpitem->rect.top;
929 measItem.itemData = lpitem->dwItemData;
931 SendMessageW( hwndOwner, WM_MEASUREITEM, lpitem->wID, (LPARAM)&measItem);
933 /* Keep the size of the bitmap in callback mode to be able to draw it correctly */
934 lppop->maxBmpSize.cx = max(lppop->maxBmpSize.cx, measItem.itemWidth - (lpitem->rect.right - lpitem->rect.left));
935 lppop->maxBmpSize.cy = max(lppop->maxBmpSize.cy, measItem.itemHeight - (lpitem->rect.bottom - lpitem->rect.top));
936 lpitem->rect.right = lpitem->rect.left + measItem.itemWidth;
937 } else {
938 SIZE size;
939 MENU_GetBitmapItemSize((UINT)lpitem->hbmpItem, lpitem->dwItemData, &size);
940 lppop->maxBmpSize.cx = max(lppop->maxBmpSize.cx, size.cx);
941 lppop->maxBmpSize.cy = max(lppop->maxBmpSize.cy, size.cy);
942 lpitem->rect.right += size.cx;
943 lpitem->rect.bottom += size.cy;
945 if (lppop->dwStyle & MNS_CHECKORBMP)
946 lpitem->rect.right += check_bitmap_width;
947 else
948 lpitem->rect.right += 2 * check_bitmap_width;
949 } else
950 lpitem->rect.right += 2 * check_bitmap_width;
951 if (lpitem->fType & MF_POPUP)
952 lpitem->rect.right += arrow_bitmap_width;
955 if (IS_BITMAP_ITEM(lpitem->fType))
957 SIZE size;
959 MENU_GetBitmapItemSize( (int)lpitem->text, lpitem->dwItemData, &size );
960 lpitem->rect.right += size.cx;
961 lpitem->rect.bottom += size.cy;
962 /* Leave space for the sunken border */
963 lpitem->rect.right += 2;
964 lpitem->rect.bottom += 2;
968 /* it must be a text item - unless it's the system menu */
969 if (!(lpitem->fType & MF_SYSMENU) && IS_STRING_ITEM( lpitem->fType ))
970 { SIZE size;
972 GetTextExtentPoint32W(hdc, lpitem->text, strlenW(lpitem->text), &size);
974 lpitem->rect.right += size.cx;
975 lpitem->rect.bottom += max(max(size.cy, GetSystemMetrics(SM_CYMENU)-1), lppop->maxBmpSize.cy);
976 lpitem->xTab = 0;
978 if (menuBar)
980 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
982 else if ((p = strchrW( lpitem->text, '\t' )) != NULL)
984 /* Item contains a tab (only meaningful in popup menus) */
985 GetTextExtentPoint32W(hdc, lpitem->text, (int)(p - lpitem->text) , &size);
986 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + size.cx;
987 lpitem->rect.right += MENU_TAB_SPACE;
989 else
991 if (strchrW( lpitem->text, '\b' ))
992 lpitem->rect.right += MENU_TAB_SPACE;
993 lpitem->xTab = lpitem->rect.right - check_bitmap_width
994 - arrow_bitmap_width;
997 TRACE("(%ld,%ld)-(%ld,%ld)\n", lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom);
1001 /***********************************************************************
1002 * MENU_PopupMenuCalcSize
1004 * Calculate the size of a popup menu.
1006 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
1008 MENUITEM *lpitem;
1009 HDC hdc;
1010 int start, i;
1011 int orgX, orgY, maxX, maxTab, maxTabWidth;
1013 lppop->Width = lppop->Height = 0;
1014 if (lppop->nItems == 0) return;
1015 hdc = GetDC( 0 );
1017 SelectObject( hdc, hMenuFont);
1019 start = 0;
1020 maxX = 2 + 1;
1022 lppop->maxBmpSize.cx = 0;
1023 lppop->maxBmpSize.cy = 0;
1025 while (start < lppop->nItems)
1027 lpitem = &lppop->items[start];
1028 orgX = maxX;
1029 if( lpitem->fType & MF_MENUBREAK)
1030 orgX += MENU_COL_SPACE;
1031 orgY = 3;
1033 maxTab = maxTabWidth = 0;
1034 /* Parse items until column break or end of menu */
1035 for (i = start; i < lppop->nItems; i++, lpitem++)
1037 if ((i != start) &&
1038 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1040 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE, lppop );
1042 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
1043 maxX = max( maxX, lpitem->rect.right );
1044 orgY = lpitem->rect.bottom;
1045 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1047 maxTab = max( maxTab, lpitem->xTab );
1048 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
1052 /* Finish the column (set all items to the largest width found) */
1053 maxX = max( maxX, maxTab + maxTabWidth );
1054 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
1056 lpitem->rect.right = maxX;
1057 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1058 lpitem->xTab = maxTab;
1061 lppop->Height = max( lppop->Height, orgY );
1064 lppop->Width = maxX;
1066 /* space for 3d border */
1067 lppop->Height += 2;
1068 lppop->Width += 2;
1070 ReleaseDC( 0, hdc );
1074 /***********************************************************************
1075 * MENU_MenuBarCalcSize
1077 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
1078 * height is off by 1 pixel which causes lengthy window relocations when
1079 * active document window is maximized/restored.
1081 * Calculate the size of the menu bar.
1083 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
1084 LPPOPUPMENU lppop, HWND hwndOwner )
1086 MENUITEM *lpitem;
1087 int start, i, orgX, orgY, maxY, helpPos;
1089 if ((lprect == NULL) || (lppop == NULL)) return;
1090 if (lppop->nItems == 0) return;
1091 TRACE("left=%ld top=%ld right=%ld bottom=%ld\n",
1092 lprect->left, lprect->top, lprect->right, lprect->bottom);
1093 lppop->Width = lprect->right - lprect->left;
1094 lppop->Height = 0;
1095 maxY = lprect->top+1;
1096 start = 0;
1097 helpPos = -1;
1098 lppop->maxBmpSize.cx = 0;
1099 lppop->maxBmpSize.cy = 0;
1100 while (start < lppop->nItems)
1102 lpitem = &lppop->items[start];
1103 orgX = lprect->left;
1104 orgY = maxY;
1106 /* Parse items until line break or end of menu */
1107 for (i = start; i < lppop->nItems; i++, lpitem++)
1109 if ((helpPos == -1) && (lpitem->fType & MF_RIGHTJUSTIFY)) helpPos = i;
1110 if ((i != start) &&
1111 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1113 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n",
1114 orgX, orgY );
1115 debug_print_menuitem (" item: ", lpitem, "");
1116 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE, lppop );
1118 if (lpitem->rect.right > lprect->right)
1120 if (i != start) break;
1121 else lpitem->rect.right = lprect->right;
1123 maxY = max( maxY, lpitem->rect.bottom );
1124 orgX = lpitem->rect.right;
1127 /* Finish the line (set all items to the largest height found) */
1128 while (start < i) lppop->items[start++].rect.bottom = maxY;
1131 lprect->bottom = maxY;
1132 lppop->Height = lprect->bottom - lprect->top;
1134 /* Flush right all items between the MF_RIGHTJUSTIFY and */
1135 /* the last item (if several lines, only move the last line) */
1136 lpitem = &lppop->items[lppop->nItems-1];
1137 orgY = lpitem->rect.top;
1138 orgX = lprect->right;
1139 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--) {
1140 if ( (helpPos==-1) || (helpPos>i) )
1141 break; /* done */
1142 if (lpitem->rect.top != orgY) break; /* Other line */
1143 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1144 lpitem->rect.left += orgX - lpitem->rect.right;
1145 lpitem->rect.right = orgX;
1146 orgX = lpitem->rect.left;
1150 /***********************************************************************
1151 * MENU_DrawMenuItem
1153 * Draw a single menu item.
1155 static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1156 UINT height, BOOL menuBar, UINT odaction )
1158 RECT rect;
1159 BOOL flat_menu = FALSE;
1160 int bkgnd;
1162 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1164 if (lpitem->fType & MF_SYSMENU)
1166 if( !IsIconic(hwnd) )
1167 NC_DrawSysButton( hwnd, hdc, lpitem->fState & (MF_HILITE | MF_MOUSESELECT) );
1168 return;
1171 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1172 bkgnd = (menuBar && flat_menu) ? COLOR_MENUBAR : COLOR_MENU;
1174 /* Setup colors */
1176 if (lpitem->fState & MF_HILITE)
1178 if(menuBar && !flat_menu) {
1179 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1180 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1181 } else {
1182 if(lpitem->fState & MF_GRAYED)
1183 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1184 else
1185 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1186 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1189 else
1191 if (lpitem->fState & MF_GRAYED)
1192 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1193 else
1194 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1195 SetBkColor( hdc, GetSysColor( bkgnd ) );
1198 if (lpitem->fType & MF_OWNERDRAW)
1201 ** Experimentation under Windows reveals that an owner-drawn
1202 ** menu is given the rectangle which includes the space it requested
1203 ** in its response to WM_MEASUREITEM _plus_ width for a checkmark
1204 ** and a popup-menu arrow. This is the value of lpitem->rect.
1205 ** Windows will leave all drawing to the application except for
1206 ** the popup-menu arrow. Windows always draws that itself, after
1207 ** the menu owner has finished drawing.
1209 DRAWITEMSTRUCT dis;
1211 dis.CtlType = ODT_MENU;
1212 dis.CtlID = 0;
1213 dis.itemID = lpitem->wID;
1214 dis.itemData = (DWORD)lpitem->dwItemData;
1215 dis.itemState = 0;
1216 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1217 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED|ODS_DISABLED;
1218 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1219 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1220 dis.hwndItem = (HWND)hmenu;
1221 dis.hDC = hdc;
1222 dis.rcItem = lpitem->rect;
1223 TRACE("Ownerdraw: owner=%p itemID=%d, itemState=%d, itemAction=%d, "
1224 "hwndItem=%p, hdc=%p, rcItem={%ld,%ld,%ld,%ld}\n", hwndOwner,
1225 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1226 dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
1227 dis.rcItem.bottom);
1228 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1229 /* Fall through to draw popup-menu arrow */
1232 TRACE("rect={%ld,%ld,%ld,%ld}\n", lpitem->rect.left, lpitem->rect.top,
1233 lpitem->rect.right,lpitem->rect.bottom);
1235 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1237 rect = lpitem->rect;
1239 if (!(lpitem->fType & MF_OWNERDRAW))
1241 if (lpitem->fState & MF_HILITE)
1243 if (flat_menu)
1245 InflateRect (&rect, -1, -1);
1246 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENUHILIGHT));
1247 InflateRect (&rect, 1, 1);
1248 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1250 else
1252 if(menuBar)
1253 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1254 else
1255 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1258 else
1259 FillRect( hdc, &rect, GetSysColorBrush(bkgnd) );
1262 SetBkMode( hdc, TRANSPARENT );
1264 if (!(lpitem->fType & MF_OWNERDRAW))
1266 HPEN oldPen;
1268 /* vertical separator */
1269 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1271 RECT rc = rect;
1272 rc.top = 3;
1273 rc.bottom = height - 3;
1274 if (flat_menu)
1276 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1277 MoveToEx( hdc, rc.left, rc.top, NULL );
1278 LineTo( hdc, rc.left, rc.bottom );
1279 SelectObject( hdc, oldPen );
1281 else
1282 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1285 /* horizontal separator */
1286 if (lpitem->fType & MF_SEPARATOR)
1288 RECT rc = rect;
1289 rc.left++;
1290 rc.right--;
1291 rc.top += SEPARATOR_HEIGHT / 2;
1292 if (flat_menu)
1294 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1295 MoveToEx( hdc, rc.left, rc.top, NULL );
1296 LineTo( hdc, rc.right, rc.top );
1297 SelectObject( hdc, oldPen );
1299 else
1300 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1301 return;
1305 /* helper lines for debugging */
1306 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1307 SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
1308 MoveToEx( hdc, rect.left, (rect.top + rect.bottom)/2, NULL );
1309 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1312 if (!menuBar)
1314 HBITMAP bm;
1315 INT y = rect.top + rect.bottom;
1316 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1317 UINT check_bitmap_height = GetSystemMetrics( SM_CYMENUCHECK );
1319 if (!(lpitem->fType & MF_OWNERDRAW))
1321 RECT rc;
1322 rc = rect;
1323 /* New style MIIM_BITMAP */
1324 if (lpitem->hbmpItem)
1326 POPUPMENU *menu = MENU_GetMenu(hmenu);
1327 if (menu->dwStyle & MNS_CHECKORBMP)
1328 rc.left += menu->maxBmpSize.cx - check_bitmap_width;
1329 else
1330 rc.left += menu->maxBmpSize.cx;
1332 /* Draw the check mark
1334 * FIXME:
1335 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1337 bm = (lpitem->fState & MF_CHECKED) ? lpitem->hCheckBit : lpitem->hUnCheckBit;
1338 if (bm) /* we have a custom bitmap */
1340 HDC hdcMem = CreateCompatibleDC( hdc );
1341 SelectObject( hdcMem, bm );
1342 BitBlt( hdc, rc.left, (y - check_bitmap_height) / 2,
1343 check_bitmap_width, check_bitmap_height,
1344 hdcMem, 0, 0, SRCCOPY );
1345 DeleteDC( hdcMem );
1347 else if (lpitem->fState & MF_CHECKED) /* standard bitmaps */
1349 RECT r;
1350 HBITMAP bm = CreateBitmap( check_bitmap_width, check_bitmap_height, 1, 1, NULL );
1351 HDC hdcMem = CreateCompatibleDC( hdc );
1352 SelectObject( hdcMem, bm );
1353 SetRect( &r, 0, 0, check_bitmap_width, check_bitmap_height );
1354 DrawFrameControl( hdcMem, &r, DFC_MENU,
1355 (lpitem->fType & MFT_RADIOCHECK) ?
1356 DFCS_MENUBULLET : DFCS_MENUCHECK );
1357 BitBlt( hdc, rc.left, (y - r.bottom) / 2, r.right, r.bottom,
1358 hdcMem, 0, 0, SRCCOPY );
1359 DeleteDC( hdcMem );
1360 DeleteObject( bm );
1362 /* New style MIIM_BITMAP */
1363 if (lpitem->hbmpItem)
1365 HBITMAP hbm = lpitem->hbmpItem;
1367 if (hbm == HBMMENU_CALLBACK)
1369 DRAWITEMSTRUCT drawItem;
1370 POINT origorg;
1371 drawItem.CtlType = ODT_MENU;
1372 drawItem.CtlID = 0;
1373 drawItem.itemID = lpitem->wID;
1374 drawItem.itemAction = odaction;
1375 drawItem.itemState = (lpitem->fState & MF_CHECKED)?ODS_CHECKED:0;
1376 drawItem.itemState |= (lpitem->fState & MF_DEFAULT)?ODS_DEFAULT:0;
1377 drawItem.itemState |= (lpitem->fState & MF_DISABLED)?ODS_DISABLED:0;
1378 drawItem.itemState |= (lpitem->fState & MF_GRAYED)?ODS_GRAYED|ODS_DISABLED:0;
1379 drawItem.itemState |= (lpitem->fState & MF_HILITE)?ODS_SELECTED:0;
1380 drawItem.hwndItem = (HWND)hmenu;
1381 drawItem.hDC = hdc;
1382 drawItem.rcItem = lpitem->rect;
1383 drawItem.itemData = lpitem->dwItemData;
1384 /* some applications make this assumption on the DC's origin */
1385 SetViewportOrgEx( hdc, lpitem->rect.left, lpitem->rect.top, &origorg);
1386 OffsetRect( &drawItem.rcItem, - lpitem->rect.left, - lpitem->rect.top);
1387 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&drawItem);
1388 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1390 } else {
1391 MENU_DrawBitmapItem(hdc, lpitem, &rect, FALSE, TRUE);
1396 /* Draw the popup-menu arrow */
1397 if (lpitem->fType & MF_POPUP)
1399 HDC hdcMem = CreateCompatibleDC( hdc );
1400 HBITMAP hOrigBitmap;
1402 hOrigBitmap = SelectObject( hdcMem, hStdMnArrow );
1403 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1404 (y - arrow_bitmap_height) / 2,
1405 arrow_bitmap_width, arrow_bitmap_height,
1406 hdcMem, 0, 0, SRCCOPY );
1407 SelectObject( hdcMem, hOrigBitmap );
1408 DeleteDC( hdcMem );
1411 rect.left += check_bitmap_width;
1412 rect.right -= arrow_bitmap_width;
1415 /* Done for owner-drawn */
1416 if (lpitem->fType & MF_OWNERDRAW)
1417 return;
1419 /* Draw the item text or bitmap */
1420 if (IS_BITMAP_ITEM(lpitem->fType))
1422 MENU_DrawBitmapItem( hdc, lpitem, &rect, menuBar, FALSE);
1423 return;
1425 /* No bitmap - process text if present */
1426 else if (IS_STRING_ITEM(lpitem->fType))
1428 register int i;
1429 HFONT hfontOld = 0;
1431 UINT uFormat = (menuBar) ?
1432 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1433 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1435 if ( lpitem->fState & MFS_DEFAULT )
1437 hfontOld = SelectObject( hdc, hMenuFontBold);
1440 if (menuBar)
1442 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1443 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1446 for (i = 0; lpitem->text[i]; i++)
1447 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1448 break;
1450 if(lpitem->fState & MF_GRAYED)
1452 if (!(lpitem->fState & MF_HILITE) )
1454 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1455 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1456 DrawTextW( hdc, lpitem->text, i, &rect, uFormat );
1457 --rect.left; --rect.top; --rect.right; --rect.bottom;
1459 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1462 DrawTextW( hdc, lpitem->text, i, &rect, uFormat);
1464 /* paint the shortcut text */
1465 if (!menuBar && lpitem->text[i]) /* There's a tab or flush-right char */
1467 if (lpitem->text[i] == '\t')
1469 rect.left = lpitem->xTab;
1470 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1472 else
1474 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1477 if(lpitem->fState & MF_GRAYED)
1479 if (!(lpitem->fState & MF_HILITE) )
1481 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1482 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1483 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1484 --rect.left; --rect.top; --rect.right; --rect.bottom;
1486 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1488 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1491 if (hfontOld)
1492 SelectObject (hdc, hfontOld);
1497 /***********************************************************************
1498 * MENU_DrawPopupMenu
1500 * Paint a popup menu.
1502 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1504 HBRUSH hPrevBrush = 0;
1505 RECT rect;
1507 TRACE("wnd=%p dc=%p menu=%p\n", hwnd, hdc, hmenu);
1509 GetClientRect( hwnd, &rect );
1511 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1512 && (SelectObject( hdc, hMenuFont)))
1514 HPEN hPrevPen;
1516 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1518 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1519 if( hPrevPen )
1521 POPUPMENU *menu;
1522 BOOL flat_menu = FALSE;
1524 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1525 if (flat_menu)
1526 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
1527 else
1528 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1530 /* draw menu items */
1532 menu = MENU_GetMenu( hmenu );
1533 if (menu && menu->nItems)
1535 MENUITEM *item;
1536 UINT u;
1538 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1539 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc, item,
1540 menu->Height, FALSE, ODA_DRAWENTIRE );
1543 } else
1545 SelectObject( hdc, hPrevBrush );
1550 /***********************************************************************
1551 * MENU_DrawMenuBar
1553 * Paint a menu bar. Returns the height of the menu bar.
1554 * called from [windows/nonclient.c]
1556 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1557 BOOL suppress_draw)
1559 LPPOPUPMENU lppop;
1560 HFONT hfontOld = 0;
1561 HMENU hMenu = GetMenu(hwnd);
1563 lppop = MENU_GetMenu( hMenu );
1564 if (lppop == NULL || lprect == NULL)
1566 return GetSystemMetrics(SM_CYMENU);
1569 if (suppress_draw)
1571 hfontOld = SelectObject( hDC, hMenuFont);
1573 if (lppop->Height == 0)
1574 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1576 lprect->bottom = lprect->top + lppop->Height;
1578 if (hfontOld) SelectObject( hDC, hfontOld);
1579 return lppop->Height;
1581 else
1582 return DrawMenuBarTemp(hwnd, hDC, lprect, hMenu, NULL);
1586 /***********************************************************************
1587 * MENU_ShowPopup
1589 * Display a popup menu.
1591 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1592 INT x, INT y, INT xanchor, INT yanchor )
1594 POPUPMENU *menu;
1595 UINT width, height;
1597 TRACE("owner=%p hmenu=%p id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1598 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1600 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1601 if (menu->FocusedItem != NO_SELECTED_ITEM)
1603 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1604 menu->FocusedItem = NO_SELECTED_ITEM;
1607 /* store the owner for DrawItem */
1608 menu->hwndOwner = hwndOwner;
1610 MENU_PopupMenuCalcSize( menu, hwndOwner );
1612 /* adjust popup menu pos so that it fits within the desktop */
1614 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1615 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1617 if( x + width > GetSystemMetrics(SM_CXSCREEN ))
1619 if( xanchor )
1620 x -= width - xanchor;
1621 if( x + width > GetSystemMetrics(SM_CXSCREEN))
1622 x = GetSystemMetrics(SM_CXSCREEN) - width;
1624 if( x < 0 ) x = 0;
1626 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1628 if( yanchor )
1629 y -= height + yanchor;
1630 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1631 y = GetSystemMetrics(SM_CYSCREEN) - height;
1633 if( y < 0 ) y = 0;
1635 /* NOTE: In Windows, top menu popup is not owned. */
1636 menu->hWnd = CreateWindowExW( 0, POPUPMENU_CLASS_ATOMW, NULL,
1637 WS_POPUP, x, y, width, height,
1638 hwndOwner, 0, (HINSTANCE)GetWindowLongPtrW(hwndOwner, GWLP_HINSTANCE),
1639 (LPVOID)hmenu );
1640 if( !menu->hWnd ) return FALSE;
1641 if (!top_popup) top_popup = menu->hWnd;
1643 /* Display the window */
1645 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1646 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1647 UpdateWindow( menu->hWnd );
1648 return TRUE;
1652 /***********************************************************************
1653 * MENU_SelectItem
1655 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1656 BOOL sendMenuSelect, HMENU topmenu )
1658 LPPOPUPMENU lppop;
1659 HDC hdc;
1661 TRACE("owner=%p menu=%p index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1663 lppop = MENU_GetMenu( hmenu );
1664 if ((!lppop) || (!lppop->nItems) || (!lppop->hWnd)) return;
1666 if (lppop->FocusedItem == wIndex) return;
1667 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1668 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1669 if (!top_popup) top_popup = lppop->hWnd;
1671 SelectObject( hdc, hMenuFont);
1673 /* Clear previous highlighted item */
1674 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1676 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1677 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1678 lppop->Height, !(lppop->wFlags & MF_POPUP),
1679 ODA_SELECT );
1682 /* Highlight new item (if any) */
1683 lppop->FocusedItem = wIndex;
1684 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1686 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1687 lppop->items[wIndex].fState |= MF_HILITE;
1688 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1689 &lppop->items[wIndex], lppop->Height,
1690 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1692 if (sendMenuSelect)
1694 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1695 SendMessageW( hwndOwner, WM_MENUSELECT,
1696 MAKELONG(ip->fType & MF_POPUP ? wIndex: ip->wID,
1697 ip->fType | ip->fState |
1698 (lppop->wFlags & MF_SYSMENU)), (LPARAM)hmenu);
1701 else if (sendMenuSelect) {
1702 if(topmenu){
1703 int pos;
1704 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1705 POPUPMENU *ptm = MENU_GetMenu( topmenu );
1706 MENUITEM *ip = &ptm->items[pos];
1707 SendMessageW( hwndOwner, WM_MENUSELECT, MAKELONG(pos,
1708 ip->fType | ip->fState |
1709 (ptm->wFlags & MF_SYSMENU)), (LPARAM)topmenu);
1713 ReleaseDC( lppop->hWnd, hdc );
1717 /***********************************************************************
1718 * MENU_MoveSelection
1720 * Moves currently selected item according to the offset parameter.
1721 * If there is no selection then it should select the last item if
1722 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1724 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1726 INT i;
1727 POPUPMENU *menu;
1729 TRACE("hwnd=%p hmenu=%p off=0x%04x\n", hwndOwner, hmenu, offset);
1731 menu = MENU_GetMenu( hmenu );
1732 if ((!menu) || (!menu->items)) return;
1734 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1736 if( menu->nItems == 1 ) return; else
1737 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1738 ; i += offset)
1739 if (!(menu->items[i].fType & MF_SEPARATOR))
1741 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1742 return;
1746 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1747 i >= 0 && i < menu->nItems ; i += offset)
1748 if (!(menu->items[i].fType & MF_SEPARATOR))
1750 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1751 return;
1756 /**********************************************************************
1757 * MENU_SetItemData
1759 * Set an item's flags, id and text ptr. Called by InsertMenu() and
1760 * ModifyMenu().
1762 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT_PTR id,
1763 LPCWSTR str )
1765 LPWSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1767 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1768 TRACE("flags=%x str=%p\n", flags, str);
1770 if (IS_STRING_ITEM(flags))
1772 if (!str)
1774 flags |= MF_SEPARATOR;
1775 item->text = NULL;
1777 else
1779 LPWSTR text;
1780 /* Item beginning with a backspace is a help item */
1781 if (*str == '\b')
1783 flags |= MF_HELP;
1784 str++;
1786 if (!(text = HeapAlloc( GetProcessHeap(), 0, (strlenW(str)+1) * sizeof(WCHAR) )))
1787 return FALSE;
1788 strcpyW( text, str );
1789 item->text = text;
1792 else if (IS_BITMAP_ITEM(flags))
1793 item->text = (LPWSTR)HBITMAP_32(LOWORD(str));
1794 else item->text = NULL;
1796 if (flags & MF_OWNERDRAW)
1797 item->dwItemData = (DWORD)str;
1798 else
1799 item->dwItemData = 0;
1801 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != (HMENU)id) )
1802 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1804 if (flags & MF_POPUP)
1806 POPUPMENU *menu = MENU_GetMenu((HMENU)id);
1807 if (menu) menu->wFlags |= MF_POPUP;
1808 else
1810 item->wID = 0;
1811 item->hSubMenu = 0;
1812 item->fType = 0;
1813 item->fState = 0;
1814 return FALSE;
1818 item->wID = id;
1819 if (flags & MF_POPUP) item->hSubMenu = (HMENU)id;
1821 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1822 flags |= MF_POPUP; /* keep popup */
1824 item->fType = flags & TYPE_MASK;
1825 item->fState = (flags & STATE_MASK) &
1826 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1829 /* Don't call SetRectEmpty here! */
1832 HeapFree( GetProcessHeap(), 0, prevText );
1834 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1835 return TRUE;
1839 /**********************************************************************
1840 * MENU_InsertItem
1842 * Insert (allocate) a new item into a menu.
1844 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1846 MENUITEM *newItems;
1847 POPUPMENU *menu;
1849 if (!(menu = MENU_GetMenu(hMenu)))
1850 return NULL;
1852 /* Find where to insert new item */
1854 if (flags & MF_BYPOSITION) {
1855 if (pos > menu->nItems)
1856 pos = menu->nItems;
1857 } else {
1858 if (!MENU_FindItem( &hMenu, &pos, flags ))
1859 pos = menu->nItems;
1860 else {
1861 if (!(menu = MENU_GetMenu( hMenu )))
1862 return NULL;
1866 /* Create new items array */
1868 newItems = HeapAlloc( GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
1869 if (!newItems)
1871 WARN("allocation failed\n" );
1872 return NULL;
1874 if (menu->nItems > 0)
1876 /* Copy the old array into the new one */
1877 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1878 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1879 (menu->nItems-pos)*sizeof(MENUITEM) );
1880 HeapFree( GetProcessHeap(), 0, menu->items );
1882 menu->items = newItems;
1883 menu->nItems++;
1884 memset( &newItems[pos], 0, sizeof(*newItems) );
1885 menu->Height = 0; /* force size recalculate */
1886 return &newItems[pos];
1890 /**********************************************************************
1891 * MENU_ParseResource
1893 * Parse a standard menu resource and add items to the menu.
1894 * Return a pointer to the end of the resource.
1896 * NOTE: flags is equivalent to the mtOption field
1898 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1900 WORD flags, id = 0;
1901 LPCSTR str;
1905 flags = GET_WORD(res);
1906 res += sizeof(WORD);
1907 if (!(flags & MF_POPUP))
1909 id = GET_WORD(res);
1910 res += sizeof(WORD);
1912 str = res;
1913 if (!unicode) res += strlen(str) + 1;
1914 else res += (strlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1915 if (flags & MF_POPUP)
1917 HMENU hSubMenu = CreatePopupMenu();
1918 if (!hSubMenu) return NULL;
1919 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1920 return NULL;
1921 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1922 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1924 else /* Not a popup */
1926 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1927 else AppendMenuW( hMenu, flags, id,
1928 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1930 } while (!(flags & MF_END));
1931 return res;
1935 /**********************************************************************
1936 * MENUEX_ParseResource
1938 * Parse an extended menu resource and add items to the menu.
1939 * Return a pointer to the end of the resource.
1941 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1943 WORD resinfo;
1944 do {
1945 MENUITEMINFOW mii;
1947 mii.cbSize = sizeof(mii);
1948 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1949 mii.fType = GET_DWORD(res);
1950 res += sizeof(DWORD);
1951 mii.fState = GET_DWORD(res);
1952 res += sizeof(DWORD);
1953 mii.wID = GET_DWORD(res);
1954 res += sizeof(DWORD);
1955 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
1956 res += sizeof(WORD);
1957 /* Align the text on a word boundary. */
1958 res += (~((int)res - 1)) & 1;
1959 mii.dwTypeData = (LPWSTR) res;
1960 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
1961 /* Align the following fields on a dword boundary. */
1962 res += (~((int)res - 1)) & 3;
1964 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1965 mii.fType, mii.fState, mii.wID, resinfo, debugstr_w(mii.dwTypeData));
1967 if (resinfo & 1) { /* Pop-up? */
1968 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
1969 res += sizeof(DWORD);
1970 mii.hSubMenu = CreatePopupMenu();
1971 if (!mii.hSubMenu)
1972 return NULL;
1973 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1974 DestroyMenu(mii.hSubMenu);
1975 return NULL;
1977 mii.fMask |= MIIM_SUBMENU;
1978 mii.fType |= MF_POPUP;
1980 else if(!*mii.dwTypeData && !(mii.fType & MF_SEPARATOR))
1982 WARN("Converting NULL menu item %04x, type %04x to SEPARATOR\n",
1983 mii.wID, mii.fType);
1984 mii.fType |= MF_SEPARATOR;
1986 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
1987 } while (!(resinfo & MF_END));
1988 return res;
1992 /***********************************************************************
1993 * MENU_GetSubPopup
1995 * Return the handle of the selected sub-popup menu (if any).
1997 static HMENU MENU_GetSubPopup( HMENU hmenu )
1999 POPUPMENU *menu;
2000 MENUITEM *item;
2002 menu = MENU_GetMenu( hmenu );
2004 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2006 item = &menu->items[menu->FocusedItem];
2007 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2008 return item->hSubMenu;
2009 return 0;
2013 /***********************************************************************
2014 * MENU_HideSubPopups
2016 * Hide the sub-popup menus of this menu.
2018 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2019 BOOL sendMenuSelect )
2021 POPUPMENU *menu = MENU_GetMenu( hmenu );
2023 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2025 if (menu && top_popup)
2027 HMENU hsubmenu;
2028 POPUPMENU *submenu;
2029 MENUITEM *item;
2031 if (menu->FocusedItem != NO_SELECTED_ITEM)
2033 item = &menu->items[menu->FocusedItem];
2034 if (!(item->fType & MF_POPUP) ||
2035 !(item->fState & MF_MOUSESELECT)) return;
2036 item->fState &= ~MF_MOUSESELECT;
2037 hsubmenu = item->hSubMenu;
2038 } else return;
2040 submenu = MENU_GetMenu( hsubmenu );
2041 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2042 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2043 DestroyWindow( submenu->hWnd );
2044 submenu->hWnd = 0;
2049 /***********************************************************************
2050 * MENU_ShowSubPopup
2052 * Display the sub-menu of the selected item of this menu.
2053 * Return the handle of the submenu, or hmenu if no submenu to display.
2055 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2056 BOOL selectFirst, UINT wFlags )
2058 RECT rect;
2059 POPUPMENU *menu;
2060 MENUITEM *item;
2061 HDC hdc;
2063 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, selectFirst);
2065 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2067 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
2069 item = &menu->items[menu->FocusedItem];
2070 if (!(item->fType & MF_POPUP) || (item->fState & (MF_GRAYED | MF_DISABLED)))
2071 return hmenu;
2073 /* message must be sent before using item,
2074 because nearly everything may be changed by the application ! */
2076 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2077 if (!(wFlags & TPM_NONOTIFY))
2078 SendMessageW( hwndOwner, WM_INITMENUPOPUP, (WPARAM)item->hSubMenu,
2079 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2081 item = &menu->items[menu->FocusedItem];
2082 rect = item->rect;
2084 /* correct item if modified as a reaction to WM_INITMENUPOPUP message */
2085 if (!(item->fState & MF_HILITE))
2087 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2088 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2090 SelectObject( hdc, hMenuFont);
2092 item->fState |= MF_HILITE;
2093 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2094 ReleaseDC( menu->hWnd, hdc );
2096 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2097 item->rect = rect;
2099 item->fState |= MF_MOUSESELECT;
2101 if (IS_SYSTEM_MENU(menu))
2103 MENU_InitSysMenuPopup(item->hSubMenu,
2104 GetWindowLongW( menu->hWnd, GWL_STYLE ),
2105 GetClassLongW( menu->hWnd, GCL_STYLE));
2107 NC_GetSysPopupPos( menu->hWnd, &rect );
2108 rect.top = rect.bottom;
2109 rect.right = GetSystemMetrics(SM_CXSIZE);
2110 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2112 else
2114 GetWindowRect( menu->hWnd, &rect );
2115 if (menu->wFlags & MF_POPUP)
2117 rect.left += item->rect.right - GetSystemMetrics(SM_CXBORDER);
2118 rect.top += item->rect.top;
2119 rect.right = item->rect.left - item->rect.right + GetSystemMetrics(SM_CXBORDER);
2120 rect.bottom = item->rect.top - item->rect.bottom;
2122 else
2124 rect.left += item->rect.left;
2125 rect.top += item->rect.bottom;
2126 rect.right = item->rect.right - item->rect.left;
2127 rect.bottom = item->rect.bottom - item->rect.top;
2131 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2132 rect.left, rect.top, rect.right, rect.bottom );
2133 if (selectFirst)
2134 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2135 return item->hSubMenu;
2140 /**********************************************************************
2141 * MENU_IsMenuActive
2143 HWND MENU_IsMenuActive(void)
2145 return top_popup;
2148 /***********************************************************************
2149 * MENU_PtMenu
2151 * Walks menu chain trying to find a menu pt maps to.
2153 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2155 POPUPMENU *menu = MENU_GetMenu( hMenu );
2156 UINT item = menu->FocusedItem;
2157 HMENU ret;
2159 /* try subpopup first (if any) */
2160 ret = (item != NO_SELECTED_ITEM &&
2161 (menu->items[item].fType & MF_POPUP) &&
2162 (menu->items[item].fState & MF_MOUSESELECT))
2163 ? MENU_PtMenu(menu->items[item].hSubMenu, pt) : 0;
2165 if (!ret) /* check the current window (avoiding WM_HITTEST) */
2167 INT ht = NC_HandleNCHitTest( menu->hWnd, pt );
2168 if( menu->wFlags & MF_POPUP )
2170 if (ht != HTNOWHERE && ht != HTERROR) ret = hMenu;
2172 else if (ht == HTSYSMENU)
2173 ret = get_win_sys_menu( menu->hWnd );
2174 else if (ht == HTMENU)
2175 ret = GetMenu( menu->hWnd );
2177 return ret;
2180 /***********************************************************************
2181 * MENU_ExecFocusedItem
2183 * Execute a menu item (for instance when user pressed Enter).
2184 * Return the wID of the executed item. Otherwise, -1 indicating
2185 * that no menu item was executed;
2186 * Have to receive the flags for the TrackPopupMenu options to avoid
2187 * sending unwanted message.
2190 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2192 MENUITEM *item;
2193 POPUPMENU *menu = MENU_GetMenu( hMenu );
2195 TRACE("%p hmenu=%p\n", pmt, hMenu);
2197 if (!menu || !menu->nItems ||
2198 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2200 item = &menu->items[menu->FocusedItem];
2202 TRACE("%p %08x %p\n", hMenu, item->wID, item->hSubMenu);
2204 if (!(item->fType & MF_POPUP))
2206 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2208 /* If TPM_RETURNCMD is set you return the id, but
2209 do not send a message to the owner */
2210 if(!(wFlags & TPM_RETURNCMD))
2212 if( menu->wFlags & MF_SYSMENU )
2213 PostMessageW( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2214 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2215 else
2216 PostMessageW( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2218 return item->wID;
2221 else
2222 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2224 return -1;
2227 /***********************************************************************
2228 * MENU_SwitchTracking
2230 * Helper function for menu navigation routines.
2232 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2234 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2235 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2237 TRACE("%p hmenu=%p 0x%04x\n", pmt, hPtMenu, id);
2239 if( pmt->hTopMenu != hPtMenu &&
2240 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2242 /* both are top level menus (system and menu-bar) */
2243 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2244 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2245 pmt->hTopMenu = hPtMenu;
2247 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2248 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2252 /***********************************************************************
2253 * MENU_ButtonDown
2255 * Return TRUE if we can go on with menu tracking.
2257 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2259 TRACE("%p hPtMenu=%p\n", pmt, hPtMenu);
2261 if (hPtMenu)
2263 UINT id = 0;
2264 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2265 MENUITEM *item;
2267 if( IS_SYSTEM_MENU(ptmenu) )
2268 item = ptmenu->items;
2269 else
2270 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2272 if( item )
2274 if( ptmenu->FocusedItem != id )
2275 MENU_SwitchTracking( pmt, hPtMenu, id );
2277 /* If the popup menu is not already "popped" */
2278 if(!(item->fState & MF_MOUSESELECT ))
2280 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2283 return TRUE;
2285 /* Else the click was on the menu bar, finish the tracking */
2287 return FALSE;
2290 /***********************************************************************
2291 * MENU_ButtonUp
2293 * Return the value of MENU_ExecFocusedItem if
2294 * the selected item was not a popup. Else open the popup.
2295 * A -1 return value indicates that we go on with menu tracking.
2298 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2300 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2302 if (hPtMenu)
2304 UINT id = 0;
2305 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2306 MENUITEM *item;
2308 if( IS_SYSTEM_MENU(ptmenu) )
2309 item = ptmenu->items;
2310 else
2311 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2313 if( item && (ptmenu->FocusedItem == id ))
2315 if( !(item->fType & MF_POPUP) )
2316 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2318 /* If we are dealing with the top-level menu */
2319 /* and this is a click on an already "popped" item: */
2320 /* Stop the menu tracking and close the opened submenus */
2321 if((pmt->hTopMenu == hPtMenu) && ptmenu->bTimeToHide)
2322 return 0;
2324 ptmenu->bTimeToHide = TRUE;
2326 return -1;
2330 /***********************************************************************
2331 * MENU_MouseMove
2333 * Return TRUE if we can go on with menu tracking.
2335 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2337 UINT id = NO_SELECTED_ITEM;
2338 POPUPMENU *ptmenu = NULL;
2340 if( hPtMenu )
2342 ptmenu = MENU_GetMenu( hPtMenu );
2343 if( IS_SYSTEM_MENU(ptmenu) )
2344 id = 0;
2345 else
2346 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2349 if( id == NO_SELECTED_ITEM )
2351 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2352 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2355 else if( ptmenu->FocusedItem != id )
2357 MENU_SwitchTracking( pmt, hPtMenu, id );
2358 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2360 return TRUE;
2364 /***********************************************************************
2365 * MENU_SetCapture
2367 static void MENU_SetCapture( HWND hwnd )
2369 HWND previous = 0;
2371 SERVER_START_REQ( set_capture_window )
2373 req->handle = hwnd;
2374 req->flags = CAPTURE_MENU;
2375 if (!wine_server_call_err( req ))
2377 previous = reply->previous;
2378 hwnd = reply->full_handle;
2381 SERVER_END_REQ;
2383 if (previous && previous != hwnd)
2384 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
2388 /***********************************************************************
2389 * MENU_DoNextMenu
2391 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2393 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2395 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2397 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2398 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2400 MDINEXTMENU next_menu;
2401 HMENU hNewMenu;
2402 HWND hNewWnd;
2403 UINT id = 0;
2405 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2406 next_menu.hmenuNext = 0;
2407 next_menu.hwndNext = 0;
2408 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2410 TRACE("%p [%p] -> %p [%p]\n",
2411 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2413 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2415 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2416 hNewWnd = pmt->hOwnerWnd;
2417 if( IS_SYSTEM_MENU(menu) )
2419 /* switch to the menu bar */
2421 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2423 if( vk == VK_LEFT )
2425 menu = MENU_GetMenu( hNewMenu );
2426 id = menu->nItems - 1;
2429 else if (style & WS_SYSMENU )
2431 /* switch to the system menu */
2432 hNewMenu = get_win_sys_menu( hNewWnd );
2434 else return FALSE;
2436 else /* application returned a new menu to switch to */
2438 hNewMenu = next_menu.hmenuNext;
2439 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2441 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2443 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2445 if (style & WS_SYSMENU &&
2446 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2448 /* get the real system menu */
2449 hNewMenu = get_win_sys_menu(hNewWnd);
2451 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2453 /* FIXME: Not sure what to do here;
2454 * perhaps try to track hNewMenu as a popup? */
2456 TRACE(" -- got confused.\n");
2457 return FALSE;
2460 else return FALSE;
2463 if( hNewMenu != pmt->hTopMenu )
2465 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2466 FALSE, 0 );
2467 if( pmt->hCurrentMenu != pmt->hTopMenu )
2468 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2471 if( hNewWnd != pmt->hOwnerWnd )
2473 pmt->hOwnerWnd = hNewWnd;
2474 MENU_SetCapture( pmt->hOwnerWnd );
2477 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2478 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2480 return TRUE;
2482 return FALSE;
2485 /***********************************************************************
2486 * MENU_SuspendPopup
2488 * The idea is not to show the popup if the next input message is
2489 * going to hide it anyway.
2491 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2493 MSG msg;
2495 msg.hwnd = pmt->hOwnerWnd;
2497 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2498 pmt->trackFlags |= TF_SKIPREMOVE;
2500 switch( uMsg )
2502 case WM_KEYDOWN:
2503 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2504 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2506 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2507 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2508 if( msg.message == WM_KEYDOWN &&
2509 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2511 pmt->trackFlags |= TF_SUSPENDPOPUP;
2512 return TRUE;
2515 break;
2518 /* failures go through this */
2519 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2520 return FALSE;
2523 /***********************************************************************
2524 * MENU_KeyEscape
2526 * Handle a VK_ESCAPE key event in a menu.
2528 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2530 BOOL bEndMenu = TRUE;
2532 if (pmt->hCurrentMenu != pmt->hTopMenu)
2534 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2536 if (menu->wFlags & MF_POPUP)
2538 HMENU hmenutmp, hmenuprev;
2540 hmenuprev = hmenutmp = pmt->hTopMenu;
2542 /* close topmost popup */
2543 while (hmenutmp != pmt->hCurrentMenu)
2545 hmenuprev = hmenutmp;
2546 hmenutmp = MENU_GetSubPopup( hmenuprev );
2549 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2550 pmt->hCurrentMenu = hmenuprev;
2551 bEndMenu = FALSE;
2555 return bEndMenu;
2558 /***********************************************************************
2559 * MENU_KeyLeft
2561 * Handle a VK_LEFT key event in a menu.
2563 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2565 POPUPMENU *menu;
2566 HMENU hmenutmp, hmenuprev;
2567 UINT prevcol;
2569 hmenuprev = hmenutmp = pmt->hTopMenu;
2570 menu = MENU_GetMenu( hmenutmp );
2572 /* Try to move 1 column left (if possible) */
2573 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2574 NO_SELECTED_ITEM ) {
2576 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2577 prevcol, TRUE, 0 );
2578 return;
2581 /* close topmost popup */
2582 while (hmenutmp != pmt->hCurrentMenu)
2584 hmenuprev = hmenutmp;
2585 hmenutmp = MENU_GetSubPopup( hmenuprev );
2588 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2589 pmt->hCurrentMenu = hmenuprev;
2591 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2593 /* move menu bar selection if no more popups are left */
2595 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2596 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2598 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2600 /* A sublevel menu was displayed - display the next one
2601 * unless there is another displacement coming up */
2603 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2604 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2605 pmt->hTopMenu, TRUE, wFlags);
2611 /***********************************************************************
2612 * MENU_KeyRight
2614 * Handle a VK_RIGHT key event in a menu.
2616 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2618 HMENU hmenutmp;
2619 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2620 UINT nextcol;
2622 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2623 pmt->hCurrentMenu,
2624 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2625 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2627 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2629 /* If already displaying a popup, try to display sub-popup */
2631 hmenutmp = pmt->hCurrentMenu;
2632 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2634 /* if subpopup was displayed then we are done */
2635 if (hmenutmp != pmt->hCurrentMenu) return;
2638 /* Check to see if there's another column */
2639 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2640 NO_SELECTED_ITEM ) {
2641 TRACE("Going to %d.\n", nextcol );
2642 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2643 nextcol, TRUE, 0 );
2644 return;
2647 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2649 if( pmt->hCurrentMenu != pmt->hTopMenu )
2651 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2652 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2653 } else hmenutmp = 0;
2655 /* try to move to the next item */
2656 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2657 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2659 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2660 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2661 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2662 pmt->hTopMenu, TRUE, wFlags);
2666 /***********************************************************************
2667 * MENU_TrackMenu
2669 * Menu tracking code.
2671 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
2672 HWND hwnd, const RECT *lprect )
2674 MSG msg;
2675 POPUPMENU *menu;
2676 BOOL fRemove;
2677 INT executedMenuId = -1;
2678 MTRACKER mt;
2679 BOOL enterIdleSent = FALSE;
2681 mt.trackFlags = 0;
2682 mt.hCurrentMenu = hmenu;
2683 mt.hTopMenu = hmenu;
2684 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
2685 mt.pt.x = x;
2686 mt.pt.y = y;
2688 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p (%ld,%ld)-(%ld,%ld)\n",
2689 hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2690 (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2692 fEndMenu = FALSE;
2693 if (!(menu = MENU_GetMenu( hmenu )))
2695 WARN("Invalid menu handle %p\n", hmenu);
2696 SetLastError(ERROR_INVALID_MENU_HANDLE);
2697 return FALSE;
2700 if (wFlags & TPM_BUTTONDOWN)
2702 /* Get the result in order to start the tracking or not */
2703 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2704 fEndMenu = !fRemove;
2707 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
2709 MENU_SetCapture( mt.hOwnerWnd );
2711 while (!fEndMenu)
2713 menu = MENU_GetMenu( mt.hCurrentMenu );
2714 if (!menu) /* sometimes happens if I do a window manager close */
2715 break;
2717 /* we have to keep the message in the queue until it's
2718 * clear that menu loop is not over yet. */
2720 for (;;)
2722 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
2724 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
2725 /* remove the message from the queue */
2726 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2728 else
2730 if (!enterIdleSent)
2732 HWND win = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2733 enterIdleSent = TRUE;
2734 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
2736 WaitMessage();
2740 /* check if EndMenu() tried to cancel us, by posting this message */
2741 if(msg.message == WM_CANCELMODE)
2743 /* we are now out of the loop */
2744 fEndMenu = TRUE;
2746 /* remove the message from the queue */
2747 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2749 /* break out of internal loop, ala ESCAPE */
2750 break;
2753 TranslateMessage( &msg );
2754 mt.pt = msg.pt;
2756 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2757 enterIdleSent=FALSE;
2759 fRemove = FALSE;
2760 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2763 * Use the mouse coordinates in lParam instead of those in the MSG
2764 * struct to properly handle synthetic messages. They are already
2765 * in screen coordinates.
2767 mt.pt.x = (short)LOWORD(msg.lParam);
2768 mt.pt.y = (short)HIWORD(msg.lParam);
2770 /* Find a menu for this mouse event */
2771 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
2773 switch(msg.message)
2775 /* no WM_NC... messages in captured state */
2777 case WM_RBUTTONDBLCLK:
2778 case WM_RBUTTONDOWN:
2779 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2780 /* fall through */
2781 case WM_LBUTTONDBLCLK:
2782 case WM_LBUTTONDOWN:
2783 /* If the message belongs to the menu, removes it from the queue */
2784 /* Else, end menu tracking */
2785 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2786 fEndMenu = !fRemove;
2787 break;
2789 case WM_RBUTTONUP:
2790 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2791 /* fall through */
2792 case WM_LBUTTONUP:
2793 /* Check if a menu was selected by the mouse */
2794 if (hmenu)
2796 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2798 /* End the loop if executedMenuId is an item ID */
2799 /* or if the job was done (executedMenuId = 0). */
2800 fEndMenu = fRemove = (executedMenuId != -1);
2802 /* No menu was selected by the mouse */
2803 /* if the function was called by TrackPopupMenu, continue
2804 with the menu tracking. If not, stop it */
2805 else
2806 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2808 break;
2810 case WM_MOUSEMOVE:
2811 /* the selected menu item must be changed every time */
2812 /* the mouse moves. */
2814 if (hmenu)
2815 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2817 } /* switch(msg.message) - mouse */
2819 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2821 fRemove = TRUE; /* Keyboard messages are always removed */
2822 switch(msg.message)
2824 case WM_KEYDOWN:
2825 case WM_SYSKEYDOWN:
2826 switch(msg.wParam)
2828 case VK_MENU:
2829 fEndMenu = TRUE;
2830 break;
2832 case VK_HOME:
2833 case VK_END:
2834 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2835 NO_SELECTED_ITEM, FALSE, 0 );
2836 /* fall through */
2837 case VK_UP:
2838 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2839 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2840 break;
2842 case VK_DOWN: /* If on menu bar, pull-down the menu */
2844 menu = MENU_GetMenu( mt.hCurrentMenu );
2845 if (!(menu->wFlags & MF_POPUP))
2846 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
2847 else /* otherwise try to move selection */
2848 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2849 break;
2851 case VK_LEFT:
2852 MENU_KeyLeft( &mt, wFlags );
2853 break;
2855 case VK_RIGHT:
2856 MENU_KeyRight( &mt, wFlags );
2857 break;
2859 case VK_ESCAPE:
2860 fEndMenu = MENU_KeyEscape(&mt, wFlags);
2861 break;
2863 case VK_F1:
2865 HELPINFO hi;
2866 hi.cbSize = sizeof(HELPINFO);
2867 hi.iContextType = HELPINFO_MENUITEM;
2868 if (menu->FocusedItem == NO_SELECTED_ITEM)
2869 hi.iCtrlId = 0;
2870 else
2871 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2872 hi.hItemHandle = hmenu;
2873 hi.dwContextId = menu->dwContextHelpID;
2874 hi.MousePos = msg.pt;
2875 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
2876 break;
2879 default:
2880 break;
2882 break; /* WM_KEYDOWN */
2884 case WM_CHAR:
2885 case WM_SYSCHAR:
2887 UINT pos;
2889 if (msg.wParam == '\r' || msg.wParam == ' ')
2891 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2892 fEndMenu = (executedMenuId != -1);
2894 break;
2897 /* Hack to avoid control chars. */
2898 /* We will find a better way real soon... */
2899 if (msg.wParam < 32) break;
2901 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2902 LOWORD(msg.wParam), FALSE );
2903 if (pos == (UINT)-2) fEndMenu = TRUE;
2904 else if (pos == (UINT)-1) MessageBeep(0);
2905 else
2907 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
2908 TRUE, 0 );
2909 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2910 fEndMenu = (executedMenuId != -1);
2913 break;
2914 } /* switch(msg.message) - kbd */
2916 else
2918 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2919 DispatchMessageW( &msg );
2920 continue;
2923 if (!fEndMenu) fRemove = TRUE;
2925 /* finally remove message from the queue */
2927 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2928 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2929 else mt.trackFlags &= ~TF_SKIPREMOVE;
2932 MENU_SetCapture(0); /* release the capture */
2934 /* If dropdown is still painted and the close box is clicked on
2935 then the menu will be destroyed as part of the DispatchMessage above.
2936 This will then invalidate the menu handle in mt.hTopMenu. We should
2937 check for this first. */
2938 if( IsMenu( mt.hTopMenu ) )
2940 menu = MENU_GetMenu( mt.hTopMenu );
2942 if( IsWindow( mt.hOwnerWnd ) )
2944 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2946 if (menu && (menu->wFlags & MF_POPUP))
2948 DestroyWindow( menu->hWnd );
2949 menu->hWnd = 0;
2951 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2952 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
2955 /* Reset the variable for hiding menu */
2956 if( menu ) menu->bTimeToHide = FALSE;
2959 /* The return value is only used by TrackPopupMenu */
2960 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
2961 if (executedMenuId == -1) executedMenuId = 0;
2962 return executedMenuId;
2965 /***********************************************************************
2966 * MENU_InitTracking
2968 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
2970 POPUPMENU *menu;
2972 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
2974 HideCaret(0);
2976 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
2977 if (!(wFlags & TPM_NONOTIFY))
2978 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
2980 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
2982 if (!(wFlags & TPM_NONOTIFY))
2984 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
2985 /* If an app changed/recreated menu bar entries in WM_INITMENU
2986 * menu sizes will be recalculated once the menu created/shown.
2990 /* This makes the menus of applications built with Delphi work.
2991 * It also enables menus to be displayed in more than one window,
2992 * but there are some bugs left that need to be fixed in this case.
2994 if ((menu = MENU_GetMenu( hMenu ))) menu->hWnd = hWnd;
2996 return TRUE;
2998 /***********************************************************************
2999 * MENU_ExitTracking
3001 static BOOL MENU_ExitTracking(HWND hWnd)
3003 TRACE("hwnd=%p\n", hWnd);
3005 SendMessageW( hWnd, WM_EXITMENULOOP, 0, 0 );
3006 ShowCaret(0);
3007 top_popup = 0;
3008 return TRUE;
3011 /***********************************************************************
3012 * MENU_TrackMouseMenuBar
3014 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3016 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3018 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3019 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3021 TRACE("wnd=%p ht=0x%04x (%ld,%ld)\n", hWnd, ht, pt.x, pt.y);
3023 if (IsMenu(hMenu))
3025 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3026 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3027 MENU_ExitTracking(hWnd);
3032 /***********************************************************************
3033 * MENU_TrackKbdMenuBar
3035 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3037 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3039 UINT uItem = NO_SELECTED_ITEM;
3040 HMENU hTrackMenu;
3041 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3043 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3045 /* find window that has a menu */
3047 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3048 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3050 /* check if we have to track a system menu */
3052 hTrackMenu = GetMenu( hwnd );
3053 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3055 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3056 hTrackMenu = get_win_sys_menu( hwnd );
3057 uItem = 0;
3058 wParam |= HTSYSMENU; /* prevent item lookup */
3061 if (!IsMenu( hTrackMenu )) return;
3063 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3065 if( wChar && wChar != ' ' )
3067 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3068 if ( uItem >= (UINT)(-2) )
3070 if( uItem == (UINT)(-1) ) MessageBeep(0);
3071 /* schedule end of menu tracking */
3072 wFlags |= TF_ENDMENU;
3073 goto track_menu;
3077 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3079 if (wParam & HTSYSMENU)
3081 /* prevent sysmenu activation for managed windows on Alt down/up */
3082 if (GetPropA( hwnd, "__wine_x11_managed" ))
3083 wFlags |= TF_ENDMENU; /* schedule end of menu tracking */
3085 else
3087 if( uItem == NO_SELECTED_ITEM )
3088 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3089 else
3090 PostMessageW( hwnd, WM_KEYDOWN, VK_DOWN, 0L );
3093 track_menu:
3094 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3095 MENU_ExitTracking( hwnd );
3099 /**********************************************************************
3100 * TrackPopupMenu (USER32.@)
3102 * Like the win32 API, the function return the command ID only if the
3103 * flag TPM_RETURNCMD is on.
3106 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3107 INT nReserved, HWND hWnd, const RECT *lpRect )
3109 BOOL ret = FALSE;
3111 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3113 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3114 if (!(wFlags & TPM_NONOTIFY))
3115 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3117 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3118 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
3119 MENU_ExitTracking(hWnd);
3121 return ret;
3124 /**********************************************************************
3125 * TrackPopupMenuEx (USER32.@)
3127 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3128 HWND hWnd, LPTPMPARAMS lpTpm )
3130 FIXME("not fully implemented\n" );
3131 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3132 lpTpm ? &lpTpm->rcExclude : NULL );
3135 /***********************************************************************
3136 * PopupMenuWndProc
3138 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3140 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3142 TRACE("hwnd=%p msg=0x%04x wp=0x%04x lp=0x%08lx\n", hwnd, message, wParam, lParam);
3144 switch(message)
3146 case WM_CREATE:
3148 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3149 SetWindowLongW( hwnd, 0, (LONG)cs->lpCreateParams );
3150 return 0;
3153 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3154 return MA_NOACTIVATE;
3156 case WM_PAINT:
3158 PAINTSTRUCT ps;
3159 BeginPaint( hwnd, &ps );
3160 MENU_DrawPopupMenu( hwnd, ps.hdc,
3161 (HMENU)GetWindowLongW( hwnd, 0 ) );
3162 EndPaint( hwnd, &ps );
3163 return 0;
3165 case WM_ERASEBKGND:
3166 return 1;
3168 case WM_DESTROY:
3169 /* zero out global pointer in case resident popup window was destroyed. */
3170 if (hwnd == top_popup) top_popup = 0;
3171 break;
3173 case WM_SHOWWINDOW:
3175 if( wParam )
3177 if (!GetWindowLongW( hwnd, 0 )) ERR("no menu to display\n");
3179 else
3180 SetWindowLongW( hwnd, 0, 0 );
3181 break;
3183 case MM_SETMENUHANDLE:
3184 SetWindowLongW( hwnd, 0, wParam );
3185 break;
3187 case MM_GETMENUHANDLE:
3188 return GetWindowLongW( hwnd, 0 );
3190 default:
3191 return DefWindowProcW( hwnd, message, wParam, lParam );
3193 return 0;
3197 /***********************************************************************
3198 * MENU_GetMenuBarHeight
3200 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3202 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3203 INT orgX, INT orgY )
3205 HDC hdc;
3206 RECT rectBar;
3207 LPPOPUPMENU lppop;
3209 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3211 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3213 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3214 SelectObject( hdc, hMenuFont);
3215 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3216 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3217 ReleaseDC( hwnd, hdc );
3218 return lppop->Height;
3222 /*******************************************************************
3223 * ChangeMenuA (USER32.@)
3225 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3226 UINT id, UINT flags )
3228 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3229 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3230 id, data );
3231 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3232 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3233 id, data );
3234 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3235 flags & MF_BYPOSITION ? pos : id,
3236 flags & ~MF_REMOVE );
3237 /* Default: MF_INSERT */
3238 return InsertMenuA( hMenu, pos, flags, id, data );
3242 /*******************************************************************
3243 * ChangeMenuW (USER32.@)
3245 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3246 UINT id, UINT flags )
3248 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3249 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3250 id, data );
3251 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3252 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3253 id, data );
3254 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3255 flags & MF_BYPOSITION ? pos : id,
3256 flags & ~MF_REMOVE );
3257 /* Default: MF_INSERT */
3258 return InsertMenuW( hMenu, pos, flags, id, data );
3262 /*******************************************************************
3263 * CheckMenuItem (USER32.@)
3265 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3267 MENUITEM *item;
3268 DWORD ret;
3270 TRACE("menu=%p id=%04x flags=%04x\n", hMenu, id, flags );
3271 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3272 ret = item->fState & MF_CHECKED;
3273 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3274 else item->fState &= ~MF_CHECKED;
3275 return ret;
3279 /**********************************************************************
3280 * EnableMenuItem (USER32.@)
3282 UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3284 UINT oldflags;
3285 MENUITEM *item;
3286 POPUPMENU *menu;
3288 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3290 /* Get the Popupmenu to access the owner menu */
3291 if (!(menu = MENU_GetMenu(hMenu)))
3292 return (UINT)-1;
3294 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3295 return (UINT)-1;
3297 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3298 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3300 /* If the close item in the system menu change update the close button */
3301 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3303 if (menu->hSysMenuOwner != 0)
3305 RECT rc;
3306 POPUPMENU* parentMenu;
3308 /* Get the parent menu to access*/
3309 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3310 return (UINT)-1;
3312 /* Refresh the frame to reflect the change */
3313 GetWindowRect(parentMenu->hWnd, &rc);
3314 MapWindowPoints(0, parentMenu->hWnd, (POINT *)&rc, 2);
3315 rc.bottom = 0;
3316 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3320 return oldflags;
3324 /*******************************************************************
3325 * GetMenuStringA (USER32.@)
3327 INT WINAPI GetMenuStringA(
3328 HMENU hMenu, /* [in] menuhandle */
3329 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3330 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3331 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3332 UINT wFlags /* [in] MF_ flags */
3334 MENUITEM *item;
3336 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3337 if (str && nMaxSiz) str[0] = '\0';
3338 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3339 if (!IS_STRING_ITEM(item->fType)) return 0;
3340 if (!str || !nMaxSiz) return strlenW(item->text);
3341 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3342 str[nMaxSiz-1] = 0;
3343 TRACE("returning '%s'\n", str );
3344 return strlen(str);
3348 /*******************************************************************
3349 * GetMenuStringW (USER32.@)
3351 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3352 LPWSTR str, INT nMaxSiz, UINT wFlags )
3354 MENUITEM *item;
3356 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3357 if (str && nMaxSiz) str[0] = '\0';
3358 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3359 if (!IS_STRING_ITEM(item->fType)) return 0;
3360 if (!str || !nMaxSiz) return strlenW(item->text);
3361 lstrcpynW( str, item->text, nMaxSiz );
3362 return strlenW(str);
3366 /**********************************************************************
3367 * HiliteMenuItem (USER32.@)
3369 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3370 UINT wHilite )
3372 LPPOPUPMENU menu;
3373 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3374 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3375 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3376 if (menu->FocusedItem == wItemID) return TRUE;
3377 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3378 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3379 return TRUE;
3383 /**********************************************************************
3384 * GetMenuState (USER32.@)
3386 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3388 MENUITEM *item;
3389 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3390 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3391 debug_print_menuitem (" item: ", item, "");
3392 if (item->fType & MF_POPUP)
3394 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3395 if (!menu) return -1;
3396 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3398 else
3400 /* We used to (from way back then) mask the result to 0xff. */
3401 /* I don't know why and it seems wrong as the documented */
3402 /* return flag MF_SEPARATOR is outside that mask. */
3403 return (item->fType | item->fState);
3408 /**********************************************************************
3409 * GetMenuItemCount (USER32.@)
3411 INT WINAPI GetMenuItemCount( HMENU hMenu )
3413 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3414 if (!menu) return -1;
3415 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3416 return menu->nItems;
3420 /**********************************************************************
3421 * GetMenuItemID (USER32.@)
3423 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3425 MENUITEM * lpmi;
3427 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return -1;
3428 if (lpmi->fType & MF_POPUP) return -1;
3429 return lpmi->wID;
3434 /*******************************************************************
3435 * InsertMenuW (USER32.@)
3437 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3438 UINT_PTR id, LPCWSTR str )
3440 MENUITEM *item;
3442 if (IS_STRING_ITEM(flags) && str)
3443 TRACE("hMenu %p, pos %d, flags %08x, id %04x, str %s\n",
3444 hMenu, pos, flags, id, debugstr_w(str) );
3445 else TRACE("hMenu %p, pos %d, flags %08x, id %04x, str %08lx (not a string)\n",
3446 hMenu, pos, flags, id, (DWORD)str );
3448 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3450 if (!(MENU_SetItemData( item, flags, id, str )))
3452 RemoveMenu( hMenu, pos, flags );
3453 return FALSE;
3456 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3457 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3459 item->hCheckBit = item->hUnCheckBit = 0;
3460 return TRUE;
3464 /*******************************************************************
3465 * InsertMenuA (USER32.@)
3467 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3468 UINT_PTR id, LPCSTR str )
3470 BOOL ret = FALSE;
3472 if (IS_STRING_ITEM(flags) && str)
3474 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3475 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3476 if (newstr)
3478 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3479 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3480 HeapFree( GetProcessHeap(), 0, newstr );
3482 return ret;
3484 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3488 /*******************************************************************
3489 * AppendMenuA (USER32.@)
3491 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3492 UINT_PTR id, LPCSTR data )
3494 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3498 /*******************************************************************
3499 * AppendMenuW (USER32.@)
3501 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3502 UINT_PTR id, LPCWSTR data )
3504 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3508 /**********************************************************************
3509 * RemoveMenu (USER32.@)
3511 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3513 LPPOPUPMENU menu;
3514 MENUITEM *item;
3516 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3517 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3518 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3520 /* Remove item */
3522 MENU_FreeItemData( item );
3524 if (--menu->nItems == 0)
3526 HeapFree( GetProcessHeap(), 0, menu->items );
3527 menu->items = NULL;
3529 else
3531 while(nPos < menu->nItems)
3533 *item = *(item+1);
3534 item++;
3535 nPos++;
3537 menu->items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
3538 menu->nItems * sizeof(MENUITEM) );
3540 return TRUE;
3544 /**********************************************************************
3545 * DeleteMenu (USER32.@)
3547 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3549 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3550 if (!item) return FALSE;
3551 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3552 /* nPos is now the position of the item */
3553 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3554 return TRUE;
3558 /*******************************************************************
3559 * ModifyMenuW (USER32.@)
3561 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3562 UINT_PTR id, LPCWSTR str )
3564 MENUITEM *item;
3566 if (IS_STRING_ITEM(flags))
3568 TRACE("%p %d %04x %04x %s\n", hMenu, pos, flags, id, debugstr_w(str) );
3570 else
3572 TRACE("%p %d %04x %04x %08lx\n", hMenu, pos, flags, id, (DWORD)str );
3575 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3576 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
3577 return MENU_SetItemData( item, flags, id, str );
3581 /*******************************************************************
3582 * ModifyMenuA (USER32.@)
3584 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3585 UINT_PTR id, LPCSTR str )
3587 BOOL ret = FALSE;
3589 if (IS_STRING_ITEM(flags) && str)
3591 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3592 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3593 if (newstr)
3595 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3596 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
3597 HeapFree( GetProcessHeap(), 0, newstr );
3599 return ret;
3601 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3605 /**********************************************************************
3606 * CreatePopupMenu (USER32.@)
3608 HMENU WINAPI CreatePopupMenu(void)
3610 HMENU hmenu;
3611 POPUPMENU *menu;
3613 if (!(hmenu = CreateMenu())) return 0;
3614 menu = MENU_GetMenu( hmenu );
3615 menu->wFlags |= MF_POPUP;
3616 menu->bTimeToHide = FALSE;
3617 return hmenu;
3621 /**********************************************************************
3622 * GetMenuCheckMarkDimensions (USER.417)
3623 * GetMenuCheckMarkDimensions (USER32.@)
3625 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3627 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
3631 /**********************************************************************
3632 * SetMenuItemBitmaps (USER32.@)
3634 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
3635 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3637 MENUITEM *item;
3638 TRACE("(%p, %04x, %04x, %p, %p)\n",
3639 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3640 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3642 if (!hNewCheck && !hNewUnCheck)
3644 item->fState &= ~MF_USECHECKBITMAPS;
3646 else /* Install new bitmaps */
3648 item->hCheckBit = hNewCheck;
3649 item->hUnCheckBit = hNewUnCheck;
3650 item->fState |= MF_USECHECKBITMAPS;
3652 return TRUE;
3656 /**********************************************************************
3657 * CreateMenu (USER32.@)
3659 HMENU WINAPI CreateMenu(void)
3661 HMENU hMenu;
3662 LPPOPUPMENU menu;
3663 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3664 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3666 ZeroMemory(menu, sizeof(POPUPMENU));
3667 menu->wMagic = MENU_MAGIC;
3668 menu->FocusedItem = NO_SELECTED_ITEM;
3669 menu->bTimeToHide = FALSE;
3671 TRACE("return %p\n", hMenu );
3673 return hMenu;
3677 /**********************************************************************
3678 * DestroyMenu (USER32.@)
3680 BOOL WINAPI DestroyMenu( HMENU hMenu )
3682 TRACE("(%p)\n", hMenu);
3684 /* Silently ignore attempts to destroy default system popup */
3686 if (hMenu && hMenu != MENU_DefSysPopup)
3688 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3690 if (!lppop) return FALSE;
3692 lppop->wMagic = 0; /* Mark it as destroyed */
3694 /* DestroyMenu should not destroy system menu popup owner */
3695 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
3697 DestroyWindow( lppop->hWnd );
3698 lppop->hWnd = 0;
3701 if (lppop->items) /* recursively destroy submenus */
3703 int i;
3704 MENUITEM *item = lppop->items;
3705 for (i = lppop->nItems; i > 0; i--, item++)
3707 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3708 MENU_FreeItemData( item );
3710 HeapFree( GetProcessHeap(), 0, lppop->items );
3712 USER_HEAP_FREE( hMenu );
3714 return (hMenu != MENU_DefSysPopup);
3718 /**********************************************************************
3719 * GetSystemMenu (USER32.@)
3721 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3723 WND *wndPtr = WIN_GetPtr( hWnd );
3724 HMENU retvalue = 0;
3726 if (wndPtr == WND_DESKTOP) return 0;
3727 if (wndPtr == WND_OTHER_PROCESS)
3729 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
3731 else if (wndPtr)
3733 if( wndPtr->hSysMenu )
3735 if( bRevert )
3737 DestroyMenu(wndPtr->hSysMenu);
3738 wndPtr->hSysMenu = 0;
3740 else
3742 POPUPMENU *menu = MENU_GetMenu( wndPtr->hSysMenu );
3743 if( menu )
3745 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
3746 menu->items[0].hSubMenu = MENU_CopySysPopup();
3748 else
3750 WARN("Current sys-menu (%p) of wnd %p is broken\n",
3751 wndPtr->hSysMenu, hWnd);
3752 wndPtr->hSysMenu = 0;
3757 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3758 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
3760 if( wndPtr->hSysMenu )
3762 POPUPMENU *menu;
3763 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
3765 /* Store the dummy sysmenu handle to facilitate the refresh */
3766 /* of the close button if the SC_CLOSE item change */
3767 menu = MENU_GetMenu(retvalue);
3768 if ( menu )
3769 menu->hSysMenuOwner = wndPtr->hSysMenu;
3771 WIN_ReleasePtr( wndPtr );
3773 return bRevert ? 0 : retvalue;
3777 /*******************************************************************
3778 * SetSystemMenu (USER32.@)
3780 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
3782 WND *wndPtr = WIN_GetPtr( hwnd );
3784 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
3786 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
3787 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3788 WIN_ReleasePtr( wndPtr );
3789 return TRUE;
3791 return FALSE;
3795 /**********************************************************************
3796 * GetMenu (USER32.@)
3798 HMENU WINAPI GetMenu( HWND hWnd )
3800 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
3801 TRACE("for %p returning %p\n", hWnd, retvalue);
3802 return retvalue;
3805 /**********************************************************************
3806 * GetMenuBarInfo (USER32.@)
3808 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
3810 FIXME( "(%p,0x%08lx,0x%08lx,%p)\n", hwnd, idObject, idItem, pmbi );
3811 return FALSE;
3814 /**********************************************************************
3815 * MENU_SetMenu
3817 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
3818 * SetWindowPos call that would result if SetMenu were called directly.
3820 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
3822 TRACE("(%p, %p);\n", hWnd, hMenu);
3824 if (hMenu && !IsMenu(hMenu))
3826 WARN("hMenu %p is not a menu handle\n", hMenu);
3827 return FALSE;
3829 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
3830 return FALSE;
3832 hWnd = WIN_GetFullHandle( hWnd );
3833 if (GetCapture() == hWnd) MENU_SetCapture(0); /* release the capture */
3835 if (hMenu != 0)
3837 LPPOPUPMENU lpmenu;
3839 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
3841 lpmenu->hWnd = hWnd;
3842 lpmenu->Height = 0; /* Make sure we recalculate the size */
3844 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
3845 return TRUE;
3849 /**********************************************************************
3850 * SetMenu (USER32.@)
3852 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
3854 if(!MENU_SetMenu(hWnd, hMenu))
3855 return FALSE;
3857 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3858 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3859 return TRUE;
3863 /**********************************************************************
3864 * GetSubMenu (USER32.@)
3866 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
3868 MENUITEM * lpmi;
3870 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return 0;
3871 if (!(lpmi->fType & MF_POPUP)) return 0;
3872 return lpmi->hSubMenu;
3876 /**********************************************************************
3877 * DrawMenuBar (USER32.@)
3879 BOOL WINAPI DrawMenuBar( HWND hWnd )
3881 LPPOPUPMENU lppop;
3882 HMENU hMenu = GetMenu(hWnd);
3884 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
3885 return FALSE;
3886 if (!hMenu || !(lppop = MENU_GetMenu( hMenu ))) return FALSE;
3888 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3889 lppop->hwndOwner = hWnd;
3890 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3891 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3892 return TRUE;
3895 /***********************************************************************
3896 * DrawMenuBarTemp (USER32.@)
3898 * UNDOCUMENTED !!
3900 * called by W98SE desk.cpl Control Panel Applet
3902 * Not 100% sure about the param names, but close.
3904 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
3906 LPPOPUPMENU lppop;
3907 UINT i,retvalue;
3908 HFONT hfontOld = 0;
3909 BOOL flat_menu = FALSE;
3911 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
3913 if (!hMenu)
3914 hMenu = GetMenu(hwnd);
3916 if (!hFont)
3917 hFont = hMenuFont;
3919 lppop = MENU_GetMenu( hMenu );
3920 if (lppop == NULL || lprect == NULL)
3922 retvalue = GetSystemMetrics(SM_CYMENU);
3923 goto END;
3926 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
3928 hfontOld = SelectObject( hDC, hFont);
3930 if (lppop->Height == 0)
3931 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
3933 lprect->bottom = lprect->top + lppop->Height;
3935 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
3937 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
3938 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
3939 LineTo( hDC, lprect->right, lprect->bottom );
3941 if (lppop->nItems == 0)
3943 retvalue = GetSystemMetrics(SM_CYMENU);
3944 goto END;
3947 for (i = 0; i < lppop->nItems; i++)
3949 MENU_DrawMenuItem( hwnd, hMenu, hwnd,
3950 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
3952 retvalue = lppop->Height;
3954 END:
3955 if (hfontOld) SelectObject (hDC, hfontOld);
3956 return retvalue;
3959 /***********************************************************************
3960 * EndMenu (USER.187)
3961 * EndMenu (USER32.@)
3963 void WINAPI EndMenu(void)
3965 /* if we are in the menu code, and it is active */
3966 if (!fEndMenu && top_popup)
3968 /* terminate the menu handling code */
3969 fEndMenu = TRUE;
3971 /* needs to be posted to wakeup the internal menu handler */
3972 /* which will now terminate the menu, in the event that */
3973 /* the main window was minimized, or lost focus, so we */
3974 /* don't end up with an orphaned menu */
3975 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
3980 /***********************************************************************
3981 * LookupMenuHandle (USER.217)
3983 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
3985 HMENU hmenu32 = HMENU_32(hmenu);
3986 UINT id32 = id;
3987 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3988 else return HMENU_16(hmenu32);
3992 /**********************************************************************
3993 * LoadMenu (USER.150)
3995 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, LPCSTR name )
3997 HRSRC16 hRsrc;
3998 HGLOBAL16 handle;
3999 HMENU16 hMenu;
4001 if (HIWORD(name) && name[0] == '#') name = (LPCSTR)atoi( name + 1 );
4002 if (!name) return 0;
4004 instance = GetExePtr( instance );
4005 if (!(hRsrc = FindResource16( instance, name, (LPSTR)RT_MENU ))) return 0;
4006 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
4007 hMenu = LoadMenuIndirect16(LockResource16(handle));
4008 FreeResource16( handle );
4009 return hMenu;
4013 /*****************************************************************
4014 * LoadMenuA (USER32.@)
4016 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4018 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
4019 if (!hrsrc) return 0;
4020 return LoadMenuIndirectA( (LPCVOID)LoadResource( instance, hrsrc ));
4024 /*****************************************************************
4025 * LoadMenuW (USER32.@)
4027 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4029 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
4030 if (!hrsrc) return 0;
4031 return LoadMenuIndirectW( (LPCVOID)LoadResource( instance, hrsrc ));
4035 /**********************************************************************
4036 * LoadMenuIndirect (USER.220)
4038 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4040 HMENU hMenu;
4041 WORD version, offset;
4042 LPCSTR p = (LPCSTR)template;
4044 TRACE("(%p)\n", template );
4045 version = GET_WORD(p);
4046 p += sizeof(WORD);
4047 if (version)
4049 WARN("version must be 0 for Win16\n" );
4050 return 0;
4052 offset = GET_WORD(p);
4053 p += sizeof(WORD) + offset;
4054 if (!(hMenu = CreateMenu())) return 0;
4055 if (!MENU_ParseResource( p, hMenu, FALSE ))
4057 DestroyMenu( hMenu );
4058 return 0;
4060 return HMENU_16(hMenu);
4064 /**********************************************************************
4065 * LoadMenuIndirectW (USER32.@)
4067 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4069 HMENU hMenu;
4070 WORD version, offset;
4071 LPCSTR p = (LPCSTR)template;
4073 version = GET_WORD(p);
4074 p += sizeof(WORD);
4075 TRACE("%p, ver %d\n", template, version );
4076 switch (version)
4078 case 0: /* standard format is version of 0 */
4079 offset = GET_WORD(p);
4080 p += sizeof(WORD) + offset;
4081 if (!(hMenu = CreateMenu())) return 0;
4082 if (!MENU_ParseResource( p, hMenu, TRUE ))
4084 DestroyMenu( hMenu );
4085 return 0;
4087 return hMenu;
4088 case 1: /* extended format is version of 1 */
4089 offset = GET_WORD(p);
4090 p += sizeof(WORD) + offset;
4091 if (!(hMenu = CreateMenu())) return 0;
4092 if (!MENUEX_ParseResource( p, hMenu))
4094 DestroyMenu( hMenu );
4095 return 0;
4097 return hMenu;
4098 default:
4099 ERR("version %d not supported.\n", version);
4100 return 0;
4105 /**********************************************************************
4106 * LoadMenuIndirectA (USER32.@)
4108 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4110 return LoadMenuIndirectW( template );
4114 /**********************************************************************
4115 * IsMenu (USER32.@)
4117 BOOL WINAPI IsMenu(HMENU hmenu)
4119 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4120 return menu != NULL;
4123 /**********************************************************************
4124 * GetMenuItemInfo_common
4127 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4128 LPMENUITEMINFOW lpmii, BOOL unicode)
4130 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4132 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4134 if (!menu)
4135 return FALSE;
4137 if (lpmii->fMask & MIIM_TYPE) {
4138 lpmii->fType = menu->fType;
4139 switch (MENU_ITEM_TYPE(menu->fType)) {
4140 case MF_STRING:
4141 break; /* will be done below */
4142 case MF_OWNERDRAW:
4143 case MF_BITMAP:
4144 lpmii->dwTypeData = menu->text;
4145 /* fall through */
4146 default:
4147 lpmii->cch = 0;
4151 /* copy the text string */
4152 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING)) &&
4153 (MENU_ITEM_TYPE(menu->fType) == MF_STRING) && menu->text)
4155 int len;
4156 if (unicode)
4158 len = strlenW(menu->text);
4159 if(lpmii->dwTypeData && lpmii->cch)
4160 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4162 else
4164 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL, 0, NULL, NULL );
4165 if(lpmii->dwTypeData && lpmii->cch)
4166 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4167 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4168 ((LPSTR)lpmii->dwTypeData)[lpmii->cch-1] = 0;
4170 /* if we've copied a substring we return its length */
4171 if(lpmii->dwTypeData && lpmii->cch)
4173 if (lpmii->cch <= len) lpmii->cch--;
4175 else /* return length of string */
4176 lpmii->cch = len;
4179 if (lpmii->fMask & MIIM_FTYPE)
4180 lpmii->fType = menu->fType;
4182 if (lpmii->fMask & MIIM_BITMAP)
4183 lpmii->hbmpItem = menu->hbmpItem;
4185 if (lpmii->fMask & MIIM_STATE)
4186 lpmii->fState = menu->fState;
4188 if (lpmii->fMask & MIIM_ID)
4189 lpmii->wID = menu->wID;
4191 if (lpmii->fMask & MIIM_SUBMENU)
4192 lpmii->hSubMenu = menu->hSubMenu;
4194 if (lpmii->fMask & MIIM_CHECKMARKS) {
4195 lpmii->hbmpChecked = menu->hCheckBit;
4196 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4198 if (lpmii->fMask & MIIM_DATA)
4199 lpmii->dwItemData = menu->dwItemData;
4201 return TRUE;
4204 /**********************************************************************
4205 * GetMenuItemInfoA (USER32.@)
4207 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4208 LPMENUITEMINFOA lpmii)
4210 return GetMenuItemInfo_common (hmenu, item, bypos,
4211 (LPMENUITEMINFOW)lpmii, FALSE);
4214 /**********************************************************************
4215 * GetMenuItemInfoW (USER32.@)
4217 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4218 LPMENUITEMINFOW lpmii)
4220 return GetMenuItemInfo_common (hmenu, item, bypos,
4221 lpmii, TRUE);
4225 /* set a menu item text from a ASCII or Unicode string */
4226 inline static void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4228 if (!text)
4230 menu->text = NULL;
4231 menu->fType |= MF_SEPARATOR;
4233 else if (unicode)
4235 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4236 strcpyW( menu->text, text );
4238 else
4240 LPCSTR str = (LPCSTR)text;
4241 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4242 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4243 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4248 /**********************************************************************
4249 * SetMenuItemInfo_common
4252 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4253 const MENUITEMINFOW *lpmii,
4254 BOOL unicode)
4256 if (!menu) return FALSE;
4258 debug_print_menuitem("MENU_SetItemInfo_common from: ", menu, "");
4260 if (lpmii->fMask & MIIM_TYPE ) {
4261 /* Get rid of old string. */
4262 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4263 HeapFree(GetProcessHeap(), 0, menu->text);
4264 menu->text = NULL;
4267 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4268 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4269 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4271 menu->text = lpmii->dwTypeData;
4273 if (IS_STRING_ITEM(menu->fType))
4274 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4277 if (lpmii->fMask & MIIM_FTYPE ) {
4278 /* free the string when the type is changing */
4279 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4280 HeapFree(GetProcessHeap(), 0, menu->text);
4281 menu->text = NULL;
4283 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4284 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4285 if ( IS_STRING_ITEM(menu->fType) && !menu->text )
4286 menu->fType |= MF_SEPARATOR;
4289 if (lpmii->fMask & MIIM_STRING ) {
4290 if (IS_STRING_ITEM(menu->fType)) {
4291 /* free the string when used */
4292 HeapFree(GetProcessHeap(), 0, menu->text);
4293 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4297 if (lpmii->fMask & MIIM_STATE)
4299 /* FIXME: MFS_DEFAULT do we have to reset the other menu items? */
4300 menu->fState = lpmii->fState;
4303 if (lpmii->fMask & MIIM_ID)
4304 menu->wID = lpmii->wID;
4306 if (lpmii->fMask & MIIM_SUBMENU) {
4307 menu->hSubMenu = lpmii->hSubMenu;
4308 if (menu->hSubMenu) {
4309 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4310 if (subMenu) {
4311 subMenu->wFlags |= MF_POPUP;
4312 menu->fType |= MF_POPUP;
4314 else
4315 /* FIXME: Return an error ? */
4316 menu->fType &= ~MF_POPUP;
4318 else
4319 menu->fType &= ~MF_POPUP;
4322 if (lpmii->fMask & MIIM_CHECKMARKS)
4324 if (lpmii->fType & MFT_RADIOCHECK)
4325 menu->fType |= MFT_RADIOCHECK;
4327 menu->hCheckBit = lpmii->hbmpChecked;
4328 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4330 if (lpmii->fMask & MIIM_DATA)
4331 menu->dwItemData = lpmii->dwItemData;
4333 if (lpmii->fMask & MIIM_BITMAP)
4334 menu->hbmpItem = lpmii->hbmpItem;
4336 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4337 return TRUE;
4340 /**********************************************************************
4341 * SetMenuItemInfoA (USER32.@)
4343 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4344 const MENUITEMINFOA *lpmii)
4346 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4347 (const MENUITEMINFOW *)lpmii, FALSE);
4350 /**********************************************************************
4351 * SetMenuItemInfoW (USER32.@)
4353 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4354 const MENUITEMINFOW *lpmii)
4356 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4357 lpmii, TRUE);
4360 /**********************************************************************
4361 * SetMenuDefaultItem (USER32.@)
4364 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4366 UINT i;
4367 POPUPMENU *menu;
4368 MENUITEM *item;
4370 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4372 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4374 /* reset all default-item flags */
4375 item = menu->items;
4376 for (i = 0; i < menu->nItems; i++, item++)
4378 item->fState &= ~MFS_DEFAULT;
4381 /* no default item */
4382 if ( -1 == uItem)
4384 return TRUE;
4387 item = menu->items;
4388 if ( bypos )
4390 if ( uItem >= menu->nItems ) return FALSE;
4391 item[uItem].fState |= MFS_DEFAULT;
4392 return TRUE;
4394 else
4396 for (i = 0; i < menu->nItems; i++, item++)
4398 if (item->wID == uItem)
4400 item->fState |= MFS_DEFAULT;
4401 return TRUE;
4406 return FALSE;
4409 /**********************************************************************
4410 * GetMenuDefaultItem (USER32.@)
4412 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4414 POPUPMENU *menu;
4415 MENUITEM * item;
4416 UINT i = 0;
4418 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
4420 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4422 /* find default item */
4423 item = menu->items;
4425 /* empty menu */
4426 if (! item) return -1;
4428 while ( !( item->fState & MFS_DEFAULT ) )
4430 i++; item++;
4431 if (i >= menu->nItems ) return -1;
4434 /* default: don't return disabled items */
4435 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4437 /* search rekursiv when needed */
4438 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4440 UINT ret;
4441 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4442 if ( -1 != ret ) return ret;
4444 /* when item not found in submenu, return the popup item */
4446 return ( bypos ) ? i : item->wID;
4451 /**********************************************************************
4452 * InsertMenuItemA (USER32.@)
4454 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4455 const MENUITEMINFOA *lpmii)
4457 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4458 return SetMenuItemInfo_common(item, (const MENUITEMINFOW *)lpmii, FALSE);
4462 /**********************************************************************
4463 * InsertMenuItemW (USER32.@)
4465 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4466 const MENUITEMINFOW *lpmii)
4468 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4469 return SetMenuItemInfo_common(item, lpmii, TRUE);
4472 /**********************************************************************
4473 * CheckMenuRadioItem (USER32.@)
4476 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4477 UINT first, UINT last, UINT check,
4478 UINT bypos)
4480 MENUITEM *mifirst, *milast, *micheck;
4481 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4483 TRACE("%p: %d-%d, check %d, bypos=%d\n", hMenu, first, last, check, bypos);
4485 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4486 milast = MENU_FindItem (&mlast, &last, bypos);
4487 micheck = MENU_FindItem (&mcheck, &check, bypos);
4489 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4490 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4491 micheck > milast || micheck < mifirst)
4492 return FALSE;
4494 while (mifirst <= milast)
4496 if (mifirst == micheck)
4498 mifirst->fType |= MFT_RADIOCHECK;
4499 mifirst->fState |= MFS_CHECKED;
4500 } else {
4501 mifirst->fType &= ~MFT_RADIOCHECK;
4502 mifirst->fState &= ~MFS_CHECKED;
4504 mifirst++;
4507 return TRUE;
4511 /**********************************************************************
4512 * GetMenuItemRect (USER32.@)
4514 * ATTENTION: Here, the returned values in rect are the screen
4515 * coordinates of the item just like if the menu was
4516 * always on the upper left side of the application.
4519 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4520 LPRECT rect)
4522 POPUPMENU *itemMenu;
4523 MENUITEM *item;
4524 HWND referenceHwnd;
4526 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
4528 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4529 referenceHwnd = hwnd;
4531 if(!hwnd)
4533 itemMenu = MENU_GetMenu(hMenu);
4534 if (itemMenu == NULL)
4535 return FALSE;
4537 if(itemMenu->hWnd == 0)
4538 return FALSE;
4539 referenceHwnd = itemMenu->hWnd;
4542 if ((rect == NULL) || (item == NULL))
4543 return FALSE;
4545 *rect = item->rect;
4547 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4549 return TRUE;
4553 /**********************************************************************
4554 * SetMenuInfo (USER32.@)
4556 * FIXME
4557 * MIM_APPLYTOSUBMENUS
4558 * actually use the items to draw the menu
4560 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4562 POPUPMENU *menu;
4564 TRACE("(%p %p)\n", hMenu, lpmi);
4566 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
4569 if (lpmi->fMask & MIM_BACKGROUND)
4570 menu->hbrBack = lpmi->hbrBack;
4572 if (lpmi->fMask & MIM_HELPID)
4573 menu->dwContextHelpID = lpmi->dwContextHelpID;
4575 if (lpmi->fMask & MIM_MAXHEIGHT)
4576 menu->cyMax = lpmi->cyMax;
4578 if (lpmi->fMask & MIM_MENUDATA)
4579 menu->dwMenuData = lpmi->dwMenuData;
4581 if (lpmi->fMask & MIM_STYLE)
4583 menu->dwStyle = lpmi->dwStyle;
4584 if (menu->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
4585 if (menu->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
4586 if (menu->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
4587 if (menu->dwStyle & MNS_NOCHECK) FIXME("MNS_NOCHECK unimplemented\n");
4588 if (menu->dwStyle & MNS_NOTIFYBYPOS) FIXME("MNS_NOTIFYBYPOS unimplemented\n");
4591 return TRUE;
4593 return FALSE;
4596 /**********************************************************************
4597 * GetMenuInfo (USER32.@)
4599 * NOTES
4600 * win98/NT5.0
4603 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4604 { POPUPMENU *menu;
4606 TRACE("(%p %p)\n", hMenu, lpmi);
4608 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4611 if (lpmi->fMask & MIM_BACKGROUND)
4612 lpmi->hbrBack = menu->hbrBack;
4614 if (lpmi->fMask & MIM_HELPID)
4615 lpmi->dwContextHelpID = menu->dwContextHelpID;
4617 if (lpmi->fMask & MIM_MAXHEIGHT)
4618 lpmi->cyMax = menu->cyMax;
4620 if (lpmi->fMask & MIM_MENUDATA)
4621 lpmi->dwMenuData = menu->dwMenuData;
4623 if (lpmi->fMask & MIM_STYLE)
4624 lpmi->dwStyle = menu->dwStyle;
4626 return TRUE;
4628 return FALSE;
4632 /**********************************************************************
4633 * SetMenuContextHelpId (USER32.@)
4635 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4637 LPPOPUPMENU menu;
4639 TRACE("(%p 0x%08lx)\n", hMenu, dwContextHelpID);
4641 if ((menu = MENU_GetMenu(hMenu)))
4643 menu->dwContextHelpID = dwContextHelpID;
4644 return TRUE;
4646 return FALSE;
4650 /**********************************************************************
4651 * GetMenuContextHelpId (USER32.@)
4653 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4655 LPPOPUPMENU menu;
4657 TRACE("(%p)\n", hMenu);
4659 if ((menu = MENU_GetMenu(hMenu)))
4661 return menu->dwContextHelpID;
4663 return 0;
4666 /**********************************************************************
4667 * MenuItemFromPoint (USER32.@)
4669 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4671 POPUPMENU *menu = MENU_GetMenu(hMenu);
4672 UINT pos;
4674 /*FIXME: Do we have to handle hWnd here? */
4675 if (!menu) return -1;
4676 if (!MENU_FindItemByCoords(menu, ptScreen, &pos)) return -1;
4677 return pos;
4681 /**********************************************************************
4682 * translate_accelerator
4684 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
4685 BYTE fVirt, WORD key, WORD cmd )
4687 INT mask = 0;
4688 UINT mesg = 0;
4690 if (wParam != key) return FALSE;
4692 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
4693 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
4694 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
4696 if (message == WM_CHAR || message == WM_SYSCHAR)
4698 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
4700 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", wParam & 0xff);
4701 goto found;
4704 else
4706 if(fVirt & FVIRTKEY)
4708 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
4709 wParam, 0xff & HIWORD(lParam));
4711 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
4712 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
4714 else
4716 if (!(lParam & 0x01000000)) /* no special_key */
4718 if ((fVirt & FALT) && (lParam & 0x20000000))
4719 { /* ^^ ALT pressed */
4720 TRACE_(accel)("found accel for Alt-%c\n", wParam & 0xff);
4721 goto found;
4726 return FALSE;
4728 found:
4729 if (message == WM_KEYUP || message == WM_SYSKEYUP)
4730 mesg = 1;
4731 else
4733 HMENU hMenu, hSubMenu, hSysMenu;
4734 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
4736 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
4737 hSysMenu = get_win_sys_menu( hWnd );
4739 /* find menu item and ask application to initialize it */
4740 /* 1. in the system menu */
4741 hSubMenu = hSysMenu;
4742 nPos = cmd;
4743 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
4745 if (GetCapture())
4746 mesg = 2;
4747 if (!IsWindowEnabled(hWnd))
4748 mesg = 3;
4749 else
4751 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
4752 if(hSubMenu != hSysMenu)
4754 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
4755 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
4756 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
4758 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
4761 else /* 2. in the window's menu */
4763 hSubMenu = hMenu;
4764 nPos = cmd;
4765 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
4767 if (GetCapture())
4768 mesg = 2;
4769 if (!IsWindowEnabled(hWnd))
4770 mesg = 3;
4771 else
4773 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
4774 if(hSubMenu != hMenu)
4776 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
4777 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
4778 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
4780 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
4785 if (mesg == 0)
4787 if (uSysStat != (UINT)-1)
4789 if (uSysStat & (MF_DISABLED|MF_GRAYED))
4790 mesg=4;
4791 else
4792 mesg=WM_SYSCOMMAND;
4794 else
4796 if (uStat != (UINT)-1)
4798 if (IsIconic(hWnd))
4799 mesg=5;
4800 else
4802 if (uStat & (MF_DISABLED|MF_GRAYED))
4803 mesg=6;
4804 else
4805 mesg=WM_COMMAND;
4808 else
4809 mesg=WM_COMMAND;
4814 if( mesg==WM_COMMAND )
4816 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
4817 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
4819 else if( mesg==WM_SYSCOMMAND )
4821 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
4822 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
4824 else
4826 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
4827 * #0: unknown (please report!)
4828 * #1: for WM_KEYUP,WM_SYSKEYUP
4829 * #2: mouse is captured
4830 * #3: window is disabled
4831 * #4: it's a disabled system menu option
4832 * #5: it's a menu option, but window is iconic
4833 * #6: it's a menu option, but disabled
4835 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
4836 if(mesg==0)
4837 ERR_(accel)(" unknown reason - please report!\n");
4839 return TRUE;
4842 /**********************************************************************
4843 * TranslateAccelerator (USER32.@)
4844 * TranslateAcceleratorA (USER32.@)
4846 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
4848 /* YES, Accel16! */
4849 LPACCEL16 lpAccelTbl;
4850 int i;
4851 WPARAM wParam;
4853 if (!hWnd || !msg) return 0;
4855 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
4857 WARN_(accel)("invalid accel handle=%p\n", hAccel);
4858 return 0;
4861 wParam = msg->wParam;
4863 switch (msg->message)
4865 case WM_KEYDOWN:
4866 case WM_SYSKEYDOWN:
4867 break;
4869 case WM_CHAR:
4870 case WM_SYSCHAR:
4872 char ch = LOWORD(wParam);
4873 WCHAR wch;
4874 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
4875 wParam = MAKEWPARAM(wch, HIWORD(wParam));
4877 break;
4879 default:
4880 return 0;
4883 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08x, lParam %08lx\n",
4884 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
4885 i = 0;
4888 if (translate_accelerator( hWnd, msg->message, wParam, msg->lParam,
4889 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
4890 return 1;
4891 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
4893 return 0;
4896 /**********************************************************************
4897 * TranslateAcceleratorW (USER32.@)
4899 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
4901 /* YES, Accel16! */
4902 LPACCEL16 lpAccelTbl;
4903 int i;
4905 if (!hWnd || !msg) return 0;
4907 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
4909 WARN_(accel)("invalid accel handle=%p\n", hAccel);
4910 return 0;
4913 switch (msg->message)
4915 case WM_KEYDOWN:
4916 case WM_SYSKEYDOWN:
4917 case WM_CHAR:
4918 case WM_SYSCHAR:
4919 break;
4921 default:
4922 return 0;
4925 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08x, lParam %08lx\n",
4926 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
4927 i = 0;
4930 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
4931 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
4932 return 1;
4933 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
4935 return 0;