rpcrt4: Store all active connections in RpcServerProtseq.
[wine.git] / dlls / user32 / menu.c
bloba65f01eaa5eed562fa8a6cfc34886982adb04732
1 /*
2 * Menu functions
4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
7 * Copyright 2005 Maxime Bellengé
8 * Copyright 2006 Phil Krylov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Note: the style MF_MOUSESELECT is used to mark popup items that
27 * have been selected, i.e. their popup menu is currently displayed.
28 * This is probably not the meaning this style has in MS-Windows.
30 * Note 2: where there is a difference, these menu API's are according
31 * the behavior of Windows 2k and Windows XP. Known differences with
32 * Windows 9x/ME are documented in the comments, in case an application
33 * is found to depend on the old behavior.
35 * TODO:
36 * implements styles :
37 * - MNS_AUTODISMISS
38 * - MNS_DRAGDROP
39 * - MNS_MODELESS
42 #include "config.h"
43 #include "wine/port.h"
45 #include <stdarg.h>
46 #include <string.h>
48 #define OEMRESOURCE
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winnls.h"
54 #include "wine/server.h"
55 #include "wine/unicode.h"
56 #include "wine/exception.h"
57 #include "win.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(menu);
63 WINE_DECLARE_DEBUG_CHANNEL(accel);
65 /* Menu item structure */
66 typedef struct {
67 /* ----------- MENUITEMINFO Stuff ----------- */
68 UINT fType; /* Item type. */
69 UINT fState; /* Item state. */
70 UINT_PTR wID; /* Item id. */
71 HMENU hSubMenu; /* Pop-up menu. */
72 HBITMAP hCheckBit; /* Bitmap when checked. */
73 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
74 LPWSTR text; /* Item text. */
75 ULONG_PTR dwItemData; /* Application defined. */
76 LPWSTR dwTypeData; /* depends on fMask */
77 HBITMAP hbmpItem; /* bitmap */
78 /* ----------- Wine stuff ----------- */
79 RECT rect; /* Item area (relative to the items_rect).
80 * See MENU_AdjustMenuItemRect(). */
81 UINT xTab; /* X position of text after Tab */
82 SIZE bmpsize; /* size needed for the HBMMENU_CALLBACK
83 * bitmap */
84 } MENUITEM;
86 /* Popup menu structure */
87 typedef struct {
88 struct user_object obj;
89 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
90 WORD Width; /* Width of the whole menu */
91 WORD Height; /* Height of the whole menu */
92 UINT nItems; /* Number of items in the menu */
93 HWND hWnd; /* Window containing the menu */
94 MENUITEM *items; /* Array of menu items */
95 UINT FocusedItem; /* Currently focused item */
96 HWND hwndOwner; /* window receiving the messages for ownerdraw */
97 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
98 BOOL bScrolling; /* Scroll arrows are active */
99 UINT nScrollPos; /* Current scroll position */
100 UINT nTotalHeight; /* Total height of menu items inside menu */
101 RECT items_rect; /* Rectangle within which the items lie. Excludes margins and scroll arrows */
102 /* ------------ MENUINFO members ------ */
103 DWORD dwStyle; /* Extended menu style */
104 UINT cyMax; /* max height of the whole menu, 0 is screen height */
105 HBRUSH hbrBack; /* brush for menu background */
106 DWORD dwContextHelpID;
107 DWORD dwMenuData; /* application defined value */
108 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
109 WORD textOffset; /* Offset of text when items have both bitmaps and text */
110 } POPUPMENU, *LPPOPUPMENU;
112 /* internal flags for menu tracking */
114 #define TF_ENDMENU 0x10000
115 #define TF_SUSPENDPOPUP 0x20000
116 #define TF_SKIPREMOVE 0x40000
118 typedef struct
120 UINT trackFlags;
121 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
122 HMENU hTopMenu; /* initial menu */
123 HWND hOwnerWnd; /* where notifications are sent */
124 POINT pt;
125 } MTRACKER;
127 #define ITEM_PREV -1
128 #define ITEM_NEXT 1
130 /* Internal MENU_TrackMenu() flags */
131 #define TPM_INTERNAL 0xF0000000
132 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
133 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
135 /* Space between 2 columns */
136 #define MENU_COL_SPACE 4
138 /* Margins for popup menus */
139 #define MENU_MARGIN 3
141 /* maximum allowed depth of any branch in the menu tree.
142 * This value is slightly larger than in windows (25) to
143 * stay on the safe side. */
144 #define MAXMENUDEPTH 30
146 /* (other menu->FocusedItem values give the position of the focused item) */
147 #define NO_SELECTED_ITEM 0xffff
149 #define MENU_ITEM_TYPE(flags) \
150 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
152 /* macro to test that flags do not indicate bitmap, ownerdraw or separator */
153 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
154 #define IS_MAGIC_BITMAP(id) ((id) && ((INT_PTR)(id) < 12) && ((INT_PTR)(id) >= -1))
156 #define IS_SYSTEM_MENU(menu) \
157 (!((menu)->wFlags & MF_POPUP) && ((menu)->wFlags & MF_SYSMENU))
159 #define MENUITEMINFO_TYPE_MASK \
160 (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
161 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
162 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY /* same as MF_HELP */ )
163 #define TYPE_MASK (MENUITEMINFO_TYPE_MASK | MF_POPUP | MF_SYSMENU)
164 #define STATE_MASK (~TYPE_MASK)
165 #define MENUITEMINFO_STATE_MASK (STATE_MASK & ~(MF_BYPOSITION | MF_MOUSESELECT))
167 #define WIN_ALLOWED_MENU(style) ((style & (WS_CHILD | WS_POPUP)) != WS_CHILD)
169 static SIZE menucharsize;
170 static UINT ODitemheight; /* default owner drawn item height */
172 /* Use global popup window because there's no way 2 menus can
173 * be tracked at the same time. */
174 static HWND top_popup;
175 static HMENU top_popup_hmenu;
177 /* Flag set by EndMenu() to force an exit from menu tracking */
178 static BOOL fEndMenu = FALSE;
180 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont);
182 static BOOL SetMenuItemInfo_common( MENUITEM *, const MENUITEMINFOW *, BOOL);
184 /*********************************************************************
185 * menu class descriptor
187 const struct builtin_class_descr MENU_builtin_class =
189 (LPCWSTR)POPUPMENU_CLASS_ATOM, /* name */
190 CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, /* style */
191 WINPROC_MENU, /* proc */
192 sizeof(HMENU), /* extra */
193 IDC_ARROW, /* cursor */
194 (HBRUSH)(COLOR_MENU+1) /* brush */
198 /***********************************************************************
199 * debug_print_menuitem
201 * Print a menuitem in readable form.
204 #define debug_print_menuitem(pre, mp, post) \
205 do { if (TRACE_ON(menu)) do_debug_print_menuitem(pre, mp, post); } while (0)
207 #define MENUOUT(text) \
208 TRACE("%s%s", (count++ ? "," : ""), (text))
210 #define MENUFLAG(bit,text) \
211 do { \
212 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
213 } while (0)
215 static void do_debug_print_menuitem(const char *prefix, const MENUITEM *mp,
216 const char *postfix)
218 static const char * const hbmmenus[] = { "HBMMENU_CALLBACK", "", "HBMMENU_SYSTEM",
219 "HBMMENU_MBAR_RESTORE", "HBMMENU_MBAR_MINIMIZE", "UNKNOWN BITMAP", "HBMMENU_MBAR_CLOSE",
220 "HBMMENU_MBAR_CLOSE_D", "HBMMENU_MBAR_MINIMIZE_D", "HBMMENU_POPUP_CLOSE",
221 "HBMMENU_POPUP_RESTORE", "HBMMENU_POPUP_MAXIMIZE", "HBMMENU_POPUP_MINIMIZE"};
222 TRACE("%s ", prefix);
223 if (mp) {
224 UINT flags = mp->fType;
225 TRACE( "{ ID=0x%lx", mp->wID);
226 if ( mp->hSubMenu)
227 TRACE( ", Sub=%p", mp->hSubMenu);
228 if (flags) {
229 int count = 0;
230 TRACE( ", fType=");
231 MENUFLAG( MFT_SEPARATOR, "sep");
232 MENUFLAG( MFT_OWNERDRAW, "own");
233 MENUFLAG( MFT_BITMAP, "bit");
234 MENUFLAG(MF_POPUP, "pop");
235 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
236 MENUFLAG(MFT_MENUBREAK, "brk");
237 MENUFLAG(MFT_RADIOCHECK, "radio");
238 MENUFLAG(MFT_RIGHTORDER, "rorder");
239 MENUFLAG(MF_SYSMENU, "sys");
240 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
241 if (flags)
242 TRACE( "+0x%x", flags);
244 flags = mp->fState;
245 if (flags) {
246 int count = 0;
247 TRACE( ", State=");
248 MENUFLAG(MFS_GRAYED, "grey");
249 MENUFLAG(MFS_DEFAULT, "default");
250 MENUFLAG(MFS_DISABLED, "dis");
251 MENUFLAG(MFS_CHECKED, "check");
252 MENUFLAG(MFS_HILITE, "hi");
253 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
254 MENUFLAG(MF_MOUSESELECT, "mouse");
255 if (flags)
256 TRACE( "+0x%x", flags);
258 if (mp->hCheckBit)
259 TRACE( ", Chk=%p", mp->hCheckBit);
260 if (mp->hUnCheckBit)
261 TRACE( ", Unc=%p", mp->hUnCheckBit);
262 if (mp->text)
263 TRACE( ", Text=%s", debugstr_w(mp->text));
264 if (mp->dwItemData)
265 TRACE( ", ItemData=0x%08lx", mp->dwItemData);
266 if (mp->hbmpItem)
268 if( IS_MAGIC_BITMAP(mp->hbmpItem))
269 TRACE( ", hbitmap=%s", hbmmenus[ (INT_PTR)mp->hbmpItem + 1]);
270 else
271 TRACE( ", hbitmap=%p", mp->hbmpItem);
273 TRACE( " }");
274 } else
275 TRACE( "NULL");
276 TRACE(" %s\n", postfix);
279 #undef MENUOUT
280 #undef MENUFLAG
283 /***********************************************************************
284 * MENU_GetMenu
286 * Validate the given menu handle and returns the menu structure pointer.
288 static POPUPMENU *MENU_GetMenu(HMENU hMenu)
290 POPUPMENU *menu = get_user_handle_ptr( hMenu, USER_MENU );
292 if (menu == OBJ_OTHER_PROCESS)
294 WARN( "other process menu %p?\n", hMenu);
295 return NULL;
297 if (menu) release_user_handle_ptr( menu ); /* FIXME! */
298 else WARN("invalid menu handle=%p\n", hMenu);
299 return menu;
302 /***********************************************************************
303 * get_win_sys_menu
305 * Get the system menu of a window
307 static HMENU get_win_sys_menu( HWND hwnd )
309 HMENU ret = 0;
310 WND *win = WIN_GetPtr( hwnd );
311 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
313 ret = win->hSysMenu;
314 WIN_ReleasePtr( win );
316 return ret;
319 /***********************************************************************
320 * get_menu_font
322 static HFONT get_menu_font( BOOL bold )
324 static HFONT hMenuFont, hMenuFontBold;
326 HFONT ret = bold ? hMenuFontBold : hMenuFont;
328 if (!ret)
330 NONCLIENTMETRICSW ncm;
331 HFONT prev;
333 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
334 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0);
336 if (bold)
338 ncm.lfMenuFont.lfWeight += 300;
339 if (ncm.lfMenuFont.lfWeight > 1000) ncm.lfMenuFont.lfWeight = 1000;
341 if (!(ret = CreateFontIndirectW( &ncm.lfMenuFont ))) return 0;
342 prev = InterlockedCompareExchangePointer( (void **)(bold ? &hMenuFontBold : &hMenuFont),
343 ret, NULL );
344 if (prev)
346 /* another thread beat us to it */
347 DeleteObject( ret );
348 ret = prev;
351 return ret;
354 /***********************************************************************
355 * get_arrow_bitmap
357 static HBITMAP get_arrow_bitmap(void)
359 static HBITMAP arrow_bitmap;
361 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_MNARROW));
362 return arrow_bitmap;
365 static inline UINT get_scroll_arrow_height(const POPUPMENU *menu)
367 return menucharsize.cy + 4;
370 /***********************************************************************
371 * MENU_CopySysPopup
373 * Return the default system menu.
375 static HMENU MENU_CopySysPopup(BOOL mdi)
377 static const WCHAR sysmenuW[] = {'S','Y','S','M','E','N','U',0};
378 static const WCHAR sysmenumdiW[] = {'S','Y','S','M','E','N','U','M','D','I',0};
379 HMENU hMenu = LoadMenuW(user32_module, (mdi ? sysmenumdiW : sysmenuW));
381 if( hMenu ) {
382 MENUINFO minfo;
383 MENUITEMINFOW miteminfo;
384 POPUPMENU* menu = MENU_GetMenu(hMenu);
385 menu->wFlags |= MF_SYSMENU | MF_POPUP;
386 /* decorate the menu with bitmaps */
387 minfo.cbSize = sizeof( MENUINFO);
388 minfo.dwStyle = MNS_CHECKORBMP;
389 minfo.fMask = MIM_STYLE;
390 SetMenuInfo( hMenu, &minfo);
391 miteminfo.cbSize = sizeof( MENUITEMINFOW);
392 miteminfo.fMask = MIIM_BITMAP;
393 miteminfo.hbmpItem = HBMMENU_POPUP_CLOSE;
394 SetMenuItemInfoW( hMenu, SC_CLOSE, FALSE, &miteminfo);
395 miteminfo.hbmpItem = HBMMENU_POPUP_RESTORE;
396 SetMenuItemInfoW( hMenu, SC_RESTORE, FALSE, &miteminfo);
397 miteminfo.hbmpItem = HBMMENU_POPUP_MAXIMIZE;
398 SetMenuItemInfoW( hMenu, SC_MAXIMIZE, FALSE, &miteminfo);
399 miteminfo.hbmpItem = HBMMENU_POPUP_MINIMIZE;
400 SetMenuItemInfoW( hMenu, SC_MINIMIZE, FALSE, &miteminfo);
401 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
403 else
404 ERR("Unable to load default system menu\n" );
406 TRACE("returning %p (mdi=%d).\n", hMenu, mdi );
408 return hMenu;
412 /**********************************************************************
413 * MENU_GetSysMenu
415 * Create a copy of the system menu. System menu in Windows is
416 * a special menu bar with the single entry - system menu popup.
417 * This popup is presented to the outside world as a "system menu".
418 * However, the real system menu handle is sometimes seen in the
419 * WM_MENUSELECT parameters (and Word 6 likes it this way).
421 static HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
423 HMENU hMenu;
425 TRACE("loading system menu, hWnd %p, hPopupMenu %p\n", hWnd, hPopupMenu);
426 if ((hMenu = CreateMenu()))
428 POPUPMENU *menu = MENU_GetMenu(hMenu);
429 menu->wFlags = MF_SYSMENU;
430 menu->hWnd = WIN_GetFullHandle( hWnd );
431 TRACE("hWnd %p (hMenu %p)\n", menu->hWnd, hMenu);
433 if (!hPopupMenu)
435 if (GetWindowLongW(hWnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
436 hPopupMenu = MENU_CopySysPopup(TRUE);
437 else
438 hPopupMenu = MENU_CopySysPopup(FALSE);
441 if (hPopupMenu)
443 if (GetClassLongW(hWnd, GCL_STYLE) & CS_NOCLOSE)
444 DeleteMenu(hPopupMenu, SC_CLOSE, MF_BYCOMMAND);
446 InsertMenuW( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION,
447 (UINT_PTR)hPopupMenu, NULL );
449 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
450 menu->items[0].fState = 0;
451 if ((menu = MENU_GetMenu(hPopupMenu))) menu->wFlags |= MF_SYSMENU;
453 TRACE("hMenu=%p (hPopup %p)\n", hMenu, hPopupMenu );
454 return hMenu;
456 DestroyMenu( hMenu );
458 ERR("failed to load system menu!\n");
459 return 0;
463 /***********************************************************************
464 * MENU_InitSysMenuPopup
466 * Grey the appropriate items in System menu.
468 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
470 BOOL gray;
472 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
473 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
474 gray = ((style & WS_MAXIMIZE) != 0);
475 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
476 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
477 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
478 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
479 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
480 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
481 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
482 gray = (clsStyle & CS_NOCLOSE) != 0;
484 /* The menu item must keep its state if it's disabled */
485 if(gray)
486 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
490 /******************************************************************************
492 * UINT MENU_GetStartOfNextColumn(
493 * HMENU hMenu )
495 *****************************************************************************/
497 static UINT MENU_GetStartOfNextColumn(
498 HMENU hMenu )
500 POPUPMENU *menu = MENU_GetMenu(hMenu);
501 UINT i;
503 if(!menu)
504 return NO_SELECTED_ITEM;
506 i = menu->FocusedItem + 1;
507 if( i == NO_SELECTED_ITEM )
508 return i;
510 for( ; i < menu->nItems; ++i ) {
511 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
512 return i;
515 return NO_SELECTED_ITEM;
519 /******************************************************************************
521 * UINT MENU_GetStartOfPrevColumn(
522 * HMENU hMenu )
524 *****************************************************************************/
526 static UINT MENU_GetStartOfPrevColumn(
527 HMENU hMenu )
529 POPUPMENU *menu = MENU_GetMenu(hMenu);
530 UINT i;
532 if( !menu )
533 return NO_SELECTED_ITEM;
535 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
536 return NO_SELECTED_ITEM;
538 /* Find the start of the column */
540 for(i = menu->FocusedItem; i != 0 &&
541 !(menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK));
542 --i); /* empty */
544 if(i == 0)
545 return NO_SELECTED_ITEM;
547 for(--i; i != 0; --i) {
548 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
549 break;
552 TRACE("ret %d.\n", i );
554 return i;
559 /***********************************************************************
560 * MENU_FindItem
562 * Find a menu item. Return a pointer on the item, and modifies *hmenu
563 * in case the item was in a sub-menu.
565 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
567 POPUPMENU *menu;
568 MENUITEM *fallback = NULL;
569 UINT fallback_pos = 0;
570 UINT i;
572 if ((*hmenu == (HMENU)0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
573 if (wFlags & MF_BYPOSITION)
575 if (*nPos >= menu->nItems) return NULL;
576 return &menu->items[*nPos];
578 else
580 MENUITEM *item = menu->items;
581 for (i = 0; i < menu->nItems; i++, item++)
583 if (item->fType & MF_POPUP)
585 HMENU hsubmenu = item->hSubMenu;
586 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
587 if (subitem)
589 *hmenu = hsubmenu;
590 return subitem;
592 else if (item->wID == *nPos)
594 /* fallback to this item if nothing else found */
595 fallback_pos = i;
596 fallback = item;
599 else if (item->wID == *nPos)
601 *nPos = i;
602 return item;
607 if (fallback)
608 *nPos = fallback_pos;
610 return fallback;
613 /***********************************************************************
614 * MENU_FindSubMenu
616 * Find a Sub menu. Return the position of the submenu, and modifies
617 * *hmenu in case it is found in another sub-menu.
618 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
620 static UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
622 POPUPMENU *menu;
623 UINT i;
624 MENUITEM *item;
625 if (((*hmenu)==(HMENU)0xffff) ||
626 (!(menu = MENU_GetMenu(*hmenu))))
627 return NO_SELECTED_ITEM;
628 item = menu->items;
629 for (i = 0; i < menu->nItems; i++, item++) {
630 if(!(item->fType & MF_POPUP)) continue;
631 if (item->hSubMenu == hSubTarget) {
632 return i;
634 else {
635 HMENU hsubmenu = item->hSubMenu;
636 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
637 if (pos != NO_SELECTED_ITEM) {
638 *hmenu = hsubmenu;
639 return pos;
643 return NO_SELECTED_ITEM;
646 /***********************************************************************
647 * MENU_FreeItemData
649 static void MENU_FreeItemData( MENUITEM* item )
651 /* delete text */
652 HeapFree( GetProcessHeap(), 0, item->text );
655 /***********************************************************************
656 * MENU_AdjustMenuItemRect
658 * Adjust menu item rectangle according to scrolling state.
660 static void
661 MENU_AdjustMenuItemRect(const POPUPMENU *menu, LPRECT rect)
663 INT scroll_offset = menu->bScrolling ? menu->nScrollPos : 0;
665 OffsetRect( rect, menu->items_rect.left, menu->items_rect.top - scroll_offset );
668 enum hittest
670 ht_nowhere, /* outside the menu */
671 ht_border, /* anywhere that's not an item or a scroll arrow */
672 ht_item, /* a menu item */
673 ht_scroll_up, /* scroll up arrow */
674 ht_scroll_down /* scroll down arrow */
677 /***********************************************************************
678 * MENU_FindItemByCoords
680 * Find the item at the specified coordinates (screen coords). Does
681 * not work for child windows and therefore should not be called for
682 * an arbitrary system menu.
684 * Returns a hittest code. *pos will contain the position of the
685 * item or NO_SELECTED_ITEM. If the hittest code is ht_scroll_up
686 * or ht_scroll_down then *pos will contain the position of the
687 * item that's just outside the items_rect - ie, the one that would
688 * be scrolled completely into view.
690 static enum hittest MENU_FindItemByCoords( const POPUPMENU *menu, POINT pt, UINT *pos )
692 MENUITEM *item;
693 UINT i;
694 RECT rect;
695 enum hittest ht = ht_border;
697 *pos = NO_SELECTED_ITEM;
699 if (!GetWindowRect(menu->hWnd, &rect) || !PtInRect(&rect, pt))
700 return ht_nowhere;
702 if (GetWindowLongW( menu->hWnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) pt.x = rect.right - 1 - pt.x;
703 else pt.x -= rect.left;
704 pt.y -= rect.top;
706 if (!PtInRect(&menu->items_rect, pt))
708 if (!menu->bScrolling || pt.x < menu->items_rect.left || pt.x >= menu->items_rect.right)
709 return ht_border;
711 /* On a scroll arrow. Update pt so that it points to the item just outside items_rect */
712 if (pt.y < menu->items_rect.top)
714 ht = ht_scroll_up;
715 pt.y = menu->items_rect.top - 1;
717 else
719 ht = ht_scroll_down;
720 pt.y = menu->items_rect.bottom;
724 item = menu->items;
725 for (i = 0; i < menu->nItems; i++, item++)
727 rect = item->rect;
728 MENU_AdjustMenuItemRect(menu, &rect);
729 if (PtInRect(&rect, pt))
731 *pos = i;
732 if (ht != ht_scroll_up && ht != ht_scroll_down) ht = ht_item;
733 break;
737 return ht;
741 /***********************************************************************
742 * MENU_FindItemByKey
744 * Find the menu item selected by a key press.
745 * Return item id, -1 if none, -2 if we should close the menu.
747 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
748 WCHAR key, BOOL forceMenuChar )
750 TRACE("\tlooking for '%c' (0x%02x) in [%p]\n", (char)key, key, hmenu );
752 if (!IsMenu( hmenu )) hmenu = GetSubMenu( get_win_sys_menu(hwndOwner), 0);
754 if (hmenu)
756 POPUPMENU *menu = MENU_GetMenu( hmenu );
757 MENUITEM *item = menu->items;
758 LRESULT menuchar;
760 if( !forceMenuChar )
762 UINT i;
763 BOOL cjk = GetSystemMetrics( SM_DBCSENABLED );
765 for (i = 0; i < menu->nItems; i++, item++)
767 if( item->text)
769 const WCHAR *p = item->text - 2;
772 const WCHAR *q = p + 2;
773 p = strchrW (q, '&');
774 if (!p && cjk) p = strchrW (q, '\036'); /* Japanese Win16 */
776 while (p != NULL && p [1] == '&');
777 if (p && (toupperW(p[1]) == toupperW(key))) return i;
781 menuchar = SendMessageW( hwndOwner, WM_MENUCHAR,
782 MAKEWPARAM( key, menu->wFlags ), (LPARAM)hmenu );
783 if (HIWORD(menuchar) == MNC_EXECUTE) return LOWORD(menuchar);
784 if (HIWORD(menuchar) == MNC_CLOSE) return (UINT)(-2);
786 return (UINT)(-1);
790 /***********************************************************************
791 * MENU_GetBitmapItemSize
793 * Get the size of a bitmap item.
795 static void MENU_GetBitmapItemSize( MENUITEM *lpitem, SIZE *size,
796 HWND hwndOwner)
798 BITMAP bm;
799 HBITMAP bmp = lpitem->hbmpItem;
801 size->cx = size->cy = 0;
803 /* check if there is a magic menu item associated with this item */
804 switch( (INT_PTR) bmp )
806 case (INT_PTR)HBMMENU_CALLBACK:
808 MEASUREITEMSTRUCT measItem;
809 measItem.CtlType = ODT_MENU;
810 measItem.CtlID = 0;
811 measItem.itemID = lpitem->wID;
812 measItem.itemWidth = lpitem->rect.right - lpitem->rect.left;
813 measItem.itemHeight = lpitem->rect.bottom - lpitem->rect.top;
814 measItem.itemData = lpitem->dwItemData;
815 SendMessageW( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&measItem);
816 size->cx = measItem.itemWidth;
817 size->cy = measItem.itemHeight;
818 return;
820 break;
821 case (INT_PTR)HBMMENU_SYSTEM:
822 if (lpitem->dwItemData)
824 bmp = (HBITMAP)lpitem->dwItemData;
825 break;
827 /* fall through */
828 case (INT_PTR)HBMMENU_MBAR_RESTORE:
829 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
830 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
831 case (INT_PTR)HBMMENU_MBAR_CLOSE:
832 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
833 size->cx = GetSystemMetrics( SM_CYMENU ) - 4;
834 size->cy = size->cx;
835 return;
836 case (INT_PTR)HBMMENU_POPUP_CLOSE:
837 case (INT_PTR)HBMMENU_POPUP_RESTORE:
838 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
839 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
840 size->cx = GetSystemMetrics( SM_CXMENUSIZE);
841 size->cy = GetSystemMetrics( SM_CYMENUSIZE);
842 return;
844 if (GetObjectW(bmp, sizeof(bm), &bm ))
846 size->cx = bm.bmWidth;
847 size->cy = bm.bmHeight;
851 /***********************************************************************
852 * MENU_DrawBitmapItem
854 * Draw a bitmap item.
856 static void MENU_DrawBitmapItem( HDC hdc, MENUITEM *lpitem, const RECT *rect,
857 POPUPMENU *menu, HWND hwndOwner, UINT odaction )
859 BITMAP bm;
860 DWORD rop;
861 HDC hdcMem;
862 HBITMAP bmp;
863 int w = rect->right - rect->left;
864 int h = rect->bottom - rect->top;
865 int bmp_xoffset = 0;
866 int left, top;
867 HBITMAP hbmToDraw = lpitem->hbmpItem;
868 bmp = hbmToDraw;
870 /* Check if there is a magic menu item associated with this item */
871 if (IS_MAGIC_BITMAP(hbmToDraw))
873 UINT flags = 0;
874 WCHAR bmchr = 0;
875 RECT r;
877 switch((INT_PTR)hbmToDraw)
879 case (INT_PTR)HBMMENU_SYSTEM:
880 if (lpitem->dwItemData)
882 bmp = (HBITMAP)lpitem->dwItemData;
883 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
885 else
887 static HBITMAP hBmpSysMenu;
889 if (!hBmpSysMenu) hBmpSysMenu = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE));
890 bmp = hBmpSysMenu;
891 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
892 /* only use right half of the bitmap */
893 bmp_xoffset = bm.bmWidth / 2;
894 bm.bmWidth -= bmp_xoffset;
896 goto got_bitmap;
897 case (INT_PTR)HBMMENU_MBAR_RESTORE:
898 flags = DFCS_CAPTIONRESTORE;
899 break;
900 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
901 flags = DFCS_CAPTIONMIN;
902 break;
903 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
904 flags = DFCS_CAPTIONMIN | DFCS_INACTIVE;
905 break;
906 case (INT_PTR)HBMMENU_MBAR_CLOSE:
907 flags = DFCS_CAPTIONCLOSE;
908 break;
909 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
910 flags = DFCS_CAPTIONCLOSE | DFCS_INACTIVE;
911 break;
912 case (INT_PTR)HBMMENU_CALLBACK:
914 DRAWITEMSTRUCT drawItem;
915 drawItem.CtlType = ODT_MENU;
916 drawItem.CtlID = 0;
917 drawItem.itemID = lpitem->wID;
918 drawItem.itemAction = odaction;
919 drawItem.itemState = (lpitem->fState & MF_CHECKED)?ODS_CHECKED:0;
920 drawItem.itemState |= (lpitem->fState & MF_DEFAULT)?ODS_DEFAULT:0;
921 drawItem.itemState |= (lpitem->fState & MF_DISABLED)?ODS_DISABLED:0;
922 drawItem.itemState |= (lpitem->fState & MF_GRAYED)?ODS_GRAYED|ODS_DISABLED:0;
923 drawItem.itemState |= (lpitem->fState & MF_HILITE)?ODS_SELECTED:0;
924 drawItem.hwndItem = (HWND)menu->obj.handle;
925 drawItem.hDC = hdc;
926 drawItem.itemData = lpitem->dwItemData;
927 drawItem.rcItem = *rect;
928 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&drawItem);
929 return;
931 break;
932 case (INT_PTR)HBMMENU_POPUP_CLOSE:
933 bmchr = 0x72;
934 break;
935 case (INT_PTR)HBMMENU_POPUP_RESTORE:
936 bmchr = 0x32;
937 break;
938 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
939 bmchr = 0x31;
940 break;
941 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
942 bmchr = 0x30;
943 break;
944 default:
945 FIXME("Magic %p not implemented\n", hbmToDraw);
946 return;
948 if (bmchr)
950 /* draw the magic bitmaps using marlett font characters */
951 /* FIXME: fontsize and the position (x,y) could probably be better */
952 HFONT hfont, hfontsav;
953 LOGFONTW logfont = { 0, 0, 0, 0, FW_NORMAL,
954 0, 0, 0, SYMBOL_CHARSET, 0, 0, 0, 0,
955 { 'M','a','r','l','e','t','t',0 } };
956 logfont.lfHeight = min( h, w) - 5 ;
957 TRACE(" height %d rect %s\n", logfont.lfHeight, wine_dbgstr_rect( rect));
958 hfont = CreateFontIndirectW( &logfont);
959 hfontsav = SelectObject(hdc, hfont);
960 TextOutW( hdc, rect->left, rect->top + 2, &bmchr, 1);
961 SelectObject(hdc, hfontsav);
962 DeleteObject( hfont);
964 else
966 r = *rect;
967 InflateRect( &r, -1, -1 );
968 if (lpitem->fState & MF_HILITE) flags |= DFCS_PUSHED;
969 DrawFrameControl( hdc, &r, DFC_CAPTION, flags );
971 return;
974 if (!bmp || !GetObjectW( bmp, sizeof(bm), &bm )) return;
976 got_bitmap:
977 hdcMem = CreateCompatibleDC( hdc );
978 SelectObject( hdcMem, bmp );
980 /* handle fontsize > bitmap_height */
981 top = (h>bm.bmHeight) ? rect->top+(h-bm.bmHeight)/2 : rect->top;
982 left=rect->left;
983 rop=((lpitem->fState & MF_HILITE) && !IS_MAGIC_BITMAP(hbmToDraw)) ? NOTSRCCOPY : SRCCOPY;
984 if ((lpitem->fState & MF_HILITE) && lpitem->hbmpItem)
985 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
986 BitBlt( hdc, left, top, w, h, hdcMem, bmp_xoffset, 0, rop );
987 DeleteDC( hdcMem );
991 /***********************************************************************
992 * MENU_CalcItemSize
994 * Calculate the size of the menu item and store it in lpitem->rect.
996 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
997 INT orgX, INT orgY, BOOL menuBar, POPUPMENU* lppop )
999 WCHAR *p;
1000 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1001 UINT arrow_bitmap_width;
1002 BITMAP bm;
1003 INT itemheight;
1005 TRACE("dc=%p owner=%p (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
1006 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
1007 (menuBar ? " (MenuBar)" : ""));
1009 GetObjectW( get_arrow_bitmap(), sizeof(bm), &bm );
1010 arrow_bitmap_width = bm.bmWidth;
1012 /* not done in Menu_Init: GetDialogBaseUnits() breaks there */
1013 if( !menucharsize.cx ) {
1014 menucharsize.cx = GdiGetCharDimensions( hdc, NULL, &menucharsize.cy );
1015 /* Win95/98/ME will use menucharsize.cy here. Testing is possible
1016 * but it is unlikely an application will depend on that */
1017 ODitemheight = HIWORD( GetDialogBaseUnits());
1020 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
1022 if (lpitem->fType & MF_OWNERDRAW)
1024 MEASUREITEMSTRUCT mis;
1025 mis.CtlType = ODT_MENU;
1026 mis.CtlID = 0;
1027 mis.itemID = lpitem->wID;
1028 mis.itemData = lpitem->dwItemData;
1029 mis.itemHeight = ODitemheight;
1030 mis.itemWidth = 0;
1031 SendMessageW( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
1032 /* Tests reveal that Windows ( Win95 through WinXP) adds twice the average
1033 * width of a menufont character to the width of an owner-drawn menu.
1035 lpitem->rect.right += mis.itemWidth + 2 * menucharsize.cx;
1036 if (menuBar) {
1037 /* under at least win95 you seem to be given a standard
1038 height for the menu and the height value is ignored */
1039 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENUSIZE);
1040 } else
1041 lpitem->rect.bottom += mis.itemHeight;
1043 TRACE("id=%04lx size=%dx%d\n",
1044 lpitem->wID, lpitem->rect.right-lpitem->rect.left,
1045 lpitem->rect.bottom-lpitem->rect.top);
1046 return;
1049 if (lpitem->fType & MF_SEPARATOR)
1051 lpitem->rect.bottom += GetSystemMetrics( SM_CYMENUSIZE)/2;
1052 if( !menuBar)
1053 lpitem->rect.right += arrow_bitmap_width + menucharsize.cx;
1054 return;
1057 itemheight = 0;
1058 lpitem->xTab = 0;
1060 if (!menuBar) {
1061 if (lpitem->hbmpItem) {
1062 SIZE size;
1064 MENU_GetBitmapItemSize(lpitem, &size, hwndOwner);
1065 /* Keep the size of the bitmap in callback mode to be able
1066 * to draw it correctly */
1067 lpitem->bmpsize = size;
1068 lppop->textOffset = max( lppop->textOffset, size.cx);
1069 lpitem->rect.right += size.cx + 2;
1070 itemheight = size.cy + 2;
1072 if( !(lppop->dwStyle & MNS_NOCHECK))
1073 lpitem->rect.right += check_bitmap_width;
1074 lpitem->rect.right += 4 + menucharsize.cx;
1075 lpitem->xTab = lpitem->rect.right;
1076 lpitem->rect.right += arrow_bitmap_width;
1077 } else if (lpitem->hbmpItem) { /* menuBar */
1078 SIZE size;
1080 MENU_GetBitmapItemSize( lpitem, &size, hwndOwner );
1081 lpitem->bmpsize = size;
1082 lpitem->rect.right += size.cx;
1083 if( lpitem->text) lpitem->rect.right += 2;
1084 itemheight = size.cy;
1087 /* it must be a text item - unless it's the system menu */
1088 if (!(lpitem->fType & MF_SYSMENU) && lpitem->text) {
1089 HFONT hfontOld = NULL;
1090 RECT rc = lpitem->rect;
1091 LONG txtheight, txtwidth;
1093 if ( lpitem->fState & MFS_DEFAULT ) {
1094 hfontOld = SelectObject( hdc, get_menu_font(TRUE) );
1096 if (menuBar) {
1097 txtheight = DrawTextW( hdc, lpitem->text, -1, &rc,
1098 DT_SINGLELINE|DT_CALCRECT);
1099 lpitem->rect.right += rc.right - rc.left;
1100 itemheight = max( max( itemheight, txtheight),
1101 GetSystemMetrics( SM_CYMENU) - 1);
1102 lpitem->rect.right += 2 * menucharsize.cx;
1103 } else {
1104 if ((p = strchrW( lpitem->text, '\t' )) != NULL) {
1105 RECT tmprc = rc;
1106 LONG tmpheight;
1107 int n = (int)( p - lpitem->text);
1108 /* Item contains a tab (only meaningful in popup menus) */
1109 /* get text size before the tab */
1110 txtheight = DrawTextW( hdc, lpitem->text, n, &rc,
1111 DT_SINGLELINE|DT_CALCRECT);
1112 txtwidth = rc.right - rc.left;
1113 p += 1; /* advance past the Tab */
1114 /* get text size after the tab */
1115 tmpheight = DrawTextW( hdc, p, -1, &tmprc,
1116 DT_SINGLELINE|DT_CALCRECT);
1117 lpitem->xTab += txtwidth;
1118 txtheight = max( txtheight, tmpheight);
1119 txtwidth += menucharsize.cx + /* space for the tab */
1120 tmprc.right - tmprc.left; /* space for the short cut */
1121 } else {
1122 txtheight = DrawTextW( hdc, lpitem->text, -1, &rc,
1123 DT_SINGLELINE|DT_CALCRECT);
1124 txtwidth = rc.right - rc.left;
1125 lpitem->xTab += txtwidth;
1127 lpitem->rect.right += 2 + txtwidth;
1128 itemheight = max( itemheight,
1129 max( txtheight + 2, menucharsize.cy + 4));
1131 if (hfontOld) SelectObject (hdc, hfontOld);
1132 } else if( menuBar) {
1133 itemheight = max( itemheight, GetSystemMetrics(SM_CYMENU)-1);
1135 lpitem->rect.bottom += itemheight;
1136 TRACE("%s\n", wine_dbgstr_rect( &lpitem->rect));
1139 /***********************************************************************
1140 * MENU_PopupMenuCalcSize
1142 * Calculate the size of a popup menu.
1144 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, UINT max_height )
1146 MENUITEM *lpitem;
1147 HDC hdc;
1148 UINT start, i;
1149 BOOL textandbmp = FALSE, multi_col = FALSE;
1150 int orgX, orgY, maxTab, maxTabWidth;
1152 lppop->Width = lppop->Height = 0;
1153 SetRectEmpty(&lppop->items_rect);
1155 if (lppop->nItems == 0) return;
1156 hdc = GetDC( 0 );
1158 SelectObject( hdc, get_menu_font(FALSE));
1160 start = 0;
1162 lppop->textOffset = 0;
1164 while (start < lppop->nItems)
1166 lpitem = &lppop->items[start];
1167 orgX = lppop->items_rect.right;
1168 if( lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))
1169 orgX += MENU_COL_SPACE;
1170 orgY = lppop->items_rect.top;
1172 maxTab = maxTabWidth = 0;
1173 /* Parse items until column break or end of menu */
1174 for (i = start; i < lppop->nItems; i++, lpitem++)
1176 if (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))
1178 multi_col = TRUE;
1179 if (i != start) break;
1182 MENU_CalcItemSize( hdc, lpitem, lppop->hwndOwner, orgX, orgY, FALSE, lppop );
1183 lppop->items_rect.right = max( lppop->items_rect.right, lpitem->rect.right );
1184 orgY = lpitem->rect.bottom;
1185 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1187 maxTab = max( maxTab, lpitem->xTab );
1188 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
1190 if( lpitem->text && lpitem->hbmpItem) textandbmp = TRUE;
1193 /* Finish the column (set all items to the largest width found) */
1194 lppop->items_rect.right = max( lppop->items_rect.right, maxTab + maxTabWidth );
1195 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
1197 lpitem->rect.right = lppop->items_rect.right;
1198 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1199 lpitem->xTab = maxTab;
1202 lppop->items_rect.bottom = max( lppop->items_rect.bottom, orgY );
1205 /* if none of the items have both text and bitmap then
1206 * the text and bitmaps are all aligned on the left. If there is at
1207 * least one item with both text and bitmap then bitmaps are
1208 * on the left and texts left aligned with the right hand side
1209 * of the bitmaps */
1210 if( !textandbmp) lppop->textOffset = 0;
1212 lppop->nTotalHeight = lppop->items_rect.bottom;
1214 /* space for the border */
1215 OffsetRect(&lppop->items_rect, MENU_MARGIN, MENU_MARGIN);
1216 lppop->Height = lppop->items_rect.bottom + MENU_MARGIN;
1217 lppop->Width = lppop->items_rect.right + MENU_MARGIN;
1219 /* Adjust popup height if it exceeds maximum */
1220 if (lppop->Height >= max_height)
1222 lppop->Height = max_height;
1223 lppop->bScrolling = !multi_col;
1224 /* When the scroll arrows are present, don't add the top/bottom margin as well */
1225 if (lppop->bScrolling)
1227 lppop->items_rect.top = get_scroll_arrow_height(lppop);
1228 lppop->items_rect.bottom = lppop->Height - get_scroll_arrow_height(lppop);
1231 else
1233 lppop->bScrolling = FALSE;
1236 ReleaseDC( 0, hdc );
1240 /***********************************************************************
1241 * MENU_MenuBarCalcSize
1243 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
1244 * height is off by 1 pixel which causes lengthy window relocations when
1245 * active document window is maximized/restored.
1247 * Calculate the size of the menu bar.
1249 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
1250 LPPOPUPMENU lppop, HWND hwndOwner )
1252 MENUITEM *lpitem;
1253 UINT start, i, helpPos;
1254 int orgX, orgY;
1256 if ((lprect == NULL) || (lppop == NULL)) return;
1257 if (lppop->nItems == 0) return;
1258 TRACE("lprect %p %s\n", lprect, wine_dbgstr_rect( lprect));
1259 /* Start with a 1 pixel top border.
1260 This corresponds to the difference between SM_CYMENU and SM_CYMENUSIZE. */
1261 SetRect(&lppop->items_rect, 0, 0, lprect->right - lprect->left, 1);
1262 start = 0;
1263 helpPos = ~0U;
1264 lppop->textOffset = 0;
1265 while (start < lppop->nItems)
1267 lpitem = &lppop->items[start];
1268 orgX = lppop->items_rect.left;
1269 orgY = lppop->items_rect.bottom;
1271 /* Parse items until line break or end of menu */
1272 for (i = start; i < lppop->nItems; i++, lpitem++)
1274 if ((helpPos == ~0U) && (lpitem->fType & MF_RIGHTJUSTIFY)) helpPos = i;
1275 if ((i != start) &&
1276 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1278 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n", orgX, orgY );
1279 debug_print_menuitem (" item: ", lpitem, "");
1280 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE, lppop );
1282 if (lpitem->rect.right > lppop->items_rect.right)
1284 if (i != start) break;
1285 else lpitem->rect.right = lppop->items_rect.right;
1287 lppop->items_rect.bottom = max( lppop->items_rect.bottom, lpitem->rect.bottom );
1288 orgX = lpitem->rect.right;
1291 /* Finish the line (set all items to the largest height found) */
1292 while (start < i) lppop->items[start++].rect.bottom = lppop->items_rect.bottom;
1295 OffsetRect(&lppop->items_rect, lprect->left, lprect->top);
1296 lppop->Width = lppop->items_rect.right - lppop->items_rect.left;
1297 lppop->Height = lppop->items_rect.bottom - lppop->items_rect.top;
1298 lprect->bottom = lppop->items_rect.bottom;
1300 /* Flush right all items between the MF_RIGHTJUSTIFY and */
1301 /* the last item (if several lines, only move the last line) */
1302 if (helpPos == ~0U) return;
1303 lpitem = &lppop->items[lppop->nItems-1];
1304 orgY = lpitem->rect.top;
1305 orgX = lprect->right - lprect->left;
1306 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--) {
1307 if (lpitem->rect.top != orgY) break; /* Other line */
1308 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1309 lpitem->rect.left += orgX - lpitem->rect.right;
1310 lpitem->rect.right = orgX;
1311 orgX = lpitem->rect.left;
1315 static void draw_scroll_arrow(HDC hdc, int x, int top, int height, BOOL up, BOOL enabled)
1317 RECT rect, light_rect;
1318 HBRUSH brush = GetSysColorBrush( enabled ? COLOR_BTNTEXT : COLOR_BTNSHADOW );
1319 HBRUSH light = GetSysColorBrush( COLOR_3DLIGHT );
1321 if (!up)
1323 top = top + height;
1324 if (!enabled)
1326 SetRect( &rect, x + 1, top, x + 2, top + 1);
1327 FillRect( hdc, &rect, light );
1329 top--;
1332 SetRect( &rect, x, top, x + 1, top + 1);
1333 while (height--)
1335 FillRect( hdc, &rect, brush );
1336 if (!enabled && !up && height)
1338 SetRect( &light_rect, rect.right, rect.top, rect.right + 2, rect.bottom );
1339 FillRect( hdc, &light_rect, light );
1341 InflateRect( &rect, 1, 0 );
1342 OffsetRect( &rect, 0, up ? 1 : -1 );
1345 if (!enabled && up)
1347 rect.left += 2;
1348 FillRect( hdc, &rect, light );
1352 /***********************************************************************
1353 * MENU_DrawScrollArrows
1355 * Draw scroll arrows.
1357 static void
1358 MENU_DrawScrollArrows(const POPUPMENU *menu, HDC hdc)
1360 UINT full_height = get_scroll_arrow_height( menu );
1361 UINT arrow_height = full_height / 3;
1362 BOOL at_end = menu->nScrollPos + menu->items_rect.bottom - menu->items_rect.top == menu->nTotalHeight;
1364 draw_scroll_arrow( hdc, menu->Width / 3, arrow_height, arrow_height,
1365 TRUE, menu->nScrollPos != 0);
1366 draw_scroll_arrow( hdc, menu->Width / 3, menu->Height - 2 * arrow_height, arrow_height,
1367 FALSE, !at_end );
1371 /***********************************************************************
1372 * draw_popup_arrow
1374 * Draws the popup-menu arrow.
1376 static void draw_popup_arrow( HDC hdc, RECT rect, UINT arrow_bitmap_width,
1377 UINT arrow_bitmap_height)
1379 HDC hdcMem = CreateCompatibleDC( hdc );
1380 HBITMAP hOrigBitmap;
1382 hOrigBitmap = SelectObject( hdcMem, get_arrow_bitmap() );
1383 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1384 (rect.top + rect.bottom - arrow_bitmap_height) / 2,
1385 arrow_bitmap_width, arrow_bitmap_height,
1386 hdcMem, 0, 0, SRCCOPY );
1387 SelectObject( hdcMem, hOrigBitmap );
1388 DeleteDC( hdcMem );
1390 /***********************************************************************
1391 * MENU_DrawMenuItem
1393 * Draw a single menu item.
1395 static void MENU_DrawMenuItem( HWND hwnd, POPUPMENU *menu, HWND hwndOwner, HDC hdc,
1396 MENUITEM *lpitem, BOOL menuBar, UINT odaction )
1398 RECT rect, bmprc;
1399 BOOL flat_menu = FALSE;
1400 int bkgnd;
1401 UINT arrow_bitmap_width = 0, arrow_bitmap_height = 0;
1402 HRGN old_clip = NULL, clip;
1404 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1406 if (!menuBar) {
1407 BITMAP bmp;
1408 GetObjectW( get_arrow_bitmap(), sizeof(bmp), &bmp );
1409 arrow_bitmap_width = bmp.bmWidth;
1410 arrow_bitmap_height = bmp.bmHeight;
1413 if (lpitem->fType & MF_SYSMENU)
1415 if( !IsIconic(hwnd) )
1416 NC_DrawSysButton( hwnd, hdc, lpitem->fState & (MF_HILITE | MF_MOUSESELECT) );
1417 return;
1420 TRACE( "rect=%s\n", wine_dbgstr_rect( &lpitem->rect ) );
1421 rect = lpitem->rect;
1422 MENU_AdjustMenuItemRect( menu, &rect );
1423 if (!IntersectRect( &bmprc, &rect, &menu->items_rect )) /* bmprc is used as a dummy */
1424 return;
1426 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1427 bkgnd = (menuBar && flat_menu) ? COLOR_MENUBAR : COLOR_MENU;
1429 /* Setup colors */
1431 if (lpitem->fState & MF_HILITE)
1433 if(menuBar && !flat_menu) {
1434 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1435 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1436 } else {
1437 if(lpitem->fState & MF_GRAYED)
1438 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1439 else
1440 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1441 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1444 else
1446 if (lpitem->fState & MF_GRAYED)
1447 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1448 else
1449 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1450 SetBkColor( hdc, GetSysColor( bkgnd ) );
1453 old_clip = CreateRectRgn( 0, 0, 0, 0 );
1454 if (GetClipRgn( hdc, old_clip ) <= 0)
1456 DeleteObject( old_clip );
1457 old_clip = NULL;
1459 clip = CreateRectRgnIndirect( &menu->items_rect );
1460 ExtSelectClipRgn( hdc, clip, RGN_AND );
1461 DeleteObject( clip );
1463 if (lpitem->fType & MF_OWNERDRAW)
1466 ** Experimentation under Windows reveals that an owner-drawn
1467 ** menu is given the rectangle which includes the space it requested
1468 ** in its response to WM_MEASUREITEM _plus_ width for a checkmark
1469 ** and a popup-menu arrow. This is the value of lpitem->rect.
1470 ** Windows will leave all drawing to the application except for
1471 ** the popup-menu arrow. Windows always draws that itself, after
1472 ** the menu owner has finished drawing.
1474 DRAWITEMSTRUCT dis;
1475 COLORREF old_bk, old_text;
1477 dis.CtlType = ODT_MENU;
1478 dis.CtlID = 0;
1479 dis.itemID = lpitem->wID;
1480 dis.itemData = lpitem->dwItemData;
1481 dis.itemState = 0;
1482 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1483 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED|ODS_DISABLED;
1484 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1485 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1486 dis.hwndItem = (HWND)menu->obj.handle;
1487 dis.hDC = hdc;
1488 dis.rcItem = rect;
1489 TRACE("Ownerdraw: owner=%p itemID=%d, itemState=%d, itemAction=%d, "
1490 "hwndItem=%p, hdc=%p, rcItem=%s\n", hwndOwner,
1491 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1492 dis.hDC, wine_dbgstr_rect( &dis.rcItem));
1493 old_bk = GetBkColor( hdc );
1494 old_text = GetTextColor( hdc );
1495 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1496 /* Draw the popup-menu arrow */
1497 SetBkColor( hdc, old_bk );
1498 SetTextColor( hdc, old_text );
1499 if (lpitem->fType & MF_POPUP)
1500 draw_popup_arrow( hdc, rect, arrow_bitmap_width,
1501 arrow_bitmap_height);
1502 goto done;
1505 if (menuBar && (lpitem->fType & MF_SEPARATOR)) goto done;
1507 if (lpitem->fState & MF_HILITE)
1509 if (flat_menu)
1511 InflateRect (&rect, -1, -1);
1512 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENUHILIGHT));
1513 InflateRect (&rect, 1, 1);
1514 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1516 else
1518 if(menuBar)
1519 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1520 else
1521 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1524 else
1525 FillRect( hdc, &rect, GetSysColorBrush(bkgnd) );
1527 SetBkMode( hdc, TRANSPARENT );
1529 /* vertical separator */
1530 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1532 HPEN oldPen;
1533 RECT rc = rect;
1535 rc.left -= MENU_COL_SPACE / 2 + 1;
1536 rc.top = 3;
1537 rc.bottom = menu->Height - 3;
1538 if (flat_menu)
1540 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1541 MoveToEx( hdc, rc.left, rc.top, NULL );
1542 LineTo( hdc, rc.left, rc.bottom );
1543 SelectObject( hdc, oldPen );
1545 else
1546 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1549 /* horizontal separator */
1550 if (lpitem->fType & MF_SEPARATOR)
1552 HPEN oldPen;
1553 RECT rc = rect;
1555 InflateRect( &rc, -1, 0 );
1556 rc.top = ( rc.top + rc.bottom) / 2;
1557 if (flat_menu)
1559 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1560 MoveToEx( hdc, rc.left, rc.top, NULL );
1561 LineTo( hdc, rc.right, rc.top );
1562 SelectObject( hdc, oldPen );
1564 else
1565 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1566 goto done;
1569 /* helper lines for debugging */
1570 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1571 SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
1572 MoveToEx( hdc, rect.left, (rect.top + rect.bottom)/2, NULL );
1573 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1576 if (lpitem->hbmpItem) {
1577 /* calculate the bitmap rectangle in coordinates relative
1578 * to the item rectangle */
1579 if( menuBar) {
1580 if( lpitem->hbmpItem == HBMMENU_CALLBACK)
1581 bmprc.left = 3;
1582 else
1583 bmprc.left = lpitem->text ? menucharsize.cx : 0;
1585 else if (menu->dwStyle & MNS_NOCHECK)
1586 bmprc.left = 4;
1587 else if (menu->dwStyle & MNS_CHECKORBMP)
1588 bmprc.left = 2;
1589 else
1590 bmprc.left = 4 + GetSystemMetrics(SM_CXMENUCHECK);
1591 bmprc.right = bmprc.left + lpitem->bmpsize.cx;
1592 if( menuBar && !(lpitem->hbmpItem == HBMMENU_CALLBACK))
1593 bmprc.top = 0;
1594 else
1595 bmprc.top = (rect.bottom - rect.top -
1596 lpitem->bmpsize.cy) / 2;
1597 bmprc.bottom = bmprc.top + lpitem->bmpsize.cy;
1600 if (!menuBar)
1602 HBITMAP bm;
1603 INT y = rect.top + rect.bottom;
1604 BOOL checked = FALSE;
1605 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1606 UINT check_bitmap_height = GetSystemMetrics( SM_CYMENUCHECK );
1607 /* Draw the check mark
1609 * FIXME:
1610 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1612 if (!(menu->dwStyle & MNS_NOCHECK))
1614 bm = (lpitem->fState & MF_CHECKED) ? lpitem->hCheckBit :
1615 lpitem->hUnCheckBit;
1616 if (bm) /* we have a custom bitmap */
1618 HDC hdcMem = CreateCompatibleDC( hdc );
1620 SelectObject( hdcMem, bm );
1621 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1622 check_bitmap_width, check_bitmap_height,
1623 hdcMem, 0, 0, SRCCOPY );
1624 DeleteDC( hdcMem );
1625 checked = TRUE;
1627 else if (lpitem->fState & MF_CHECKED) /* standard bitmaps */
1629 RECT r;
1630 HBITMAP bm = CreateBitmap( check_bitmap_width,
1631 check_bitmap_height, 1, 1, NULL );
1632 HDC hdcMem = CreateCompatibleDC( hdc );
1634 SelectObject( hdcMem, bm );
1635 SetRect( &r, 0, 0, check_bitmap_width, check_bitmap_height);
1636 DrawFrameControl( hdcMem, &r, DFC_MENU,
1637 (lpitem->fType & MFT_RADIOCHECK) ?
1638 DFCS_MENUBULLET : DFCS_MENUCHECK );
1639 BitBlt( hdc, rect.left, (y - r.bottom) / 2, r.right, r.bottom,
1640 hdcMem, 0, 0, SRCCOPY );
1641 DeleteDC( hdcMem );
1642 DeleteObject( bm );
1643 checked = TRUE;
1646 if (lpitem->hbmpItem && !(checked && (menu->dwStyle & MNS_CHECKORBMP)))
1648 POINT origorg;
1649 /* some applications make this assumption on the DC's origin */
1650 SetViewportOrgEx( hdc, rect.left, rect.top, &origorg);
1651 MENU_DrawBitmapItem( hdc, lpitem, &bmprc, menu, hwndOwner, odaction );
1652 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1654 /* Draw the popup-menu arrow */
1655 if (lpitem->fType & MF_POPUP)
1656 draw_popup_arrow( hdc, rect, arrow_bitmap_width,
1657 arrow_bitmap_height);
1658 rect.left += 4;
1659 if( !(menu->dwStyle & MNS_NOCHECK))
1660 rect.left += check_bitmap_width;
1661 rect.right -= arrow_bitmap_width;
1663 else if (lpitem->hbmpItem)
1664 { /* Draw the bitmap */
1665 POINT origorg;
1667 SetViewportOrgEx( hdc, rect.left, rect.top, &origorg);
1668 MENU_DrawBitmapItem( hdc, lpitem, &bmprc, menu, hwndOwner, odaction );
1669 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1671 /* process text if present */
1672 if (lpitem->text)
1674 int i;
1675 HFONT hfontOld = 0;
1677 UINT uFormat = (menuBar) ?
1678 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1679 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1681 if( !(menu->dwStyle & MNS_CHECKORBMP))
1682 rect.left += menu->textOffset;
1684 if ( lpitem->fState & MFS_DEFAULT )
1686 hfontOld = SelectObject( hdc, get_menu_font(TRUE) );
1689 if (menuBar) {
1690 if( lpitem->hbmpItem)
1691 rect.left += lpitem->bmpsize.cx;
1692 if( !(lpitem->hbmpItem == HBMMENU_CALLBACK))
1693 rect.left += menucharsize.cx;
1694 rect.right -= menucharsize.cx;
1697 for (i = 0; lpitem->text[i]; i++)
1698 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1699 break;
1701 if(lpitem->fState & MF_GRAYED)
1703 if (!(lpitem->fState & MF_HILITE) )
1705 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1706 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1707 DrawTextW( hdc, lpitem->text, i, &rect, uFormat );
1708 --rect.left; --rect.top; --rect.right; --rect.bottom;
1710 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1713 DrawTextW( hdc, lpitem->text, i, &rect, uFormat);
1715 /* paint the shortcut text */
1716 if (!menuBar && lpitem->text[i]) /* There's a tab or flush-right char */
1718 if (lpitem->text[i] == '\t')
1720 rect.left = lpitem->xTab;
1721 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1723 else
1725 rect.right = lpitem->xTab;
1726 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1729 if(lpitem->fState & MF_GRAYED)
1731 if (!(lpitem->fState & MF_HILITE) )
1733 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1734 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1735 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1736 --rect.left; --rect.top; --rect.right; --rect.bottom;
1738 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1740 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1743 if (hfontOld)
1744 SelectObject (hdc, hfontOld);
1747 done:
1748 ExtSelectClipRgn( hdc, old_clip, RGN_COPY );
1749 if (old_clip) DeleteObject( old_clip );
1753 /***********************************************************************
1754 * MENU_DrawPopupMenu
1756 * Paint a popup menu.
1758 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1760 HBRUSH hPrevBrush, brush = GetSysColorBrush( COLOR_MENU );
1761 RECT rect;
1762 POPUPMENU *menu = MENU_GetMenu( hmenu );
1764 TRACE("wnd=%p dc=%p menu=%p\n", hwnd, hdc, hmenu);
1766 GetClientRect( hwnd, &rect );
1768 if (menu && menu->hbrBack) brush = menu->hbrBack;
1769 if ((hPrevBrush = SelectObject( hdc, brush ))
1770 && SelectObject( hdc, get_menu_font(FALSE) ))
1772 HPEN hPrevPen;
1774 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1776 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1777 if( hPrevPen )
1779 BOOL flat_menu = FALSE;
1781 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1782 if (flat_menu)
1783 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
1784 else
1785 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1787 if (menu)
1789 TRACE("hmenu %p Style %08x\n", hmenu, menu->dwStyle);
1790 /* draw menu items */
1791 if (menu->nItems)
1793 MENUITEM *item;
1794 UINT u;
1796 item = menu->items;
1797 for (u = menu->nItems; u > 0; u--, item++)
1798 MENU_DrawMenuItem( hwnd, menu, menu->hwndOwner, hdc,
1799 item, FALSE, ODA_DRAWENTIRE );
1801 /* draw scroll arrows */
1802 if (menu->bScrolling)
1803 MENU_DrawScrollArrows(menu, hdc);
1805 } else
1807 SelectObject( hdc, hPrevBrush );
1812 /***********************************************************************
1813 * MENU_DrawMenuBar
1815 * Paint a menu bar. Returns the height of the menu bar.
1816 * called from [windows/nonclient.c]
1818 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd )
1820 LPPOPUPMENU lppop;
1821 HMENU hMenu = GetMenu(hwnd);
1823 lppop = MENU_GetMenu( hMenu );
1824 if (lppop == NULL || lprect == NULL)
1826 return GetSystemMetrics(SM_CYMENU);
1829 return DrawMenuBarTemp(hwnd, hDC, lprect, hMenu, NULL);
1833 /***********************************************************************
1834 * MENU_InitPopup
1836 * Popup menu initialization before WM_ENTERMENULOOP.
1838 static BOOL MENU_InitPopup( HWND hwndOwner, HMENU hmenu, UINT flags )
1840 POPUPMENU *menu;
1841 DWORD ex_style = 0;
1843 TRACE("owner=%p hmenu=%p\n", hwndOwner, hmenu);
1845 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1847 /* store the owner for DrawItem */
1848 if (!IsWindow( hwndOwner ))
1850 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1851 return FALSE;
1853 menu->hwndOwner = hwndOwner;
1855 if (flags & TPM_LAYOUTRTL)
1856 ex_style = WS_EX_LAYOUTRTL;
1858 /* NOTE: In Windows, top menu popup is not owned. */
1859 menu->hWnd = CreateWindowExW( ex_style, (LPCWSTR)POPUPMENU_CLASS_ATOM, NULL,
1860 WS_POPUP, 0, 0, 0, 0,
1861 hwndOwner, 0, (HINSTANCE)GetWindowLongPtrW(hwndOwner, GWLP_HINSTANCE),
1862 (LPVOID)hmenu );
1863 if( !menu->hWnd ) return FALSE;
1864 return TRUE;
1868 /***********************************************************************
1869 * MENU_ShowPopup
1871 * Display a popup menu.
1873 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id, UINT flags,
1874 INT x, INT y, INT xanchor, INT yanchor )
1876 POPUPMENU *menu;
1877 POINT pt;
1878 HMONITOR monitor;
1879 MONITORINFO info;
1880 UINT max_height;
1882 TRACE("owner=%p hmenu=%p id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1883 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1885 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1886 if (menu->FocusedItem != NO_SELECTED_ITEM)
1888 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1889 menu->FocusedItem = NO_SELECTED_ITEM;
1892 menu->nScrollPos = 0;
1894 /* FIXME: should use item rect */
1895 pt.x = x;
1896 pt.y = y;
1897 monitor = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
1898 info.cbSize = sizeof(info);
1899 GetMonitorInfoW( monitor, &info );
1901 max_height = info.rcWork.bottom - info.rcWork.top;
1902 if (menu->cyMax)
1903 max_height = min( max_height, menu->cyMax );
1905 MENU_PopupMenuCalcSize( menu, max_height );
1907 /* adjust popup menu pos so that it fits within the desktop */
1909 if (flags & TPM_LAYOUTRTL)
1910 flags ^= TPM_RIGHTALIGN;
1912 if( flags & TPM_RIGHTALIGN ) x -= menu->Width;
1913 if( flags & TPM_CENTERALIGN ) x -= menu->Width / 2;
1915 if( flags & TPM_BOTTOMALIGN ) y -= menu->Height;
1916 if( flags & TPM_VCENTERALIGN ) y -= menu->Height / 2;
1918 if( x + menu->Width > info.rcWork.right)
1920 if( xanchor && x >= menu->Width - xanchor )
1921 x -= menu->Width - xanchor;
1923 if( x + menu->Width > info.rcWork.right)
1924 x = info.rcWork.right - menu->Width;
1926 if( x < info.rcWork.left ) x = info.rcWork.left;
1928 if( y + menu->Height > info.rcWork.bottom)
1930 if( yanchor && y >= menu->Height + yanchor )
1931 y -= menu->Height + yanchor;
1933 if( y + menu->Height > info.rcWork.bottom)
1934 y = info.rcWork.bottom - menu->Height;
1936 if( y < info.rcWork.top ) y = info.rcWork.top;
1938 if (!top_popup) {
1939 top_popup = menu->hWnd;
1940 top_popup_hmenu = hmenu;
1942 /* Display the window */
1944 SetWindowPos( menu->hWnd, HWND_TOPMOST, x, y, menu->Width, menu->Height,
1945 SWP_SHOWWINDOW | SWP_NOACTIVATE );
1946 UpdateWindow( menu->hWnd );
1947 return TRUE;
1951 /***********************************************************************
1952 * MENU_EnsureMenuItemVisible
1954 static void
1955 MENU_EnsureMenuItemVisible(LPPOPUPMENU lppop, UINT wIndex, HDC hdc)
1957 if (lppop->bScrolling)
1959 MENUITEM *item = &lppop->items[wIndex];
1960 UINT nOldPos = lppop->nScrollPos;
1961 const RECT *rc = &lppop->items_rect;
1962 UINT scroll_height = rc->bottom - rc->top;
1964 if (item->rect.bottom > lppop->nScrollPos + scroll_height)
1966 lppop->nScrollPos = item->rect.bottom - scroll_height;
1967 ScrollWindow(lppop->hWnd, 0, nOldPos - lppop->nScrollPos, rc, rc);
1969 else if (item->rect.top < lppop->nScrollPos)
1971 lppop->nScrollPos = item->rect.top;
1972 ScrollWindow(lppop->hWnd, 0, nOldPos - lppop->nScrollPos, rc, rc);
1975 /* Invalidate the scroll arrows if necessary */
1976 if (nOldPos != lppop->nScrollPos)
1978 RECT arrow_rect = lppop->items_rect;
1979 if (nOldPos == 0 || lppop->nScrollPos == 0)
1981 arrow_rect.top = 0;
1982 arrow_rect.bottom = lppop->items_rect.top;
1983 InvalidateRect(lppop->hWnd, &arrow_rect, FALSE);
1985 if (nOldPos + scroll_height == lppop->nTotalHeight ||
1986 lppop->nScrollPos + scroll_height == lppop->nTotalHeight)
1988 arrow_rect.top = lppop->items_rect.bottom;
1989 arrow_rect.bottom = lppop->Height;
1990 InvalidateRect(lppop->hWnd, &arrow_rect, FALSE);
1997 /***********************************************************************
1998 * MENU_SelectItem
2000 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
2001 BOOL sendMenuSelect, HMENU topmenu )
2003 LPPOPUPMENU lppop;
2004 HDC hdc;
2006 TRACE("owner=%p menu=%p index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
2008 lppop = MENU_GetMenu( hmenu );
2009 if ((!lppop) || (!lppop->nItems) || (!lppop->hWnd)) return;
2011 if (lppop->FocusedItem == wIndex) return;
2012 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
2013 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2014 if (!top_popup) {
2015 top_popup = lppop->hWnd;
2016 top_popup_hmenu = hmenu;
2019 SelectObject( hdc, get_menu_font(FALSE));
2021 /* Clear previous highlighted item */
2022 if (lppop->FocusedItem != NO_SELECTED_ITEM)
2024 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
2025 MENU_DrawMenuItem( lppop->hWnd, lppop, hwndOwner, hdc, &lppop->items[lppop->FocusedItem],
2026 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
2029 /* Highlight new item (if any) */
2030 lppop->FocusedItem = wIndex;
2031 if (lppop->FocusedItem != NO_SELECTED_ITEM)
2033 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
2034 lppop->items[wIndex].fState |= MF_HILITE;
2035 MENU_EnsureMenuItemVisible(lppop, wIndex, hdc);
2036 MENU_DrawMenuItem( lppop->hWnd, lppop, hwndOwner, hdc, &lppop->items[wIndex],
2037 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
2039 if (sendMenuSelect)
2041 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
2042 SendMessageW( hwndOwner, WM_MENUSELECT,
2043 MAKEWPARAM(ip->fType & MF_POPUP ? wIndex: ip->wID,
2044 ip->fType | ip->fState |
2045 (lppop->wFlags & MF_SYSMENU)), (LPARAM)hmenu);
2048 else if (sendMenuSelect) {
2049 if(topmenu){
2050 int pos;
2051 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
2052 POPUPMENU *ptm = MENU_GetMenu( topmenu );
2053 MENUITEM *ip = &ptm->items[pos];
2054 SendMessageW( hwndOwner, WM_MENUSELECT, MAKEWPARAM(pos,
2055 ip->fType | ip->fState |
2056 (ptm->wFlags & MF_SYSMENU)), (LPARAM)topmenu);
2060 ReleaseDC( lppop->hWnd, hdc );
2064 /***********************************************************************
2065 * MENU_MoveSelection
2067 * Moves currently selected item according to the offset parameter.
2068 * If there is no selection then it should select the last item if
2069 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
2071 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
2073 INT i;
2074 POPUPMENU *menu;
2076 TRACE("hwnd=%p hmenu=%p off=0x%04x\n", hwndOwner, hmenu, offset);
2078 menu = MENU_GetMenu( hmenu );
2079 if ((!menu) || (!menu->items)) return;
2081 if ( menu->FocusedItem != NO_SELECTED_ITEM )
2083 if( menu->nItems == 1 ) return; else
2084 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
2085 ; i += offset)
2086 if (!(menu->items[i].fType & MF_SEPARATOR))
2088 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
2089 return;
2093 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
2094 i >= 0 && i < menu->nItems ; i += offset)
2095 if (!(menu->items[i].fType & MF_SEPARATOR))
2097 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
2098 return;
2103 /**********************************************************************
2104 * MENU_InsertItem
2106 * Insert (allocate) a new item into a menu.
2108 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
2110 MENUITEM *newItems;
2111 POPUPMENU *menu;
2113 if (!(menu = MENU_GetMenu(hMenu)))
2114 return NULL;
2116 /* Find where to insert new item */
2118 if (flags & MF_BYPOSITION) {
2119 if (pos > menu->nItems)
2120 pos = menu->nItems;
2121 } else {
2122 if (!MENU_FindItem( &hMenu, &pos, flags ))
2123 pos = menu->nItems;
2124 else {
2125 if (!(menu = MENU_GetMenu( hMenu )))
2126 return NULL;
2130 /* Make sure that MDI system buttons stay on the right side.
2131 * Note: XP treats only bitmap handles 1 - 6 as "magic" ones
2132 * regardless of their id.
2134 while (pos > 0 && (INT_PTR)menu->items[pos - 1].hbmpItem >= (INT_PTR)HBMMENU_SYSTEM &&
2135 (INT_PTR)menu->items[pos - 1].hbmpItem <= (INT_PTR)HBMMENU_MBAR_CLOSE_D)
2136 pos--;
2138 TRACE("inserting at %u flags %x\n", pos, flags);
2140 /* Create new items array */
2142 newItems = HeapAlloc( GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
2143 if (!newItems)
2145 WARN("allocation failed\n" );
2146 return NULL;
2148 if (menu->nItems > 0)
2150 /* Copy the old array into the new one */
2151 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
2152 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
2153 (menu->nItems-pos)*sizeof(MENUITEM) );
2154 HeapFree( GetProcessHeap(), 0, menu->items );
2156 menu->items = newItems;
2157 menu->nItems++;
2158 memset( &newItems[pos], 0, sizeof(*newItems) );
2159 menu->Height = 0; /* force size recalculate */
2160 return &newItems[pos];
2164 /**********************************************************************
2165 * MENU_ParseResource
2167 * Parse a standard menu resource and add items to the menu.
2168 * Return a pointer to the end of the resource.
2170 * NOTE: flags is equivalent to the mtOption field
2172 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu )
2174 WORD flags, id = 0;
2175 LPCWSTR str;
2176 BOOL end_flag;
2180 flags = GET_WORD(res);
2181 end_flag = flags & MF_END;
2182 /* Remove MF_END because it has the same value as MF_HILITE */
2183 flags &= ~MF_END;
2184 res += sizeof(WORD);
2185 if (!(flags & MF_POPUP))
2187 id = GET_WORD(res);
2188 res += sizeof(WORD);
2190 str = (LPCWSTR)res;
2191 res += (strlenW(str) + 1) * sizeof(WCHAR);
2192 if (flags & MF_POPUP)
2194 HMENU hSubMenu = CreatePopupMenu();
2195 if (!hSubMenu) return NULL;
2196 if (!(res = MENU_ParseResource( res, hSubMenu ))) return NULL;
2197 AppendMenuW( hMenu, flags, (UINT_PTR)hSubMenu, str );
2199 else /* Not a popup */
2201 AppendMenuW( hMenu, flags, id, *str ? str : NULL );
2203 } while (!end_flag);
2204 return res;
2208 /**********************************************************************
2209 * MENUEX_ParseResource
2211 * Parse an extended menu resource and add items to the menu.
2212 * Return a pointer to the end of the resource.
2214 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
2216 WORD resinfo;
2217 do {
2218 MENUITEMINFOW mii;
2220 mii.cbSize = sizeof(mii);
2221 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
2222 mii.fType = GET_DWORD(res);
2223 res += sizeof(DWORD);
2224 mii.fState = GET_DWORD(res);
2225 res += sizeof(DWORD);
2226 mii.wID = GET_DWORD(res);
2227 res += sizeof(DWORD);
2228 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
2229 res += sizeof(WORD);
2230 /* Align the text on a word boundary. */
2231 res += (~((UINT_PTR)res - 1)) & 1;
2232 mii.dwTypeData = (LPWSTR) res;
2233 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
2234 /* Align the following fields on a dword boundary. */
2235 res += (~((UINT_PTR)res - 1)) & 3;
2237 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
2238 mii.fType, mii.fState, mii.wID, resinfo, debugstr_w(mii.dwTypeData));
2240 if (resinfo & 1) { /* Pop-up? */
2241 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
2242 res += sizeof(DWORD);
2243 mii.hSubMenu = CreatePopupMenu();
2244 if (!mii.hSubMenu)
2245 return NULL;
2246 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
2247 DestroyMenu(mii.hSubMenu);
2248 return NULL;
2250 mii.fMask |= MIIM_SUBMENU;
2251 mii.fType |= MF_POPUP;
2253 else if(!*mii.dwTypeData && !(mii.fType & MF_SEPARATOR))
2255 WARN("Converting NULL menu item %04x, type %04x to SEPARATOR\n",
2256 mii.wID, mii.fType);
2257 mii.fType |= MF_SEPARATOR;
2259 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2260 } while (!(resinfo & MF_END));
2261 return res;
2265 /***********************************************************************
2266 * MENU_GetSubPopup
2268 * Return the handle of the selected sub-popup menu (if any).
2270 static HMENU MENU_GetSubPopup( HMENU hmenu )
2272 POPUPMENU *menu;
2273 MENUITEM *item;
2275 menu = MENU_GetMenu( hmenu );
2277 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2279 item = &menu->items[menu->FocusedItem];
2280 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2281 return item->hSubMenu;
2282 return 0;
2286 /***********************************************************************
2287 * MENU_HideSubPopups
2289 * Hide the sub-popup menus of this menu.
2291 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2292 BOOL sendMenuSelect, UINT wFlags )
2294 POPUPMENU *menu = MENU_GetMenu( hmenu );
2296 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2298 if (menu && top_popup)
2300 HMENU hsubmenu;
2301 POPUPMENU *submenu;
2302 MENUITEM *item;
2304 if (menu->FocusedItem != NO_SELECTED_ITEM)
2306 item = &menu->items[menu->FocusedItem];
2307 if (!(item->fType & MF_POPUP) ||
2308 !(item->fState & MF_MOUSESELECT)) return;
2309 item->fState &= ~MF_MOUSESELECT;
2310 hsubmenu = item->hSubMenu;
2311 } else return;
2313 if (!(submenu = MENU_GetMenu( hsubmenu ))) return;
2314 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE, wFlags );
2315 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2316 DestroyWindow( submenu->hWnd );
2317 submenu->hWnd = 0;
2319 if (!(wFlags & TPM_NONOTIFY))
2320 SendMessageW( hwndOwner, WM_UNINITMENUPOPUP, (WPARAM)hsubmenu,
2321 MAKELPARAM(0, IS_SYSTEM_MENU(submenu)) );
2326 /***********************************************************************
2327 * MENU_ShowSubPopup
2329 * Display the sub-menu of the selected item of this menu.
2330 * Return the handle of the submenu, or hmenu if no submenu to display.
2332 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2333 BOOL selectFirst, UINT wFlags )
2335 RECT rect;
2336 POPUPMENU *menu;
2337 MENUITEM *item;
2338 HDC hdc;
2340 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, selectFirst);
2342 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2344 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
2346 item = &menu->items[menu->FocusedItem];
2347 if (!(item->fType & MF_POPUP) || (item->fState & (MF_GRAYED | MF_DISABLED)))
2348 return hmenu;
2350 /* message must be sent before using item,
2351 because nearly everything may be changed by the application ! */
2353 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2354 if (!(wFlags & TPM_NONOTIFY))
2355 SendMessageW( hwndOwner, WM_INITMENUPOPUP, (WPARAM)item->hSubMenu,
2356 MAKELPARAM( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2358 item = &menu->items[menu->FocusedItem];
2359 rect = item->rect;
2361 /* correct item if modified as a reaction to WM_INITMENUPOPUP message */
2362 if (!(item->fState & MF_HILITE))
2364 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2365 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2367 SelectObject( hdc, get_menu_font(FALSE));
2369 item->fState |= MF_HILITE;
2370 MENU_DrawMenuItem( menu->hWnd, menu, hwndOwner, hdc, item, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2371 ReleaseDC( menu->hWnd, hdc );
2373 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2374 item->rect = rect;
2376 item->fState |= MF_MOUSESELECT;
2378 if (IS_SYSTEM_MENU(menu))
2380 MENU_InitSysMenuPopup(item->hSubMenu,
2381 GetWindowLongW( menu->hWnd, GWL_STYLE ),
2382 GetClassLongW( menu->hWnd, GCL_STYLE));
2384 NC_GetSysPopupPos( menu->hWnd, &rect );
2385 if (wFlags & TPM_LAYOUTRTL) rect.left = rect.right;
2386 rect.top = rect.bottom;
2387 rect.right = GetSystemMetrics(SM_CXSIZE);
2388 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2390 else
2392 RECT item_rect = item->rect;
2394 MENU_AdjustMenuItemRect(menu, &item_rect);
2395 GetWindowRect( menu->hWnd, &rect );
2397 if (menu->wFlags & MF_POPUP)
2399 /* The first item in the popup menu has to be at the
2400 same y position as the focused menu item */
2401 if (wFlags & TPM_LAYOUTRTL)
2402 rect.left += GetSystemMetrics(SM_CXBORDER);
2403 else
2404 rect.left += item_rect.right - GetSystemMetrics(SM_CXBORDER);
2405 rect.top += item_rect.top - MENU_MARGIN;
2406 rect.right = item_rect.left - item_rect.right + GetSystemMetrics(SM_CXBORDER);
2407 rect.bottom = item_rect.top - item_rect.bottom - 2 * MENU_MARGIN;
2409 else
2411 if (wFlags & TPM_LAYOUTRTL)
2412 rect.left = rect.right - item_rect.left;
2413 else
2414 rect.left += item_rect.left;
2415 rect.top += item_rect.bottom;
2416 rect.right = item_rect.right - item_rect.left;
2417 rect.bottom = item_rect.bottom - item_rect.top;
2421 /* use default alignment for submenus */
2422 wFlags &= ~(TPM_CENTERALIGN | TPM_RIGHTALIGN | TPM_VCENTERALIGN | TPM_BOTTOMALIGN);
2424 MENU_InitPopup( hwndOwner, item->hSubMenu, wFlags );
2426 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem, wFlags,
2427 rect.left, rect.top, rect.right, rect.bottom );
2428 if (selectFirst)
2429 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2430 return item->hSubMenu;
2435 /**********************************************************************
2436 * MENU_IsMenuActive
2438 HWND MENU_IsMenuActive(void)
2440 return top_popup;
2443 /**********************************************************************
2444 * MENU_EndMenu
2446 * Calls EndMenu() if the hwnd parameter belongs to the menu owner
2448 * Does the (menu stuff) of the default window handling of WM_CANCELMODE
2450 void MENU_EndMenu( HWND hwnd )
2452 POPUPMENU *menu;
2453 menu = top_popup_hmenu ? MENU_GetMenu( top_popup_hmenu ) : NULL;
2454 if (menu && (hwnd == menu->hWnd || hwnd == menu->hwndOwner)) EndMenu();
2457 /***********************************************************************
2458 * MENU_PtMenu
2460 * Walks menu chain trying to find a menu pt maps to.
2462 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2464 POPUPMENU *menu = MENU_GetMenu( hMenu );
2465 UINT item = menu->FocusedItem;
2466 HMENU ret;
2468 /* try subpopup first (if any) */
2469 ret = (item != NO_SELECTED_ITEM &&
2470 (menu->items[item].fType & MF_POPUP) &&
2471 (menu->items[item].fState & MF_MOUSESELECT))
2472 ? MENU_PtMenu(menu->items[item].hSubMenu, pt) : 0;
2474 if (!ret) /* check the current window (avoiding WM_HITTEST) */
2476 INT ht = NC_HandleNCHitTest( menu->hWnd, pt );
2477 if( menu->wFlags & MF_POPUP )
2479 if (ht != HTNOWHERE && ht != HTERROR) ret = hMenu;
2481 else if (ht == HTSYSMENU)
2482 ret = get_win_sys_menu( menu->hWnd );
2483 else if (ht == HTMENU)
2484 ret = GetMenu( menu->hWnd );
2486 return ret;
2489 /***********************************************************************
2490 * MENU_ExecFocusedItem
2492 * Execute a menu item (for instance when user pressed Enter).
2493 * Return the wID of the executed item. Otherwise, -1 indicating
2494 * that no menu item was executed, -2 if a popup is shown;
2495 * Have to receive the flags for the TrackPopupMenu options to avoid
2496 * sending unwanted message.
2499 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2501 MENUITEM *item;
2502 POPUPMENU *menu = MENU_GetMenu( hMenu );
2504 TRACE("%p hmenu=%p\n", pmt, hMenu);
2506 if (!menu || !menu->nItems ||
2507 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2509 item = &menu->items[menu->FocusedItem];
2511 TRACE("hMenu %p wID %08lx hSubMenu %p fType %04x\n", hMenu, item->wID, item->hSubMenu, item->fType);
2513 if (!(item->fType & MF_POPUP))
2515 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2517 /* If TPM_RETURNCMD is set you return the id, but
2518 do not send a message to the owner */
2519 if(!(wFlags & TPM_RETURNCMD))
2521 if( menu->wFlags & MF_SYSMENU )
2522 PostMessageW( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2523 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2524 else
2526 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2527 DWORD dwStyle = menu->dwStyle | (topmenu ? topmenu->dwStyle : 0);
2529 if (dwStyle & MNS_NOTIFYBYPOS)
2530 PostMessageW( pmt->hOwnerWnd, WM_MENUCOMMAND, menu->FocusedItem,
2531 (LPARAM)hMenu);
2532 else
2533 PostMessageW( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2536 return item->wID;
2539 else
2541 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2542 return -2;
2545 return -1;
2548 /***********************************************************************
2549 * MENU_SwitchTracking
2551 * Helper function for menu navigation routines.
2553 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id, UINT wFlags )
2555 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2556 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2558 TRACE("%p hmenu=%p 0x%04x\n", pmt, hPtMenu, id);
2560 if( pmt->hTopMenu != hPtMenu &&
2561 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2563 /* both are top level menus (system and menu-bar) */
2564 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2565 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2566 pmt->hTopMenu = hPtMenu;
2568 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2569 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2573 /***********************************************************************
2574 * MENU_ButtonDown
2576 * Return TRUE if we can go on with menu tracking.
2578 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2580 TRACE("%p hPtMenu=%p\n", pmt, hPtMenu);
2582 if (hPtMenu)
2584 UINT pos;
2585 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2586 enum hittest ht = ht_nowhere;
2588 if( IS_SYSTEM_MENU(ptmenu) )
2589 pos = 0;
2590 else
2591 ht = MENU_FindItemByCoords( ptmenu, pmt->pt, &pos );
2593 if (pos != NO_SELECTED_ITEM)
2595 if (ptmenu->FocusedItem != pos)
2596 MENU_SwitchTracking( pmt, hPtMenu, pos, wFlags );
2598 /* If the popup menu is not already "popped" */
2599 if (!(ptmenu->items[pos].fState & MF_MOUSESELECT))
2600 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2603 /* A click on an item or anywhere on a popup keeps tracking going */
2604 if (ht == ht_item || ((ptmenu->wFlags & MF_POPUP) && ht != ht_nowhere))
2605 return TRUE;
2607 return FALSE;
2610 /***********************************************************************
2611 * MENU_ButtonUp
2613 * Return the value of MENU_ExecFocusedItem if
2614 * the selected item was not a popup. Else open the popup.
2615 * A -1 return value indicates that we go on with menu tracking.
2618 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2620 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2622 if (hPtMenu)
2624 UINT pos;
2625 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2627 if( IS_SYSTEM_MENU(ptmenu) )
2628 pos = 0;
2629 else if (MENU_FindItemByCoords( ptmenu, pmt->pt, &pos ) != ht_item)
2630 pos = NO_SELECTED_ITEM;
2632 if (pos != NO_SELECTED_ITEM && (ptmenu->FocusedItem == pos))
2634 debug_print_menuitem ("FocusedItem: ", &ptmenu->items[pos], "");
2636 if (!(ptmenu->items[pos].fType & MF_POPUP))
2638 INT executedMenuId = MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2639 if (executedMenuId == -1 || executedMenuId == -2) return -1;
2640 return executedMenuId;
2643 /* If we are dealing with the menu bar */
2644 /* and this is a click on an already "popped" item: */
2645 /* Stop the menu tracking and close the opened submenus */
2646 if((pmt->hTopMenu == hPtMenu) && ptmenu->bTimeToHide)
2647 return 0;
2649 if( GetMenu(ptmenu->hWnd) == hPtMenu )
2650 ptmenu->bTimeToHide = TRUE;
2652 return -1;
2656 /***********************************************************************
2657 * MENU_MouseMove
2659 * Return TRUE if we can go on with menu tracking.
2661 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2663 UINT id = NO_SELECTED_ITEM;
2664 POPUPMENU *ptmenu = NULL;
2666 if( hPtMenu )
2668 ptmenu = MENU_GetMenu( hPtMenu );
2669 if( IS_SYSTEM_MENU(ptmenu) )
2670 id = 0;
2671 else if (MENU_FindItemByCoords( ptmenu, pmt->pt, &id ) != ht_item)
2672 id = NO_SELECTED_ITEM;
2675 if( id == NO_SELECTED_ITEM )
2677 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2678 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2681 else if( ptmenu->FocusedItem != id )
2683 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2684 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2686 return TRUE;
2690 /***********************************************************************
2691 * MENU_DoNextMenu
2693 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2695 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk, UINT wFlags )
2697 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2698 BOOL atEnd = FALSE;
2700 /* When skipping left, we need to do something special after the
2701 first menu. */
2702 if (vk == VK_LEFT && menu->FocusedItem == 0)
2704 atEnd = TRUE;
2706 /* When skipping right, for the non-system menu, we need to
2707 handle the last non-special menu item (ie skip any window
2708 icons such as MDI maximize, restore or close) */
2709 else if ((vk == VK_RIGHT) && !IS_SYSTEM_MENU(menu))
2711 UINT i = menu->FocusedItem + 1;
2712 while (i < menu->nItems) {
2713 if ((menu->items[i].wID >= SC_SIZE &&
2714 menu->items[i].wID <= SC_RESTORE)) {
2715 i++;
2716 } else break;
2718 if (i == menu->nItems) {
2719 atEnd = TRUE;
2722 /* When skipping right, we need to cater for the system menu */
2723 else if ((vk == VK_RIGHT) && IS_SYSTEM_MENU(menu))
2725 if (menu->FocusedItem == (menu->nItems - 1)) {
2726 atEnd = TRUE;
2730 if( atEnd )
2732 MDINEXTMENU next_menu;
2733 HMENU hNewMenu;
2734 HWND hNewWnd;
2735 UINT id = 0;
2737 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2738 next_menu.hmenuNext = 0;
2739 next_menu.hwndNext = 0;
2740 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2742 TRACE("%p [%p] -> %p [%p]\n",
2743 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2745 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2747 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2748 hNewWnd = pmt->hOwnerWnd;
2749 if( IS_SYSTEM_MENU(menu) )
2751 /* switch to the menu bar */
2753 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2755 if( vk == VK_LEFT )
2757 menu = MENU_GetMenu( hNewMenu );
2758 id = menu->nItems - 1;
2760 /* Skip backwards over any system predefined icons,
2761 eg. MDI close, restore etc icons */
2762 while ((id > 0) &&
2763 (menu->items[id].wID >= SC_SIZE &&
2764 menu->items[id].wID <= SC_RESTORE)) id--;
2767 else if (style & WS_SYSMENU )
2769 /* switch to the system menu */
2770 hNewMenu = get_win_sys_menu( hNewWnd );
2772 else return FALSE;
2774 else /* application returned a new menu to switch to */
2776 hNewMenu = next_menu.hmenuNext;
2777 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2779 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2781 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2783 if (style & WS_SYSMENU &&
2784 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2786 /* get the real system menu */
2787 hNewMenu = get_win_sys_menu(hNewWnd);
2789 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2791 /* FIXME: Not sure what to do here;
2792 * perhaps try to track hNewMenu as a popup? */
2794 TRACE(" -- got confused.\n");
2795 return FALSE;
2798 else return FALSE;
2801 if( hNewMenu != pmt->hTopMenu )
2803 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2804 FALSE, 0 );
2805 if( pmt->hCurrentMenu != pmt->hTopMenu )
2806 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2809 if( hNewWnd != pmt->hOwnerWnd )
2811 pmt->hOwnerWnd = hNewWnd;
2812 set_capture_window( pmt->hOwnerWnd, GUI_INMENUMODE, NULL );
2815 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2816 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2818 return TRUE;
2820 return FALSE;
2823 /***********************************************************************
2824 * MENU_SuspendPopup
2826 * The idea is not to show the popup if the next input message is
2827 * going to hide it anyway.
2829 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT uMsg )
2831 MSG msg;
2833 msg.hwnd = pmt->hOwnerWnd;
2835 PeekMessageW( &msg, 0, uMsg, uMsg, PM_NOYIELD | PM_REMOVE);
2836 pmt->trackFlags |= TF_SKIPREMOVE;
2838 switch( uMsg )
2840 case WM_KEYDOWN:
2841 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2842 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2844 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2845 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2846 if( msg.message == WM_KEYDOWN &&
2847 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2849 pmt->trackFlags |= TF_SUSPENDPOPUP;
2850 return TRUE;
2853 break;
2856 /* failures go through this */
2857 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2858 return FALSE;
2861 /***********************************************************************
2862 * MENU_KeyEscape
2864 * Handle a VK_ESCAPE key event in a menu.
2866 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2868 BOOL bEndMenu = TRUE;
2870 if (pmt->hCurrentMenu != pmt->hTopMenu)
2872 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2874 if (menu->wFlags & MF_POPUP)
2876 HMENU hmenutmp, hmenuprev;
2878 hmenuprev = hmenutmp = pmt->hTopMenu;
2880 /* close topmost popup */
2881 while (hmenutmp != pmt->hCurrentMenu)
2883 hmenuprev = hmenutmp;
2884 hmenutmp = MENU_GetSubPopup( hmenuprev );
2887 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2888 pmt->hCurrentMenu = hmenuprev;
2889 bEndMenu = FALSE;
2893 return bEndMenu;
2896 /***********************************************************************
2897 * MENU_KeyLeft
2899 * Handle a VK_LEFT key event in a menu.
2901 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags, UINT msg )
2903 POPUPMENU *menu;
2904 HMENU hmenutmp, hmenuprev;
2905 UINT prevcol;
2907 hmenuprev = hmenutmp = pmt->hTopMenu;
2908 menu = MENU_GetMenu( hmenutmp );
2910 /* Try to move 1 column left (if possible) */
2911 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2912 NO_SELECTED_ITEM ) {
2914 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2915 prevcol, TRUE, 0 );
2916 return;
2919 /* close topmost popup */
2920 while (hmenutmp != pmt->hCurrentMenu)
2922 hmenuprev = hmenutmp;
2923 hmenutmp = MENU_GetSubPopup( hmenuprev );
2926 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2927 pmt->hCurrentMenu = hmenuprev;
2929 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2931 /* move menu bar selection if no more popups are left */
2933 if( !MENU_DoNextMenu( pmt, VK_LEFT, wFlags ) )
2934 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2936 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2938 /* A sublevel menu was displayed - display the next one
2939 * unless there is another displacement coming up */
2941 if( !MENU_SuspendPopup( pmt, msg ) )
2942 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2943 pmt->hTopMenu, TRUE, wFlags);
2949 /***********************************************************************
2950 * MENU_KeyRight
2952 * Handle a VK_RIGHT key event in a menu.
2954 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags, UINT msg )
2956 HMENU hmenutmp;
2957 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2958 UINT nextcol;
2960 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2961 pmt->hCurrentMenu,
2962 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2963 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2965 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2967 /* If already displaying a popup, try to display sub-popup */
2969 hmenutmp = pmt->hCurrentMenu;
2970 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2972 /* if subpopup was displayed then we are done */
2973 if (hmenutmp != pmt->hCurrentMenu) return;
2976 /* Check to see if there's another column */
2977 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2978 NO_SELECTED_ITEM ) {
2979 TRACE("Going to %d.\n", nextcol );
2980 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2981 nextcol, TRUE, 0 );
2982 return;
2985 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2987 if( pmt->hCurrentMenu != pmt->hTopMenu )
2989 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2990 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2991 } else hmenutmp = 0;
2993 /* try to move to the next item */
2994 if( !MENU_DoNextMenu( pmt, VK_RIGHT, wFlags ) )
2995 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2997 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2998 if( !MENU_SuspendPopup( pmt, msg ) )
2999 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
3000 pmt->hTopMenu, TRUE, wFlags);
3004 static void CALLBACK release_capture( BOOL __normal )
3006 set_capture_window( 0, GUI_INMENUMODE, NULL );
3009 /***********************************************************************
3010 * MENU_TrackMenu
3012 * Menu tracking code.
3014 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
3015 HWND hwnd, const RECT *lprect )
3017 MSG msg;
3018 POPUPMENU *menu;
3019 BOOL fRemove;
3020 INT executedMenuId = -1;
3021 MTRACKER mt;
3022 BOOL enterIdleSent = FALSE;
3023 HWND capture_win;
3025 mt.trackFlags = 0;
3026 mt.hCurrentMenu = hmenu;
3027 mt.hTopMenu = hmenu;
3028 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
3029 mt.pt.x = x;
3030 mt.pt.y = y;
3032 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p %s\n",
3033 hmenu, wFlags, x, y, hwnd, wine_dbgstr_rect( lprect));
3035 if (!(menu = MENU_GetMenu( hmenu )))
3037 WARN("Invalid menu handle %p\n", hmenu);
3038 SetLastError(ERROR_INVALID_MENU_HANDLE);
3039 return FALSE;
3042 if (wFlags & TPM_BUTTONDOWN)
3044 /* Get the result in order to start the tracking or not */
3045 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3046 fEndMenu = !fRemove;
3049 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
3051 /* owner may not be visible when tracking a popup, so use the menu itself */
3052 capture_win = (wFlags & TPM_POPUPMENU) ? menu->hWnd : mt.hOwnerWnd;
3053 set_capture_window( capture_win, GUI_INMENUMODE, NULL );
3055 if ((wFlags & TPM_POPUPMENU) && menu->nItems == 0)
3056 return FALSE;
3058 __TRY while (!fEndMenu)
3060 menu = MENU_GetMenu( mt.hCurrentMenu );
3061 if (!menu) /* sometimes happens if I do a window manager close */
3062 break;
3064 /* we have to keep the message in the queue until it's
3065 * clear that menu loop is not over yet. */
3067 for (;;)
3069 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
3071 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
3072 /* remove the message from the queue */
3073 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3075 else
3077 if (!enterIdleSent)
3079 HWND win = menu->wFlags & MF_POPUP ? menu->hWnd : 0;
3080 enterIdleSent = TRUE;
3081 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
3083 WaitMessage();
3087 /* check if EndMenu() tried to cancel us, by posting this message */
3088 if(msg.message == WM_CANCELMODE)
3090 /* we are now out of the loop */
3091 fEndMenu = TRUE;
3093 /* remove the message from the queue */
3094 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3096 /* break out of internal loop, ala ESCAPE */
3097 break;
3100 mt.pt = msg.pt;
3102 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
3103 enterIdleSent=FALSE;
3105 fRemove = FALSE;
3106 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
3109 * Use the mouse coordinates in lParam instead of those in the MSG
3110 * struct to properly handle synthetic messages. They are already
3111 * in screen coordinates.
3113 mt.pt.x = (short)LOWORD(msg.lParam);
3114 mt.pt.y = (short)HIWORD(msg.lParam);
3116 /* Find a menu for this mouse event */
3117 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
3119 switch(msg.message)
3121 /* no WM_NC... messages in captured state */
3123 case WM_RBUTTONDBLCLK:
3124 case WM_RBUTTONDOWN:
3125 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3126 /* fall through */
3127 case WM_LBUTTONDBLCLK:
3128 case WM_LBUTTONDOWN:
3129 /* If the message belongs to the menu, removes it from the queue */
3130 /* Else, end menu tracking */
3131 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3132 fEndMenu = !fRemove;
3133 break;
3135 case WM_RBUTTONUP:
3136 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3137 /* fall through */
3138 case WM_LBUTTONUP:
3139 /* Check if a menu was selected by the mouse */
3140 if (hmenu)
3142 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
3143 TRACE("executedMenuId %d\n", executedMenuId);
3145 /* End the loop if executedMenuId is an item ID */
3146 /* or if the job was done (executedMenuId = 0). */
3147 fEndMenu = fRemove = (executedMenuId != -1);
3149 /* No menu was selected by the mouse */
3150 /* if the function was called by TrackPopupMenu, continue
3151 with the menu tracking. If not, stop it */
3152 else
3153 fEndMenu = !(wFlags & TPM_POPUPMENU);
3155 break;
3157 case WM_MOUSEMOVE:
3158 /* the selected menu item must be changed every time */
3159 /* the mouse moves. */
3161 if (hmenu)
3162 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
3164 } /* switch(msg.message) - mouse */
3166 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
3168 fRemove = TRUE; /* Keyboard messages are always removed */
3169 switch(msg.message)
3171 case WM_KEYDOWN:
3172 case WM_SYSKEYDOWN:
3173 switch(msg.wParam)
3175 case VK_MENU:
3176 case VK_F10:
3177 fEndMenu = TRUE;
3178 break;
3180 case VK_HOME:
3181 case VK_END:
3182 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
3183 NO_SELECTED_ITEM, FALSE, 0 );
3184 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3185 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
3186 break;
3188 case VK_UP:
3189 case VK_DOWN: /* If on menu bar, pull-down the menu */
3191 menu = MENU_GetMenu( mt.hCurrentMenu );
3192 if (!(menu->wFlags & MF_POPUP))
3193 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
3194 else /* otherwise try to move selection */
3195 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3196 (msg.wParam == VK_UP)? ITEM_PREV : ITEM_NEXT );
3197 break;
3199 case VK_LEFT:
3200 MENU_KeyLeft( &mt, wFlags, msg.message );
3201 break;
3203 case VK_RIGHT:
3204 MENU_KeyRight( &mt, wFlags, msg.message );
3205 break;
3207 case VK_ESCAPE:
3208 fEndMenu = MENU_KeyEscape(&mt, wFlags);
3209 break;
3211 case VK_F1:
3213 HELPINFO hi;
3214 hi.cbSize = sizeof(HELPINFO);
3215 hi.iContextType = HELPINFO_MENUITEM;
3216 if (menu->FocusedItem == NO_SELECTED_ITEM)
3217 hi.iCtrlId = 0;
3218 else
3219 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
3220 hi.hItemHandle = hmenu;
3221 hi.dwContextId = menu->dwContextHelpID;
3222 hi.MousePos = msg.pt;
3223 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
3224 break;
3227 default:
3228 TranslateMessage( &msg );
3229 break;
3231 break; /* WM_KEYDOWN */
3233 case WM_CHAR:
3234 case WM_SYSCHAR:
3236 UINT pos;
3238 if (msg.wParam == '\r' || msg.wParam == ' ')
3240 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3241 fEndMenu = (executedMenuId != -2);
3243 break;
3246 /* Hack to avoid control chars. */
3247 /* We will find a better way real soon... */
3248 if (msg.wParam < 32) break;
3250 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3251 LOWORD(msg.wParam), FALSE );
3252 if (pos == (UINT)-2) fEndMenu = TRUE;
3253 else if (pos == (UINT)-1) MessageBeep(0);
3254 else
3256 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3257 TRUE, 0 );
3258 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3259 fEndMenu = (executedMenuId != -2);
3262 break;
3263 } /* switch(msg.message) - kbd */
3265 else
3267 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3268 DispatchMessageW( &msg );
3269 continue;
3272 if (!fEndMenu) fRemove = TRUE;
3274 /* finally remove message from the queue */
3276 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3277 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3278 else mt.trackFlags &= ~TF_SKIPREMOVE;
3280 __FINALLY( release_capture )
3282 /* If dropdown is still painted and the close box is clicked on
3283 then the menu will be destroyed as part of the DispatchMessage above.
3284 This will then invalidate the menu handle in mt.hTopMenu. We should
3285 check for this first. */
3286 if( IsMenu( mt.hTopMenu ) )
3288 menu = MENU_GetMenu( mt.hTopMenu );
3290 if( IsWindow( mt.hOwnerWnd ) )
3292 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE, wFlags );
3294 if (menu && (menu->wFlags & MF_POPUP))
3296 DestroyWindow( menu->hWnd );
3297 menu->hWnd = 0;
3299 if (!(wFlags & TPM_NONOTIFY))
3300 SendMessageW( mt.hOwnerWnd, WM_UNINITMENUPOPUP, (WPARAM)mt.hTopMenu,
3301 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3303 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3304 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKEWPARAM(0,0xffff), 0 );
3307 /* Reset the variable for hiding menu */
3308 if( menu ) menu->bTimeToHide = FALSE;
3311 SetLastError( ERROR_SUCCESS );
3312 /* The return value is only used by TrackPopupMenu */
3313 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
3314 if (executedMenuId == -1) executedMenuId = 0;
3315 return executedMenuId;
3318 /***********************************************************************
3319 * MENU_InitTracking
3321 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3323 POPUPMENU *menu;
3325 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
3327 HideCaret(0);
3329 if (!(menu = MENU_GetMenu( hMenu ))) return FALSE;
3331 /* This makes the menus of applications built with Delphi work.
3332 * It also enables menus to be displayed in more than one window,
3333 * but there are some bugs left that need to be fixed in this case.
3335 if (!bPopup) menu->hWnd = hWnd;
3336 if (!top_popup)
3338 top_popup = menu->hWnd;
3339 top_popup_hmenu = hMenu;
3342 fEndMenu = FALSE;
3344 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3345 if (!(wFlags & TPM_NONOTIFY))
3346 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3348 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
3350 if (!(wFlags & TPM_NONOTIFY))
3352 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
3353 /* If an app changed/recreated menu bar entries in WM_INITMENU
3354 * menu sizes will be recalculated once the menu created/shown.
3358 return TRUE;
3361 /***********************************************************************
3362 * MENU_ExitTracking
3364 static BOOL MENU_ExitTracking(HWND hWnd, BOOL bPopup)
3366 TRACE("hwnd=%p\n", hWnd);
3368 SendMessageW( hWnd, WM_EXITMENULOOP, bPopup, 0 );
3369 ShowCaret(0);
3370 top_popup = 0;
3371 top_popup_hmenu = NULL;
3372 return TRUE;
3375 /***********************************************************************
3376 * MENU_TrackMouseMenuBar
3378 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3380 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3382 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3383 UINT wFlags = TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3385 TRACE("wnd=%p ht=0x%04x %s\n", hWnd, ht, wine_dbgstr_point( &pt));
3387 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) wFlags |= TPM_LAYOUTRTL;
3388 if (IsMenu(hMenu))
3390 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3392 /* fetch the window menu again, it may have changed */
3393 hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3394 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3395 MENU_ExitTracking(hWnd, FALSE);
3400 /***********************************************************************
3401 * MENU_TrackKbdMenuBar
3403 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3405 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3407 UINT uItem = NO_SELECTED_ITEM;
3408 HMENU hTrackMenu;
3409 UINT wFlags = TPM_LEFTALIGN | TPM_LEFTBUTTON;
3411 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3413 /* find window that has a menu */
3415 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3416 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3418 /* check if we have to track a system menu */
3420 hTrackMenu = GetMenu( hwnd );
3421 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3423 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3424 hTrackMenu = get_win_sys_menu( hwnd );
3425 uItem = 0;
3426 wParam |= HTSYSMENU; /* prevent item lookup */
3428 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) wFlags |= TPM_LAYOUTRTL;
3430 if (!IsMenu( hTrackMenu )) return;
3432 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3434 /* fetch the window menu again, it may have changed */
3435 hTrackMenu = (wParam & HTSYSMENU) ? get_win_sys_menu( hwnd ) : GetMenu( hwnd );
3437 if( wChar && wChar != ' ' )
3439 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3440 if ( uItem >= (UINT)(-2) )
3442 if( uItem == (UINT)(-1) ) MessageBeep(0);
3443 /* schedule end of menu tracking */
3444 wFlags |= TF_ENDMENU;
3445 goto track_menu;
3449 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3451 if (!(wParam & HTSYSMENU) || wChar == ' ')
3453 if( uItem == NO_SELECTED_ITEM )
3454 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3455 else
3456 PostMessageW( hwnd, WM_KEYDOWN, VK_RETURN, 0 );
3459 track_menu:
3460 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3461 MENU_ExitTracking( hwnd, FALSE );
3464 /**********************************************************************
3465 * TrackPopupMenuEx (USER32.@)
3467 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3468 HWND hWnd, LPTPMPARAMS lpTpm )
3470 POPUPMENU *menu;
3471 BOOL ret = FALSE;
3473 TRACE("hmenu %p flags %04x (%d,%d) hwnd %p lpTpm %p rect %s\n",
3474 hMenu, wFlags, x, y, hWnd, lpTpm,
3475 lpTpm ? wine_dbgstr_rect( &lpTpm->rcExclude) : "-" );
3477 /* Parameter check */
3478 /* FIXME: this check is performed several times, here and in the called
3479 functions. That could be optimized */
3480 if (!(menu = MENU_GetMenu( hMenu )))
3482 SetLastError( ERROR_INVALID_MENU_HANDLE );
3483 return FALSE;
3486 if (IsWindow(menu->hWnd))
3488 SetLastError( ERROR_POPUP_ALREADY_ACTIVE );
3489 return FALSE;
3492 if (MENU_InitPopup( hWnd, hMenu, wFlags ))
3494 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3496 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3497 if (!(wFlags & TPM_NONOTIFY))
3498 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3500 if (menu->wFlags & MF_SYSMENU)
3501 MENU_InitSysMenuPopup( hMenu, GetWindowLongW( hWnd, GWL_STYLE ),
3502 GetClassLongW( hWnd, GCL_STYLE));
3504 if (MENU_ShowPopup( hWnd, hMenu, 0, wFlags, x, y, 0, 0 ))
3505 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,
3506 lpTpm ? &lpTpm->rcExclude : NULL );
3507 MENU_ExitTracking(hWnd, TRUE);
3509 if (menu->hWnd)
3511 DestroyWindow( menu->hWnd );
3512 menu->hWnd = 0;
3514 if (!(wFlags & TPM_NONOTIFY))
3515 SendMessageW( hWnd, WM_UNINITMENUPOPUP, (WPARAM)hMenu,
3516 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3520 return ret;
3523 /**********************************************************************
3524 * TrackPopupMenu (USER32.@)
3526 * Like the win32 API, the function return the command ID only if the
3527 * flag TPM_RETURNCMD is on.
3530 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3531 INT nReserved, HWND hWnd, const RECT *lpRect )
3533 return TrackPopupMenuEx( hMenu, wFlags, x, y, hWnd, NULL);
3536 /***********************************************************************
3537 * PopupMenuWndProc
3539 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3541 LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3543 TRACE("hwnd=%p msg=0x%04x wp=0x%04lx lp=0x%08lx\n", hwnd, message, wParam, lParam);
3545 switch(message)
3547 case WM_CREATE:
3549 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3550 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)cs->lpCreateParams );
3551 return 0;
3554 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3555 return MA_NOACTIVATE;
3557 case WM_PAINT:
3559 PAINTSTRUCT ps;
3560 BeginPaint( hwnd, &ps );
3561 MENU_DrawPopupMenu( hwnd, ps.hdc,
3562 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3563 EndPaint( hwnd, &ps );
3564 return 0;
3567 case WM_PRINTCLIENT:
3569 MENU_DrawPopupMenu( hwnd, (HDC)wParam,
3570 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3571 return 0;
3574 case WM_ERASEBKGND:
3575 return 1;
3577 case WM_DESTROY:
3578 /* zero out global pointer in case resident popup window was destroyed. */
3579 if (hwnd == top_popup) {
3580 top_popup = 0;
3581 top_popup_hmenu = NULL;
3583 break;
3585 case WM_SHOWWINDOW:
3587 if( wParam )
3589 if (!GetWindowLongPtrW( hwnd, 0 )) ERR("no menu to display\n");
3591 else
3592 SetWindowLongPtrW( hwnd, 0, 0 );
3593 break;
3595 case MN_GETHMENU:
3596 return GetWindowLongPtrW( hwnd, 0 );
3598 default:
3599 return DefWindowProcW( hwnd, message, wParam, lParam );
3601 return 0;
3605 /***********************************************************************
3606 * MENU_GetMenuBarHeight
3608 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3610 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3611 INT orgX, INT orgY )
3613 HDC hdc;
3614 RECT rectBar;
3615 LPPOPUPMENU lppop;
3617 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3619 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3621 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3622 SelectObject( hdc, get_menu_font(FALSE));
3623 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3624 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3625 ReleaseDC( hwnd, hdc );
3626 return lppop->Height;
3630 /*******************************************************************
3631 * ChangeMenuA (USER32.@)
3633 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3634 UINT id, UINT flags )
3636 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3637 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3638 id, data );
3639 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3640 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3641 id, data );
3642 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3643 flags & MF_BYPOSITION ? pos : id,
3644 flags & ~MF_REMOVE );
3645 /* Default: MF_INSERT */
3646 return InsertMenuA( hMenu, pos, flags, id, data );
3650 /*******************************************************************
3651 * ChangeMenuW (USER32.@)
3653 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3654 UINT id, UINT flags )
3656 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3657 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3658 id, data );
3659 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3660 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3661 id, data );
3662 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3663 flags & MF_BYPOSITION ? pos : id,
3664 flags & ~MF_REMOVE );
3665 /* Default: MF_INSERT */
3666 return InsertMenuW( hMenu, pos, flags, id, data );
3670 /*******************************************************************
3671 * CheckMenuItem (USER32.@)
3673 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3675 MENUITEM *item;
3676 DWORD ret;
3678 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3679 ret = item->fState & MF_CHECKED;
3680 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3681 else item->fState &= ~MF_CHECKED;
3682 return ret;
3686 /**********************************************************************
3687 * EnableMenuItem (USER32.@)
3689 BOOL WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3691 UINT oldflags;
3692 MENUITEM *item;
3693 POPUPMENU *menu;
3695 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3697 /* Get the Popupmenu to access the owner menu */
3698 if (!(menu = MENU_GetMenu(hMenu)))
3699 return (UINT)-1;
3701 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3702 return (UINT)-1;
3704 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3705 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3707 /* If the close item in the system menu change update the close button */
3708 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3710 if (menu->hSysMenuOwner != 0)
3712 RECT rc;
3713 POPUPMENU* parentMenu;
3715 /* Get the parent menu to access*/
3716 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3717 return (UINT)-1;
3719 /* Refresh the frame to reflect the change */
3720 WIN_GetRectangles( parentMenu->hWnd, COORDS_CLIENT, &rc, NULL );
3721 rc.bottom = 0;
3722 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3726 return oldflags;
3730 /*******************************************************************
3731 * GetMenuStringA (USER32.@)
3733 INT WINAPI GetMenuStringA(
3734 HMENU hMenu, /* [in] menuhandle */
3735 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3736 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3737 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3738 UINT wFlags /* [in] MF_ flags */
3740 MENUITEM *item;
3742 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3743 if (str && nMaxSiz) str[0] = '\0';
3744 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3745 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3746 return 0;
3748 if (!item->text) return 0;
3749 if (!str || !nMaxSiz) return WideCharToMultiByte( CP_ACP, 0, item->text, -1, NULL, 0, NULL, NULL );
3750 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3751 str[nMaxSiz-1] = 0;
3752 TRACE("returning %s\n", debugstr_a(str));
3753 return strlen(str);
3757 /*******************************************************************
3758 * GetMenuStringW (USER32.@)
3760 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3761 LPWSTR str, INT nMaxSiz, UINT wFlags )
3763 MENUITEM *item;
3765 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3766 if (str && nMaxSiz) str[0] = '\0';
3767 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3768 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3769 return 0;
3771 if (!str || !nMaxSiz) return item->text ? strlenW(item->text) : 0;
3772 if( !(item->text)) {
3773 str[0] = 0;
3774 return 0;
3776 lstrcpynW( str, item->text, nMaxSiz );
3777 TRACE("returning %s\n", debugstr_w(str));
3778 return strlenW(str);
3782 /**********************************************************************
3783 * HiliteMenuItem (USER32.@)
3785 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3786 UINT wHilite )
3788 LPPOPUPMENU menu;
3789 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3790 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3791 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3792 if (menu->FocusedItem == wItemID) return TRUE;
3793 MENU_HideSubPopups( hWnd, hMenu, FALSE, 0 );
3794 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3795 return TRUE;
3799 /**********************************************************************
3800 * GetMenuState (USER32.@)
3802 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3804 MENUITEM *item;
3805 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3806 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3807 debug_print_menuitem (" item: ", item, "");
3808 if (item->fType & MF_POPUP)
3810 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3811 if (!menu) return -1;
3812 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3814 else
3816 /* We used to (from way back then) mask the result to 0xff. */
3817 /* I don't know why and it seems wrong as the documented */
3818 /* return flag MF_SEPARATOR is outside that mask. */
3819 return (item->fType | item->fState);
3824 /**********************************************************************
3825 * GetMenuItemCount (USER32.@)
3827 INT WINAPI GetMenuItemCount( HMENU hMenu )
3829 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3830 if (!menu) return -1;
3831 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3832 return menu->nItems;
3836 /**********************************************************************
3837 * GetMenuItemID (USER32.@)
3839 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3841 MENUITEM * lpmi;
3843 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
3844 if (lpmi->fType & MF_POPUP) return -1;
3845 return lpmi->wID;
3850 /**********************************************************************
3851 * MENU_mnu2mnuii
3853 * Uses flags, id and text ptr, passed by InsertMenu() and
3854 * ModifyMenu() to setup a MenuItemInfo structure.
3856 static void MENU_mnu2mnuii( UINT flags, UINT_PTR id, LPCWSTR str,
3857 LPMENUITEMINFOW pmii)
3859 ZeroMemory( pmii, sizeof( MENUITEMINFOW));
3860 pmii->cbSize = sizeof( MENUITEMINFOW);
3861 pmii->fMask = MIIM_STATE | MIIM_ID | MIIM_FTYPE;
3862 /* setting bitmap clears text and vice versa */
3863 if( IS_STRING_ITEM(flags)) {
3864 pmii->fMask |= MIIM_STRING | MIIM_BITMAP;
3865 if( !str)
3866 flags |= MF_SEPARATOR;
3867 /* Item beginning with a backspace is a help item */
3868 /* FIXME: wrong place, this is only true in win16 */
3869 else if( *str == '\b') {
3870 flags |= MF_HELP;
3871 str++;
3873 pmii->dwTypeData = (LPWSTR)str;
3874 } else if( flags & MFT_BITMAP){
3875 pmii->fMask |= MIIM_BITMAP | MIIM_STRING;
3876 pmii->hbmpItem = (HBITMAP)str;
3878 if( flags & MF_OWNERDRAW){
3879 pmii->fMask |= MIIM_DATA;
3880 pmii->dwItemData = (ULONG_PTR) str;
3882 if( flags & MF_POPUP && MENU_GetMenu((HMENU)id)) {
3883 pmii->fMask |= MIIM_SUBMENU;
3884 pmii->hSubMenu = (HMENU)id;
3886 if( flags & MF_SEPARATOR) flags |= MF_GRAYED | MF_DISABLED;
3887 pmii->fState = flags & MENUITEMINFO_STATE_MASK & ~MFS_DEFAULT;
3888 pmii->fType = flags & MENUITEMINFO_TYPE_MASK;
3889 pmii->wID = (UINT)id;
3893 /*******************************************************************
3894 * InsertMenuW (USER32.@)
3896 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3897 UINT_PTR id, LPCWSTR str )
3899 MENUITEM *item;
3900 MENUITEMINFOW mii;
3902 if (IS_STRING_ITEM(flags) && str)
3903 TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %s\n",
3904 hMenu, pos, flags, id, debugstr_w(str) );
3905 else TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %p (not a string)\n",
3906 hMenu, pos, flags, id, str );
3908 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3909 MENU_mnu2mnuii( flags, id, str, &mii);
3910 if (!(SetMenuItemInfo_common( item, &mii, TRUE)))
3912 RemoveMenu( hMenu, pos, flags );
3913 return FALSE;
3916 item->hCheckBit = item->hUnCheckBit = 0;
3917 return TRUE;
3921 /*******************************************************************
3922 * InsertMenuA (USER32.@)
3924 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3925 UINT_PTR id, LPCSTR str )
3927 BOOL ret = FALSE;
3929 if (IS_STRING_ITEM(flags) && str)
3931 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3932 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3933 if (newstr)
3935 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3936 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3937 HeapFree( GetProcessHeap(), 0, newstr );
3939 return ret;
3941 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3945 /*******************************************************************
3946 * AppendMenuA (USER32.@)
3948 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3949 UINT_PTR id, LPCSTR data )
3951 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3955 /*******************************************************************
3956 * AppendMenuW (USER32.@)
3958 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3959 UINT_PTR id, LPCWSTR data )
3961 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3965 /**********************************************************************
3966 * RemoveMenu (USER32.@)
3968 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3970 LPPOPUPMENU menu;
3971 MENUITEM *item;
3973 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3974 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3975 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3977 /* Remove item */
3979 MENU_FreeItemData( item );
3981 if (--menu->nItems == 0)
3983 HeapFree( GetProcessHeap(), 0, menu->items );
3984 menu->items = NULL;
3986 else
3988 while(nPos < menu->nItems)
3990 *item = *(item+1);
3991 item++;
3992 nPos++;
3994 menu->items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
3995 menu->nItems * sizeof(MENUITEM) );
3997 return TRUE;
4001 /**********************************************************************
4002 * DeleteMenu (USER32.@)
4004 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
4006 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
4007 if (!item) return FALSE;
4008 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
4009 /* nPos is now the position of the item */
4010 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
4011 return TRUE;
4015 /*******************************************************************
4016 * ModifyMenuW (USER32.@)
4018 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
4019 UINT_PTR id, LPCWSTR str )
4021 MENUITEM *item;
4022 MENUITEMINFOW mii;
4024 if (IS_STRING_ITEM(flags))
4025 TRACE("%p %d %04x %04lx %s\n", hMenu, pos, flags, id, debugstr_w(str) );
4026 else
4027 TRACE("%p %d %04x %04lx %p\n", hMenu, pos, flags, id, str );
4029 if (!(item = MENU_FindItem( &hMenu, &pos, flags )))
4031 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4032 if (pos == SC_TASKLIST && !(flags & MF_BYPOSITION)) return TRUE;
4033 return FALSE;
4035 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
4036 MENU_mnu2mnuii( flags, id, str, &mii);
4037 return SetMenuItemInfo_common( item, &mii, TRUE);
4041 /*******************************************************************
4042 * ModifyMenuA (USER32.@)
4044 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
4045 UINT_PTR id, LPCSTR str )
4047 BOOL ret = FALSE;
4049 if (IS_STRING_ITEM(flags) && str)
4051 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4052 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
4053 if (newstr)
4055 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
4056 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
4057 HeapFree( GetProcessHeap(), 0, newstr );
4059 return ret;
4061 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
4065 /**********************************************************************
4066 * CreatePopupMenu (USER32.@)
4068 HMENU WINAPI CreatePopupMenu(void)
4070 HMENU hmenu;
4071 POPUPMENU *menu;
4073 if (!(hmenu = CreateMenu())) return 0;
4074 menu = MENU_GetMenu( hmenu );
4075 menu->wFlags |= MF_POPUP;
4076 menu->bTimeToHide = FALSE;
4077 return hmenu;
4081 /**********************************************************************
4082 * GetMenuCheckMarkDimensions (USER.417)
4083 * GetMenuCheckMarkDimensions (USER32.@)
4085 DWORD WINAPI GetMenuCheckMarkDimensions(void)
4087 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
4091 /**********************************************************************
4092 * SetMenuItemBitmaps (USER32.@)
4094 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
4095 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
4097 MENUITEM *item;
4099 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
4101 if (!hNewCheck && !hNewUnCheck)
4103 item->fState &= ~MF_USECHECKBITMAPS;
4105 else /* Install new bitmaps */
4107 item->hCheckBit = hNewCheck;
4108 item->hUnCheckBit = hNewUnCheck;
4109 item->fState |= MF_USECHECKBITMAPS;
4111 return TRUE;
4115 /**********************************************************************
4116 * CreateMenu (USER32.@)
4118 HMENU WINAPI CreateMenu(void)
4120 HMENU hMenu;
4121 LPPOPUPMENU menu;
4123 if (!(menu = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*menu) ))) return 0;
4124 menu->FocusedItem = NO_SELECTED_ITEM;
4125 menu->bTimeToHide = FALSE;
4127 if (!(hMenu = alloc_user_handle( &menu->obj, USER_MENU ))) HeapFree( GetProcessHeap(), 0, menu );
4129 TRACE("return %p\n", hMenu );
4131 return hMenu;
4135 /**********************************************************************
4136 * DestroyMenu (USER32.@)
4138 BOOL WINAPI DestroyMenu( HMENU hMenu )
4140 LPPOPUPMENU lppop;
4142 TRACE("(%p)\n", hMenu);
4144 if (!(lppop = free_user_handle( hMenu, USER_MENU ))) return FALSE;
4145 if (lppop == OBJ_OTHER_PROCESS) return FALSE;
4147 /* DestroyMenu should not destroy system menu popup owner */
4148 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
4150 DestroyWindow( lppop->hWnd );
4151 lppop->hWnd = 0;
4154 if (lppop->items) /* recursively destroy submenus */
4156 int i;
4157 MENUITEM *item = lppop->items;
4158 for (i = lppop->nItems; i > 0; i--, item++)
4160 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
4161 MENU_FreeItemData( item );
4163 HeapFree( GetProcessHeap(), 0, lppop->items );
4165 HeapFree( GetProcessHeap(), 0, lppop );
4166 return TRUE;
4170 /**********************************************************************
4171 * GetSystemMenu (USER32.@)
4173 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4175 WND *wndPtr = WIN_GetPtr( hWnd );
4176 HMENU retvalue = 0;
4178 if (wndPtr == WND_DESKTOP) return 0;
4179 if (wndPtr == WND_OTHER_PROCESS)
4181 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
4183 else if (wndPtr)
4185 if (wndPtr->hSysMenu && bRevert)
4187 DestroyMenu(wndPtr->hSysMenu);
4188 wndPtr->hSysMenu = 0;
4191 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
4192 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, 0 );
4194 if( wndPtr->hSysMenu )
4196 POPUPMENU *menu;
4197 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
4199 /* Store the dummy sysmenu handle to facilitate the refresh */
4200 /* of the close button if the SC_CLOSE item change */
4201 menu = MENU_GetMenu(retvalue);
4202 if ( menu )
4203 menu->hSysMenuOwner = wndPtr->hSysMenu;
4205 WIN_ReleasePtr( wndPtr );
4207 return bRevert ? 0 : retvalue;
4211 /*******************************************************************
4212 * SetSystemMenu (USER32.@)
4214 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4216 WND *wndPtr = WIN_GetPtr( hwnd );
4218 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
4220 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
4221 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
4222 WIN_ReleasePtr( wndPtr );
4223 return TRUE;
4225 return FALSE;
4229 /**********************************************************************
4230 * GetMenu (USER32.@)
4232 HMENU WINAPI GetMenu( HWND hWnd )
4234 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
4235 TRACE("for %p returning %p\n", hWnd, retvalue);
4236 return retvalue;
4239 /**********************************************************************
4240 * GetMenuBarInfo (USER32.@)
4242 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
4244 POPUPMENU *menu;
4245 HMENU hmenu = NULL;
4246 ATOM class_atom;
4248 TRACE( "(%p,0x%08x,0x%08x,%p)\n", hwnd, idObject, idItem, pmbi );
4250 switch (idObject)
4252 case OBJID_CLIENT:
4253 class_atom = GetClassLongW(hwnd, GCW_ATOM);
4254 if (!class_atom)
4255 return FALSE;
4256 if (class_atom != POPUPMENU_CLASS_ATOM)
4258 WARN("called on invalid window: %d\n", class_atom);
4259 SetLastError(ERROR_INVALID_MENU_HANDLE);
4260 return FALSE;
4263 hmenu = (HMENU)GetWindowLongPtrW(hwnd, 0);
4264 break;
4265 case OBJID_MENU:
4266 hmenu = GetMenu(hwnd);
4267 break;
4268 case OBJID_SYSMENU:
4269 hmenu = GetSystemMenu(hwnd, FALSE);
4270 break;
4271 default:
4272 return FALSE;
4275 if (!hmenu)
4276 return FALSE;
4278 if (pmbi->cbSize != sizeof(MENUBARINFO))
4280 SetLastError(ERROR_INVALID_PARAMETER);
4281 return FALSE;
4284 menu = MENU_GetMenu(hmenu);
4285 if (!menu)
4286 return FALSE;
4287 if (idItem < 0 || idItem > menu->nItems)
4288 return FALSE;
4290 if (!menu->Height)
4292 SetRectEmpty(&pmbi->rcBar);
4294 else if (idItem == 0)
4296 GetMenuItemRect(hwnd, hmenu, 0, &pmbi->rcBar);
4297 pmbi->rcBar.right = pmbi->rcBar.left + menu->Width;
4298 pmbi->rcBar.bottom = pmbi->rcBar.top + menu->Height;
4300 else
4302 GetMenuItemRect(hwnd, hmenu, idItem - 1, &pmbi->rcBar);
4305 pmbi->hMenu = hmenu;
4306 pmbi->hwndMenu = NULL;
4307 pmbi->fBarFocused = top_popup_hmenu == hmenu;
4308 if (idItem)
4310 pmbi->fFocused = menu->FocusedItem == idItem - 1;
4311 if (pmbi->fFocused && (menu->items[idItem - 1].fType & MF_POPUP))
4313 menu = MENU_GetMenu(menu->items[idItem - 1].hSubMenu);
4314 if (menu)
4315 pmbi->hwndMenu = menu->hWnd;
4318 else
4320 pmbi->fFocused = pmbi->fBarFocused;
4323 return TRUE;
4326 /**********************************************************************
4327 * MENU_SetMenu
4329 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
4330 * SetWindowPos call that would result if SetMenu were called directly.
4332 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
4334 TRACE("(%p, %p);\n", hWnd, hMenu);
4336 if (hMenu && !IsMenu(hMenu))
4338 WARN("hMenu %p is not a menu handle\n", hMenu);
4339 return FALSE;
4341 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4342 return FALSE;
4344 hWnd = WIN_GetFullHandle( hWnd );
4345 if (GetCapture() == hWnd)
4346 set_capture_window( 0, GUI_INMENUMODE, NULL ); /* release the capture */
4348 if (hMenu != 0)
4350 LPPOPUPMENU lpmenu;
4352 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
4354 lpmenu->hWnd = hWnd;
4355 lpmenu->Height = 0; /* Make sure we recalculate the size */
4357 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
4358 return TRUE;
4362 /**********************************************************************
4363 * SetMenu (USER32.@)
4365 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4367 if(!MENU_SetMenu(hWnd, hMenu))
4368 return FALSE;
4370 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4371 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4372 return TRUE;
4376 /**********************************************************************
4377 * GetSubMenu (USER32.@)
4379 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4381 MENUITEM * lpmi;
4383 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4384 if (!(lpmi->fType & MF_POPUP)) return 0;
4385 return lpmi->hSubMenu;
4389 /**********************************************************************
4390 * DrawMenuBar (USER32.@)
4392 BOOL WINAPI DrawMenuBar( HWND hWnd )
4394 LPPOPUPMENU lppop;
4395 HMENU hMenu;
4397 if (!IsWindow( hWnd ))
4398 return FALSE;
4399 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4400 return TRUE;
4402 if ((hMenu = GetMenu( hWnd )) && (lppop = MENU_GetMenu( hMenu ))) {
4403 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4404 lppop->hwndOwner = hWnd;
4407 return SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4408 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4411 /***********************************************************************
4412 * DrawMenuBarTemp (USER32.@)
4414 * UNDOCUMENTED !!
4416 * called by W98SE desk.cpl Control Panel Applet
4418 * Not 100% sure about the param names, but close.
4420 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
4422 LPPOPUPMENU lppop;
4423 UINT i,retvalue;
4424 HFONT hfontOld = 0;
4425 BOOL flat_menu = FALSE;
4427 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
4429 if (!hMenu)
4430 hMenu = GetMenu(hwnd);
4432 if (!hFont)
4433 hFont = get_menu_font(FALSE);
4435 lppop = MENU_GetMenu( hMenu );
4436 if (lppop == NULL || lprect == NULL)
4438 retvalue = GetSystemMetrics(SM_CYMENU);
4439 goto END;
4442 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
4444 hfontOld = SelectObject( hDC, hFont);
4446 if (lppop->Height == 0)
4447 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
4449 lprect->bottom = lprect->top + lppop->Height;
4451 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
4453 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
4454 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
4455 LineTo( hDC, lprect->right, lprect->bottom );
4457 if (lppop->nItems == 0)
4459 retvalue = GetSystemMetrics(SM_CYMENU);
4460 goto END;
4463 for (i = 0; i < lppop->nItems; i++)
4464 MENU_DrawMenuItem( hwnd, lppop, hwnd, hDC, &lppop->items[i], TRUE, ODA_DRAWENTIRE );
4466 retvalue = lppop->Height;
4468 END:
4469 if (hfontOld) SelectObject (hDC, hfontOld);
4470 return retvalue;
4473 /***********************************************************************
4474 * EndMenu (USER.187)
4475 * EndMenu (USER32.@)
4477 BOOL WINAPI EndMenu(void)
4479 /* if we are in the menu code, and it is active */
4480 if (!fEndMenu && top_popup)
4482 /* terminate the menu handling code */
4483 fEndMenu = TRUE;
4485 /* needs to be posted to wakeup the internal menu handler */
4486 /* which will now terminate the menu, in the event that */
4487 /* the main window was minimized, or lost focus, so we */
4488 /* don't end up with an orphaned menu */
4489 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
4491 return fEndMenu;
4495 /*****************************************************************
4496 * LoadMenuA (USER32.@)
4498 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4500 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
4501 if (!hrsrc) return 0;
4502 return LoadMenuIndirectA( LoadResource( instance, hrsrc ));
4506 /*****************************************************************
4507 * LoadMenuW (USER32.@)
4509 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4511 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
4512 if (!hrsrc) return 0;
4513 return LoadMenuIndirectW( LoadResource( instance, hrsrc ));
4517 /**********************************************************************
4518 * LoadMenuIndirectW (USER32.@)
4520 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4522 HMENU hMenu;
4523 WORD version, offset;
4524 LPCSTR p = template;
4526 version = GET_WORD(p);
4527 p += sizeof(WORD);
4528 TRACE("%p, ver %d\n", template, version );
4529 switch (version)
4531 case 0: /* standard format is version of 0 */
4532 offset = GET_WORD(p);
4533 p += sizeof(WORD) + offset;
4534 if (!(hMenu = CreateMenu())) return 0;
4535 if (!MENU_ParseResource( p, hMenu ))
4537 DestroyMenu( hMenu );
4538 return 0;
4540 return hMenu;
4541 case 1: /* extended format is version of 1 */
4542 offset = GET_WORD(p);
4543 p += sizeof(WORD) + offset;
4544 if (!(hMenu = CreateMenu())) return 0;
4545 if (!MENUEX_ParseResource( p, hMenu))
4547 DestroyMenu( hMenu );
4548 return 0;
4550 return hMenu;
4551 default:
4552 ERR("version %d not supported.\n", version);
4553 return 0;
4558 /**********************************************************************
4559 * LoadMenuIndirectA (USER32.@)
4561 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4563 return LoadMenuIndirectW( template );
4567 /**********************************************************************
4568 * IsMenu (USER32.@)
4570 BOOL WINAPI IsMenu(HMENU hmenu)
4572 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4574 if (!menu)
4576 SetLastError(ERROR_INVALID_MENU_HANDLE);
4577 return FALSE;
4579 return TRUE;
4582 /**********************************************************************
4583 * GetMenuItemInfo_common
4586 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4587 LPMENUITEMINFOW lpmii, BOOL unicode)
4589 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos ? MF_BYPOSITION : 0);
4591 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4593 if (!menu) {
4594 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
4595 return FALSE;
4598 if( lpmii->fMask & MIIM_TYPE) {
4599 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4600 WARN("invalid combination of fMask bits used\n");
4601 /* this does not happen on Win9x/ME */
4602 SetLastError( ERROR_INVALID_PARAMETER);
4603 return FALSE;
4605 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4606 if (menu->hbmpItem && !IS_MAGIC_BITMAP(menu->hbmpItem))
4607 lpmii->fType |= MFT_BITMAP;
4608 lpmii->hbmpItem = menu->hbmpItem; /* not on Win9x/ME */
4609 if( lpmii->fType & MFT_BITMAP) {
4610 lpmii->dwTypeData = (LPWSTR) menu->hbmpItem;
4611 lpmii->cch = 0;
4612 } else if( lpmii->fType & (MFT_OWNERDRAW | MFT_SEPARATOR)) {
4613 /* this does not happen on Win9x/ME */
4614 lpmii->dwTypeData = 0;
4615 lpmii->cch = 0;
4619 /* copy the text string */
4620 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING))) {
4621 if( !menu->text ) {
4622 if(lpmii->dwTypeData && lpmii->cch) {
4623 if( unicode)
4624 *((WCHAR *)lpmii->dwTypeData) = 0;
4625 else
4626 *((CHAR *)lpmii->dwTypeData) = 0;
4628 lpmii->cch = 0;
4629 } else {
4630 int len;
4631 if (unicode)
4633 len = strlenW(menu->text);
4634 if(lpmii->dwTypeData && lpmii->cch)
4635 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4637 else
4639 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL,
4640 0, NULL, NULL ) - 1;
4641 if(lpmii->dwTypeData && lpmii->cch)
4642 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4643 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4644 ((LPSTR)lpmii->dwTypeData)[lpmii->cch - 1] = 0;
4646 /* if we've copied a substring we return its length */
4647 if(lpmii->dwTypeData && lpmii->cch)
4648 if (lpmii->cch <= len + 1)
4649 lpmii->cch--;
4650 else
4651 lpmii->cch = len;
4652 else {
4653 /* return length of string */
4654 /* not on Win9x/ME if fType & MFT_BITMAP */
4655 lpmii->cch = len;
4660 if (lpmii->fMask & MIIM_FTYPE)
4661 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4663 if (lpmii->fMask & MIIM_BITMAP)
4664 lpmii->hbmpItem = menu->hbmpItem;
4666 if (lpmii->fMask & MIIM_STATE)
4667 lpmii->fState = menu->fState & MENUITEMINFO_STATE_MASK;
4669 if (lpmii->fMask & MIIM_ID)
4670 lpmii->wID = menu->wID;
4672 if (lpmii->fMask & MIIM_SUBMENU)
4673 lpmii->hSubMenu = menu->hSubMenu;
4674 else {
4675 /* hSubMenu is always cleared
4676 * (not on Win9x/ME ) */
4677 lpmii->hSubMenu = 0;
4680 if (lpmii->fMask & MIIM_CHECKMARKS) {
4681 lpmii->hbmpChecked = menu->hCheckBit;
4682 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4684 if (lpmii->fMask & MIIM_DATA)
4685 lpmii->dwItemData = menu->dwItemData;
4687 return TRUE;
4690 /**********************************************************************
4691 * GetMenuItemInfoA (USER32.@)
4693 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4694 LPMENUITEMINFOA lpmii)
4696 BOOL ret;
4697 MENUITEMINFOA mii;
4698 if( lpmii->cbSize != sizeof( mii) &&
4699 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4700 SetLastError( ERROR_INVALID_PARAMETER);
4701 return FALSE;
4703 memcpy( &mii, lpmii, lpmii->cbSize);
4704 mii.cbSize = sizeof( mii);
4705 ret = GetMenuItemInfo_common (hmenu, item, bypos,
4706 (LPMENUITEMINFOW)&mii, FALSE);
4707 mii.cbSize = lpmii->cbSize;
4708 memcpy( lpmii, &mii, mii.cbSize);
4709 return ret;
4712 /**********************************************************************
4713 * GetMenuItemInfoW (USER32.@)
4715 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4716 LPMENUITEMINFOW lpmii)
4718 BOOL ret;
4719 MENUITEMINFOW mii;
4720 if( lpmii->cbSize != sizeof( mii) &&
4721 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4722 SetLastError( ERROR_INVALID_PARAMETER);
4723 return FALSE;
4725 memcpy( &mii, lpmii, lpmii->cbSize);
4726 mii.cbSize = sizeof( mii);
4727 ret = GetMenuItemInfo_common (hmenu, item, bypos, &mii, TRUE);
4728 mii.cbSize = lpmii->cbSize;
4729 memcpy( lpmii, &mii, mii.cbSize);
4730 return ret;
4734 /* set a menu item text from a ASCII or Unicode string */
4735 static inline void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4737 if (!text)
4738 menu->text = NULL;
4739 else if (unicode)
4741 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4742 strcpyW( menu->text, text );
4744 else
4746 LPCSTR str = (LPCSTR)text;
4747 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4748 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4749 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4754 /**********************************************************************
4755 * MENU_depth
4757 * detect if there are loops in the menu tree (or the depth is too large)
4759 static int MENU_depth( POPUPMENU *pmenu, int depth)
4761 UINT i;
4762 MENUITEM *item;
4763 int subdepth;
4765 depth++;
4766 if( depth > MAXMENUDEPTH) return depth;
4767 item = pmenu->items;
4768 subdepth = depth;
4769 for( i = 0; i < pmenu->nItems && subdepth <= MAXMENUDEPTH; i++, item++){
4770 POPUPMENU *psubmenu = item->hSubMenu ? MENU_GetMenu( item->hSubMenu) : NULL;
4771 if( psubmenu){
4772 int bdepth = MENU_depth( psubmenu, depth);
4773 if( bdepth > subdepth) subdepth = bdepth;
4775 if( subdepth > MAXMENUDEPTH)
4776 TRACE("<- hmenu %p\n", item->hSubMenu);
4778 return subdepth;
4782 /**********************************************************************
4783 * SetMenuItemInfo_common
4785 * Note: does not support the MIIM_TYPE flag. Use the MIIM_FTYPE,
4786 * MIIM_BITMAP and MIIM_STRING flags instead.
4789 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4790 const MENUITEMINFOW *lpmii,
4791 BOOL unicode)
4793 if (!menu) return FALSE;
4795 debug_print_menuitem("SetMenuItemInfo_common from: ", menu, "");
4797 if (lpmii->fMask & MIIM_FTYPE ) {
4798 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4799 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4801 if (lpmii->fMask & MIIM_STRING ) {
4802 /* free the string when used */
4803 HeapFree(GetProcessHeap(), 0, menu->text);
4804 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4807 if (lpmii->fMask & MIIM_STATE)
4808 /* Other menu items having MFS_DEFAULT are not converted
4809 to normal items */
4810 menu->fState = lpmii->fState & MENUITEMINFO_STATE_MASK;
4812 if (lpmii->fMask & MIIM_ID)
4813 menu->wID = lpmii->wID;
4815 if (lpmii->fMask & MIIM_SUBMENU) {
4816 menu->hSubMenu = lpmii->hSubMenu;
4817 if (menu->hSubMenu) {
4818 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4819 if (subMenu) {
4820 if( MENU_depth( subMenu, 0) > MAXMENUDEPTH) {
4821 ERR( "Loop detected in menu hierarchy or maximum menu depth exceeded!\n");
4822 menu->hSubMenu = 0;
4823 return FALSE;
4825 subMenu->wFlags |= MF_POPUP;
4826 menu->fType |= MF_POPUP;
4827 } else {
4828 SetLastError( ERROR_INVALID_PARAMETER);
4829 return FALSE;
4832 else
4833 menu->fType &= ~MF_POPUP;
4836 if (lpmii->fMask & MIIM_CHECKMARKS)
4838 menu->hCheckBit = lpmii->hbmpChecked;
4839 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4841 if (lpmii->fMask & MIIM_DATA)
4842 menu->dwItemData = lpmii->dwItemData;
4844 if (lpmii->fMask & MIIM_BITMAP)
4845 menu->hbmpItem = lpmii->hbmpItem;
4847 if( !menu->text && !(menu->fType & MFT_OWNERDRAW) && !menu->hbmpItem)
4848 menu->fType |= MFT_SEPARATOR;
4850 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4851 return TRUE;
4854 /**********************************************************************
4855 * MENU_NormalizeMenuItemInfoStruct
4857 * Helper for SetMenuItemInfo and InsertMenuItemInfo:
4858 * check, copy and extend the MENUITEMINFO struct from the version that the application
4859 * supplied to the version used by wine source. */
4860 static BOOL MENU_NormalizeMenuItemInfoStruct( const MENUITEMINFOW *pmii_in,
4861 MENUITEMINFOW *pmii_out )
4863 /* do we recognize the size? */
4864 if( !pmii_in || (pmii_in->cbSize != sizeof( MENUITEMINFOW) &&
4865 pmii_in->cbSize != sizeof( MENUITEMINFOW) - sizeof( pmii_in->hbmpItem)) ) {
4866 SetLastError( ERROR_INVALID_PARAMETER);
4867 return FALSE;
4869 /* copy the fields that we have */
4870 memcpy( pmii_out, pmii_in, pmii_in->cbSize);
4871 /* if the hbmpItem member is missing then extend */
4872 if( pmii_in->cbSize != sizeof( MENUITEMINFOW)) {
4873 pmii_out->cbSize = sizeof( MENUITEMINFOW);
4874 pmii_out->hbmpItem = NULL;
4876 /* test for invalid bit combinations */
4877 if( (pmii_out->fMask & MIIM_TYPE &&
4878 pmii_out->fMask & (MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) ||
4879 (pmii_out->fMask & MIIM_FTYPE && pmii_out->fType & MFT_BITMAP)) {
4880 WARN("invalid combination of fMask bits used\n");
4881 /* this does not happen on Win9x/ME */
4882 SetLastError( ERROR_INVALID_PARAMETER);
4883 return FALSE;
4885 /* convert old style (MIIM_TYPE) to the new */
4886 if( pmii_out->fMask & MIIM_TYPE){
4887 pmii_out->fMask |= MIIM_FTYPE;
4888 if( IS_STRING_ITEM(pmii_out->fType)){
4889 pmii_out->fMask |= MIIM_STRING;
4890 } else if( (pmii_out->fType) & MFT_BITMAP){
4891 pmii_out->fMask |= MIIM_BITMAP;
4892 pmii_out->hbmpItem = UlongToHandle(LOWORD(pmii_out->dwTypeData));
4895 return TRUE;
4898 /**********************************************************************
4899 * SetMenuItemInfoA (USER32.@)
4901 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4902 const MENUITEMINFOA *lpmii)
4904 MENUITEM *menuitem;
4905 MENUITEMINFOW mii;
4907 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4909 if (!MENU_NormalizeMenuItemInfoStruct( (const MENUITEMINFOW *)lpmii, &mii )) return FALSE;
4911 if (!(menuitem = MENU_FindItem( &hmenu, &item, bypos? MF_BYPOSITION : 0 )))
4913 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4914 if (item == SC_TASKLIST && !bypos) return TRUE;
4915 return FALSE;
4917 return SetMenuItemInfo_common( menuitem, &mii, FALSE );
4920 /**********************************************************************
4921 * SetMenuItemInfoW (USER32.@)
4923 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4924 const MENUITEMINFOW *lpmii)
4926 MENUITEM *menuitem;
4927 MENUITEMINFOW mii;
4929 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4931 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
4932 if (!(menuitem = MENU_FindItem( &hmenu, &item, bypos? MF_BYPOSITION : 0 )))
4934 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4935 if (item == SC_TASKLIST && !bypos) return TRUE;
4936 return FALSE;
4938 return SetMenuItemInfo_common( menuitem, &mii, TRUE );
4941 /**********************************************************************
4942 * SetMenuDefaultItem (USER32.@)
4945 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4947 UINT i;
4948 POPUPMENU *menu;
4949 MENUITEM *item;
4951 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4953 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4955 /* reset all default-item flags */
4956 item = menu->items;
4957 for (i = 0; i < menu->nItems; i++, item++)
4959 item->fState &= ~MFS_DEFAULT;
4962 /* no default item */
4963 if ( -1 == uItem)
4965 return TRUE;
4968 item = menu->items;
4969 if ( bypos )
4971 if ( uItem >= menu->nItems ) return FALSE;
4972 item[uItem].fState |= MFS_DEFAULT;
4973 return TRUE;
4975 else
4977 for (i = 0; i < menu->nItems; i++, item++)
4979 if (item->wID == uItem)
4981 item->fState |= MFS_DEFAULT;
4982 return TRUE;
4987 return FALSE;
4990 /**********************************************************************
4991 * GetMenuDefaultItem (USER32.@)
4993 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4995 POPUPMENU *menu;
4996 MENUITEM * item;
4997 UINT i = 0;
4999 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
5001 if (!(menu = MENU_GetMenu(hmenu))) return -1;
5003 /* find default item */
5004 item = menu->items;
5006 /* empty menu */
5007 if (! item) return -1;
5009 while ( !( item->fState & MFS_DEFAULT ) )
5011 i++; item++;
5012 if (i >= menu->nItems ) return -1;
5015 /* default: don't return disabled items */
5016 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
5018 /* search rekursiv when needed */
5019 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
5021 UINT ret;
5022 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
5023 if ( -1 != ret ) return ret;
5025 /* when item not found in submenu, return the popup item */
5027 return ( bypos ) ? i : item->wID;
5032 /**********************************************************************
5033 * InsertMenuItemA (USER32.@)
5035 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
5036 const MENUITEMINFOA *lpmii)
5038 MENUITEM *item;
5039 MENUITEMINFOW mii;
5041 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
5043 if (!MENU_NormalizeMenuItemInfoStruct( (const MENUITEMINFOW *)lpmii, &mii )) return FALSE;
5045 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
5046 return SetMenuItemInfo_common(item, &mii, FALSE);
5050 /**********************************************************************
5051 * InsertMenuItemW (USER32.@)
5053 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
5054 const MENUITEMINFOW *lpmii)
5056 MENUITEM *item;
5057 MENUITEMINFOW mii;
5059 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
5061 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
5063 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
5064 return SetMenuItemInfo_common(item, &mii, TRUE);
5067 /**********************************************************************
5068 * CheckMenuRadioItem (USER32.@)
5071 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
5072 UINT first, UINT last, UINT check,
5073 UINT bypos)
5075 BOOL done = FALSE;
5076 UINT i;
5077 MENUITEM *mi_first = NULL, *mi_check;
5078 HMENU m_first, m_check;
5080 for (i = first; i <= last; i++)
5082 UINT pos = i;
5084 if (!mi_first)
5086 m_first = hMenu;
5087 mi_first = MENU_FindItem(&m_first, &pos, bypos);
5088 if (!mi_first) continue;
5089 mi_check = mi_first;
5090 m_check = m_first;
5092 else
5094 m_check = hMenu;
5095 mi_check = MENU_FindItem(&m_check, &pos, bypos);
5096 if (!mi_check) continue;
5099 if (m_first != m_check) continue;
5100 if (mi_check->fType == MFT_SEPARATOR) continue;
5102 if (i == check)
5104 mi_check->fType |= MFT_RADIOCHECK;
5105 mi_check->fState |= MFS_CHECKED;
5106 done = TRUE;
5108 else
5110 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
5111 mi_check->fState &= ~MFS_CHECKED;
5115 return done;
5119 /**********************************************************************
5120 * GetMenuItemRect (USER32.@)
5122 * ATTENTION: Here, the returned values in rect are the screen
5123 * coordinates of the item just like if the menu was
5124 * always on the upper left side of the application.
5127 BOOL WINAPI GetMenuItemRect(HWND hwnd, HMENU hMenu, UINT uItem, RECT *rect)
5129 POPUPMENU *menu;
5130 MENUITEM *item;
5132 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
5134 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
5135 if ((rect == NULL) || (item == NULL))
5136 return FALSE;
5138 menu = MENU_GetMenu(hMenu);
5139 if (!menu) return FALSE;
5141 if (!hwnd) hwnd = menu->hWnd;
5142 if (!hwnd) return FALSE;
5144 *rect = item->rect;
5146 OffsetRect(rect, menu->items_rect.left, menu->items_rect.top);
5147 MapWindowPoints(hwnd, 0, (POINT *)rect, 2);
5149 return TRUE;
5152 /**********************************************************************
5153 * SetMenuInfo (USER32.@)
5155 * FIXME
5156 * actually use the items to draw the menu
5157 * (recalculate and/or redraw)
5159 static BOOL menu_SetMenuInfo( HMENU hMenu, LPCMENUINFO lpmi)
5161 POPUPMENU *menu;
5162 if( !(menu = MENU_GetMenu(hMenu))) return FALSE;
5164 if (lpmi->fMask & MIM_BACKGROUND)
5165 menu->hbrBack = lpmi->hbrBack;
5167 if (lpmi->fMask & MIM_HELPID)
5168 menu->dwContextHelpID = lpmi->dwContextHelpID;
5170 if (lpmi->fMask & MIM_MAXHEIGHT)
5171 menu->cyMax = lpmi->cyMax;
5173 if (lpmi->fMask & MIM_MENUDATA)
5174 menu->dwMenuData = lpmi->dwMenuData;
5176 if (lpmi->fMask & MIM_STYLE)
5177 menu->dwStyle = lpmi->dwStyle;
5179 if( lpmi->fMask & MIM_APPLYTOSUBMENUS) {
5180 int i;
5181 MENUITEM *item = menu->items;
5182 for( i = menu->nItems; i; i--, item++)
5183 if( item->fType & MF_POPUP)
5184 menu_SetMenuInfo( item->hSubMenu, lpmi);
5186 return TRUE;
5189 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
5191 TRACE("(%p %p)\n", hMenu, lpmi);
5192 if( lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu_SetMenuInfo( hMenu, lpmi))) {
5193 if( lpmi->fMask & MIM_STYLE) {
5194 if (lpmi->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
5195 if (lpmi->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
5196 if (lpmi->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
5198 return TRUE;
5200 SetLastError( ERROR_INVALID_PARAMETER);
5201 return FALSE;
5204 /**********************************************************************
5205 * GetMenuInfo (USER32.@)
5207 * NOTES
5208 * win98/NT5.0
5211 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
5212 { POPUPMENU *menu;
5214 TRACE("(%p %p)\n", hMenu, lpmi);
5216 if (lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
5219 if (lpmi->fMask & MIM_BACKGROUND)
5220 lpmi->hbrBack = menu->hbrBack;
5222 if (lpmi->fMask & MIM_HELPID)
5223 lpmi->dwContextHelpID = menu->dwContextHelpID;
5225 if (lpmi->fMask & MIM_MAXHEIGHT)
5226 lpmi->cyMax = menu->cyMax;
5228 if (lpmi->fMask & MIM_MENUDATA)
5229 lpmi->dwMenuData = menu->dwMenuData;
5231 if (lpmi->fMask & MIM_STYLE)
5232 lpmi->dwStyle = menu->dwStyle;
5234 return TRUE;
5236 SetLastError( ERROR_INVALID_PARAMETER);
5237 return FALSE;
5241 /**********************************************************************
5242 * SetMenuContextHelpId (USER32.@)
5244 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
5246 LPPOPUPMENU menu;
5248 TRACE("(%p 0x%08x)\n", hMenu, dwContextHelpID);
5250 if ((menu = MENU_GetMenu(hMenu)))
5252 menu->dwContextHelpID = dwContextHelpID;
5253 return TRUE;
5255 return FALSE;
5259 /**********************************************************************
5260 * GetMenuContextHelpId (USER32.@)
5262 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
5264 LPPOPUPMENU menu;
5266 TRACE("(%p)\n", hMenu);
5268 if ((menu = MENU_GetMenu(hMenu)))
5270 return menu->dwContextHelpID;
5272 return 0;
5275 /**********************************************************************
5276 * MenuItemFromPoint (USER32.@)
5278 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
5280 POPUPMENU *menu = MENU_GetMenu(hMenu);
5281 UINT pos;
5283 /*FIXME: Do we have to handle hWnd here? */
5284 if (!menu) return -1;
5285 if (MENU_FindItemByCoords( menu, ptScreen, &pos ) != ht_item) return -1;
5286 return pos;
5290 /**********************************************************************
5291 * translate_accelerator
5293 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
5294 BYTE fVirt, WORD key, WORD cmd )
5296 INT mask = 0;
5297 UINT mesg = 0;
5299 if (wParam != key) return FALSE;
5301 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
5302 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
5303 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
5305 if (message == WM_CHAR || message == WM_SYSCHAR)
5307 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
5309 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam) & 0xff);
5310 goto found;
5313 else
5315 if(fVirt & FVIRTKEY)
5317 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
5318 wParam, 0xff & HIWORD(lParam));
5320 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
5321 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
5323 else
5325 if (!(lParam & 0x01000000)) /* no special_key */
5327 if ((fVirt & FALT) && (lParam & 0x20000000))
5328 { /* ^^ ALT pressed */
5329 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(wParam) & 0xff);
5330 goto found;
5335 return FALSE;
5337 found:
5338 if (message == WM_KEYUP || message == WM_SYSKEYUP)
5339 mesg = 1;
5340 else
5342 HMENU hMenu, hSubMenu, hSysMenu;
5343 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
5345 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
5346 hSysMenu = get_win_sys_menu( hWnd );
5348 /* find menu item and ask application to initialize it */
5349 /* 1. in the system menu */
5350 hSubMenu = hSysMenu;
5351 nPos = cmd;
5352 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5354 if (GetCapture())
5355 mesg = 2;
5356 if (!IsWindowEnabled(hWnd))
5357 mesg = 3;
5358 else
5360 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
5361 if(hSubMenu != hSysMenu)
5363 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
5364 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
5365 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
5367 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
5370 else /* 2. in the window's menu */
5372 hSubMenu = hMenu;
5373 nPos = cmd;
5374 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5376 if (GetCapture())
5377 mesg = 2;
5378 if (!IsWindowEnabled(hWnd))
5379 mesg = 3;
5380 else
5382 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
5383 if(hSubMenu != hMenu)
5385 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
5386 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
5387 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
5389 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
5394 if (mesg == 0)
5396 if (uSysStat != (UINT)-1)
5398 if (uSysStat & (MF_DISABLED|MF_GRAYED))
5399 mesg=4;
5400 else
5401 mesg=WM_SYSCOMMAND;
5403 else
5405 if (uStat != (UINT)-1)
5407 if (IsIconic(hWnd))
5408 mesg=5;
5409 else
5411 if (uStat & (MF_DISABLED|MF_GRAYED))
5412 mesg=6;
5413 else
5414 mesg=WM_COMMAND;
5417 else
5418 mesg=WM_COMMAND;
5423 if( mesg==WM_COMMAND )
5425 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
5426 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
5428 else if( mesg==WM_SYSCOMMAND )
5430 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
5431 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
5433 else
5435 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
5436 * #0: unknown (please report!)
5437 * #1: for WM_KEYUP,WM_SYSKEYUP
5438 * #2: mouse is captured
5439 * #3: window is disabled
5440 * #4: it's a disabled system menu option
5441 * #5: it's a menu option, but window is iconic
5442 * #6: it's a menu option, but disabled
5444 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
5445 if(mesg==0)
5446 ERR_(accel)(" unknown reason - please report!\n");
5448 return TRUE;
5451 /**********************************************************************
5452 * TranslateAcceleratorA (USER32.@)
5453 * TranslateAccelerator (USER32.@)
5455 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
5457 switch (msg->message)
5459 case WM_KEYDOWN:
5460 case WM_SYSKEYDOWN:
5461 return TranslateAcceleratorW( hWnd, hAccel, msg );
5463 case WM_CHAR:
5464 case WM_SYSCHAR:
5466 MSG msgW = *msg;
5467 char ch = LOWORD(msg->wParam);
5468 WCHAR wch;
5469 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
5470 msgW.wParam = MAKEWPARAM(wch, HIWORD(msg->wParam));
5471 return TranslateAcceleratorW( hWnd, hAccel, &msgW );
5474 default:
5475 return 0;
5479 /**********************************************************************
5480 * TranslateAcceleratorW (USER32.@)
5482 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
5484 ACCEL data[32], *ptr = data;
5485 int i, count;
5487 if (!hWnd) return 0;
5489 if (msg->message != WM_KEYDOWN &&
5490 msg->message != WM_SYSKEYDOWN &&
5491 msg->message != WM_CHAR &&
5492 msg->message != WM_SYSCHAR)
5493 return 0;
5495 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5496 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5498 if (!(count = CopyAcceleratorTableW( hAccel, NULL, 0 ))) return 0;
5499 if (count > sizeof(data)/sizeof(data[0]))
5501 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*ptr) ))) return 0;
5503 count = CopyAcceleratorTableW( hAccel, ptr, count );
5504 for (i = 0; i < count; i++)
5506 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5507 ptr[i].fVirt, ptr[i].key, ptr[i].cmd))
5508 break;
5510 if (ptr != data) HeapFree( GetProcessHeap(), 0, ptr );
5511 return (i < count);