mstask: Improve stubs for ITask::GetTaskFlags and ITask::GetFlags.
[wine.git] / dlls / user32 / menu.c
blobf4194ed6f2a24f920fce6a4892d13e23e8a1ae30
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 bScrolling; /* Scroll arrows are active */
98 UINT nScrollPos; /* Current scroll position */
99 UINT nTotalHeight; /* Total height of menu items inside menu */
100 RECT items_rect; /* Rectangle within which the items lie. Excludes margins and scroll arrows */
101 /* ------------ MENUINFO members ------ */
102 DWORD dwStyle; /* Extended menu style */
103 UINT cyMax; /* max height of the whole menu, 0 is screen height */
104 HBRUSH hbrBack; /* brush for menu background */
105 DWORD dwContextHelpID;
106 DWORD dwMenuData; /* application defined value */
107 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
108 WORD textOffset; /* Offset of text when items have both bitmaps and text */
109 } POPUPMENU, *LPPOPUPMENU;
111 /* internal flags for menu tracking */
113 #define TF_ENDMENU 0x10000
114 #define TF_SUSPENDPOPUP 0x20000
115 #define TF_SKIPREMOVE 0x40000
116 #define TF_RCVD_BTN_UP 0x80000
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, UINT message, 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_item;
2588 if( IS_SYSTEM_MENU(ptmenu) )
2590 if (message == WM_LBUTTONDBLCLK) return FALSE;
2591 pos = 0;
2593 else
2594 ht = MENU_FindItemByCoords( ptmenu, pmt->pt, &pos );
2596 if (pos != NO_SELECTED_ITEM)
2598 if (ptmenu->FocusedItem != pos)
2599 MENU_SwitchTracking( pmt, hPtMenu, pos, wFlags );
2601 /* If the popup menu is not already "popped" */
2602 if (!(ptmenu->items[pos].fState & MF_MOUSESELECT))
2603 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2606 /* A click on an item or anywhere on a popup keeps tracking going */
2607 if (ht == ht_item || ((ptmenu->wFlags & MF_POPUP) && ht != ht_nowhere))
2608 return TRUE;
2610 return FALSE;
2613 /***********************************************************************
2614 * MENU_ButtonUp
2616 * Return the value of MENU_ExecFocusedItem if
2617 * the selected item was not a popup. Else open the popup.
2618 * A -1 return value indicates that we go on with menu tracking.
2621 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2623 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2625 if (hPtMenu)
2627 UINT pos;
2628 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2630 if( IS_SYSTEM_MENU(ptmenu) )
2631 pos = 0;
2632 else if (MENU_FindItemByCoords( ptmenu, pmt->pt, &pos ) != ht_item)
2633 pos = NO_SELECTED_ITEM;
2635 if (pos != NO_SELECTED_ITEM && (ptmenu->FocusedItem == pos))
2637 debug_print_menuitem ("FocusedItem: ", &ptmenu->items[pos], "");
2639 if (!(ptmenu->items[pos].fType & MF_POPUP))
2641 INT executedMenuId = MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2642 if (executedMenuId == -1 || executedMenuId == -2) return -1;
2643 return executedMenuId;
2646 /* If we are dealing with the menu bar */
2647 /* and this is a click on an already "popped" item: */
2648 /* Stop the menu tracking and close the opened submenus */
2649 if(((pmt->hTopMenu == hPtMenu) || IS_SYSTEM_MENU(ptmenu)) && (pmt->trackFlags & TF_RCVD_BTN_UP))
2650 return 0;
2652 if( GetMenu(ptmenu->hWnd) == hPtMenu || IS_SYSTEM_MENU(ptmenu) )
2654 if (pos == NO_SELECTED_ITEM) return 0;
2655 pmt->trackFlags |= TF_RCVD_BTN_UP;
2658 return -1;
2662 /***********************************************************************
2663 * MENU_MouseMove
2665 * Return TRUE if we can go on with menu tracking.
2667 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2669 UINT id = NO_SELECTED_ITEM;
2670 POPUPMENU *ptmenu = NULL;
2672 if( hPtMenu )
2674 ptmenu = MENU_GetMenu( hPtMenu );
2675 if( IS_SYSTEM_MENU(ptmenu) )
2676 id = 0;
2677 else if (MENU_FindItemByCoords( ptmenu, pmt->pt, &id ) != ht_item)
2678 id = NO_SELECTED_ITEM;
2681 if( id == NO_SELECTED_ITEM )
2683 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2684 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2687 else if( ptmenu->FocusedItem != id )
2689 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2690 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2692 return TRUE;
2696 /***********************************************************************
2697 * MENU_DoNextMenu
2699 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2701 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk, UINT wFlags )
2703 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2704 BOOL atEnd = FALSE;
2706 /* When skipping left, we need to do something special after the
2707 first menu. */
2708 if (vk == VK_LEFT && menu->FocusedItem == 0)
2710 atEnd = TRUE;
2712 /* When skipping right, for the non-system menu, we need to
2713 handle the last non-special menu item (ie skip any window
2714 icons such as MDI maximize, restore or close) */
2715 else if ((vk == VK_RIGHT) && !IS_SYSTEM_MENU(menu))
2717 UINT i = menu->FocusedItem + 1;
2718 while (i < menu->nItems) {
2719 if ((menu->items[i].wID >= SC_SIZE &&
2720 menu->items[i].wID <= SC_RESTORE)) {
2721 i++;
2722 } else break;
2724 if (i == menu->nItems) {
2725 atEnd = TRUE;
2728 /* When skipping right, we need to cater for the system menu */
2729 else if ((vk == VK_RIGHT) && IS_SYSTEM_MENU(menu))
2731 if (menu->FocusedItem == (menu->nItems - 1)) {
2732 atEnd = TRUE;
2736 if( atEnd )
2738 MDINEXTMENU next_menu;
2739 HMENU hNewMenu;
2740 HWND hNewWnd;
2741 UINT id = 0;
2743 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2744 next_menu.hmenuNext = 0;
2745 next_menu.hwndNext = 0;
2746 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2748 TRACE("%p [%p] -> %p [%p]\n",
2749 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2751 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2753 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2754 hNewWnd = pmt->hOwnerWnd;
2755 if( IS_SYSTEM_MENU(menu) )
2757 /* switch to the menu bar */
2759 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2761 if( vk == VK_LEFT )
2763 menu = MENU_GetMenu( hNewMenu );
2764 id = menu->nItems - 1;
2766 /* Skip backwards over any system predefined icons,
2767 eg. MDI close, restore etc icons */
2768 while ((id > 0) &&
2769 (menu->items[id].wID >= SC_SIZE &&
2770 menu->items[id].wID <= SC_RESTORE)) id--;
2773 else if (style & WS_SYSMENU )
2775 /* switch to the system menu */
2776 hNewMenu = get_win_sys_menu( hNewWnd );
2778 else return FALSE;
2780 else /* application returned a new menu to switch to */
2782 hNewMenu = next_menu.hmenuNext;
2783 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2785 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2787 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2789 if (style & WS_SYSMENU &&
2790 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2792 /* get the real system menu */
2793 hNewMenu = get_win_sys_menu(hNewWnd);
2795 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2797 /* FIXME: Not sure what to do here;
2798 * perhaps try to track hNewMenu as a popup? */
2800 TRACE(" -- got confused.\n");
2801 return FALSE;
2804 else return FALSE;
2807 if( hNewMenu != pmt->hTopMenu )
2809 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2810 FALSE, 0 );
2811 if( pmt->hCurrentMenu != pmt->hTopMenu )
2812 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2815 if( hNewWnd != pmt->hOwnerWnd )
2817 pmt->hOwnerWnd = hNewWnd;
2818 set_capture_window( pmt->hOwnerWnd, GUI_INMENUMODE, NULL );
2821 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2822 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2824 return TRUE;
2826 return FALSE;
2829 /***********************************************************************
2830 * MENU_SuspendPopup
2832 * The idea is not to show the popup if the next input message is
2833 * going to hide it anyway.
2835 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT uMsg )
2837 MSG msg;
2839 msg.hwnd = pmt->hOwnerWnd;
2841 PeekMessageW( &msg, 0, uMsg, uMsg, PM_NOYIELD | PM_REMOVE);
2842 pmt->trackFlags |= TF_SKIPREMOVE;
2844 switch( uMsg )
2846 case WM_KEYDOWN:
2847 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2848 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2850 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2851 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2852 if( msg.message == WM_KEYDOWN &&
2853 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2855 pmt->trackFlags |= TF_SUSPENDPOPUP;
2856 return TRUE;
2859 break;
2862 /* failures go through this */
2863 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2864 return FALSE;
2867 /***********************************************************************
2868 * MENU_KeyEscape
2870 * Handle a VK_ESCAPE key event in a menu.
2872 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2874 BOOL bEndMenu = TRUE;
2876 if (pmt->hCurrentMenu != pmt->hTopMenu)
2878 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2880 if (menu->wFlags & MF_POPUP)
2882 HMENU hmenutmp, hmenuprev;
2884 hmenuprev = hmenutmp = pmt->hTopMenu;
2886 /* close topmost popup */
2887 while (hmenutmp != pmt->hCurrentMenu)
2889 hmenuprev = hmenutmp;
2890 hmenutmp = MENU_GetSubPopup( hmenuprev );
2893 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2894 pmt->hCurrentMenu = hmenuprev;
2895 bEndMenu = FALSE;
2899 return bEndMenu;
2902 /***********************************************************************
2903 * MENU_KeyLeft
2905 * Handle a VK_LEFT key event in a menu.
2907 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags, UINT msg )
2909 POPUPMENU *menu;
2910 HMENU hmenutmp, hmenuprev;
2911 UINT prevcol;
2913 hmenuprev = hmenutmp = pmt->hTopMenu;
2914 menu = MENU_GetMenu( hmenutmp );
2916 /* Try to move 1 column left (if possible) */
2917 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2918 NO_SELECTED_ITEM ) {
2920 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2921 prevcol, TRUE, 0 );
2922 return;
2925 /* close topmost popup */
2926 while (hmenutmp != pmt->hCurrentMenu)
2928 hmenuprev = hmenutmp;
2929 hmenutmp = MENU_GetSubPopup( hmenuprev );
2932 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2933 pmt->hCurrentMenu = hmenuprev;
2935 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2937 /* move menu bar selection if no more popups are left */
2939 if( !MENU_DoNextMenu( pmt, VK_LEFT, wFlags ) )
2940 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2942 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2944 /* A sublevel menu was displayed - display the next one
2945 * unless there is another displacement coming up */
2947 if( !MENU_SuspendPopup( pmt, msg ) )
2948 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2949 pmt->hTopMenu, TRUE, wFlags);
2955 /***********************************************************************
2956 * MENU_KeyRight
2958 * Handle a VK_RIGHT key event in a menu.
2960 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags, UINT msg )
2962 HMENU hmenutmp;
2963 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2964 UINT nextcol;
2966 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2967 pmt->hCurrentMenu,
2968 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2969 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2971 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2973 /* If already displaying a popup, try to display sub-popup */
2975 hmenutmp = pmt->hCurrentMenu;
2976 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2978 /* if subpopup was displayed then we are done */
2979 if (hmenutmp != pmt->hCurrentMenu) return;
2982 /* Check to see if there's another column */
2983 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2984 NO_SELECTED_ITEM ) {
2985 TRACE("Going to %d.\n", nextcol );
2986 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2987 nextcol, TRUE, 0 );
2988 return;
2991 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2993 if( pmt->hCurrentMenu != pmt->hTopMenu )
2995 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2996 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2997 } else hmenutmp = 0;
2999 /* try to move to the next item */
3000 if( !MENU_DoNextMenu( pmt, VK_RIGHT, wFlags ) )
3001 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
3003 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
3004 if( !MENU_SuspendPopup( pmt, msg ) )
3005 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
3006 pmt->hTopMenu, TRUE, wFlags);
3010 static void CALLBACK release_capture( BOOL __normal )
3012 set_capture_window( 0, GUI_INMENUMODE, NULL );
3015 /***********************************************************************
3016 * MENU_TrackMenu
3018 * Menu tracking code.
3020 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
3021 HWND hwnd, const RECT *lprect )
3023 MSG msg;
3024 POPUPMENU *menu;
3025 BOOL fRemove;
3026 INT executedMenuId = -1;
3027 MTRACKER mt;
3028 BOOL enterIdleSent = FALSE;
3029 HWND capture_win;
3031 mt.trackFlags = 0;
3032 mt.hCurrentMenu = hmenu;
3033 mt.hTopMenu = hmenu;
3034 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
3035 mt.pt.x = x;
3036 mt.pt.y = y;
3038 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p %s\n",
3039 hmenu, wFlags, x, y, hwnd, wine_dbgstr_rect( lprect));
3041 if (!(menu = MENU_GetMenu( hmenu )))
3043 WARN("Invalid menu handle %p\n", hmenu);
3044 SetLastError(ERROR_INVALID_MENU_HANDLE);
3045 return FALSE;
3048 if (wFlags & TPM_BUTTONDOWN)
3050 /* Get the result in order to start the tracking or not */
3051 fRemove = MENU_ButtonDown( &mt, WM_LBUTTONDOWN, hmenu, wFlags );
3052 fEndMenu = !fRemove;
3055 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
3057 /* owner may not be visible when tracking a popup, so use the menu itself */
3058 capture_win = (wFlags & TPM_POPUPMENU) ? menu->hWnd : mt.hOwnerWnd;
3059 set_capture_window( capture_win, GUI_INMENUMODE, NULL );
3061 if ((wFlags & TPM_POPUPMENU) && menu->nItems == 0)
3062 return FALSE;
3064 __TRY while (!fEndMenu)
3066 menu = MENU_GetMenu( mt.hCurrentMenu );
3067 if (!menu) /* sometimes happens if I do a window manager close */
3068 break;
3070 /* we have to keep the message in the queue until it's
3071 * clear that menu loop is not over yet. */
3073 for (;;)
3075 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
3077 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
3078 /* remove the message from the queue */
3079 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3081 else
3083 if (!enterIdleSent)
3085 HWND win = menu->wFlags & MF_POPUP ? menu->hWnd : 0;
3086 enterIdleSent = TRUE;
3087 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
3089 WaitMessage();
3093 /* check if EndMenu() tried to cancel us, by posting this message */
3094 if(msg.message == WM_CANCELMODE)
3096 /* we are now out of the loop */
3097 fEndMenu = TRUE;
3099 /* remove the message from the queue */
3100 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3102 /* break out of internal loop, ala ESCAPE */
3103 break;
3106 mt.pt = msg.pt;
3108 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
3109 enterIdleSent=FALSE;
3111 fRemove = FALSE;
3112 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
3115 * Use the mouse coordinates in lParam instead of those in the MSG
3116 * struct to properly handle synthetic messages. They are already
3117 * in screen coordinates.
3119 mt.pt.x = (short)LOWORD(msg.lParam);
3120 mt.pt.y = (short)HIWORD(msg.lParam);
3122 /* Find a menu for this mouse event */
3123 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
3125 switch(msg.message)
3127 /* no WM_NC... messages in captured state */
3129 case WM_RBUTTONDBLCLK:
3130 case WM_RBUTTONDOWN:
3131 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3132 /* fall through */
3133 case WM_LBUTTONDBLCLK:
3134 case WM_LBUTTONDOWN:
3135 /* If the message belongs to the menu, removes it from the queue */
3136 /* Else, end menu tracking */
3137 fRemove = MENU_ButtonDown( &mt, msg.message, hmenu, wFlags );
3138 fEndMenu = !fRemove;
3139 break;
3141 case WM_RBUTTONUP:
3142 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3143 /* fall through */
3144 case WM_LBUTTONUP:
3145 /* Check if a menu was selected by the mouse */
3146 if (hmenu)
3148 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
3149 TRACE("executedMenuId %d\n", executedMenuId);
3151 /* End the loop if executedMenuId is an item ID */
3152 /* or if the job was done (executedMenuId = 0). */
3153 fEndMenu = fRemove = (executedMenuId != -1);
3155 /* No menu was selected by the mouse */
3156 /* if the function was called by TrackPopupMenu, continue
3157 with the menu tracking. If not, stop it */
3158 else
3159 fEndMenu = !(wFlags & TPM_POPUPMENU);
3161 break;
3163 case WM_MOUSEMOVE:
3164 /* the selected menu item must be changed every time */
3165 /* the mouse moves. */
3167 if (hmenu)
3168 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
3170 } /* switch(msg.message) - mouse */
3172 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
3174 fRemove = TRUE; /* Keyboard messages are always removed */
3175 switch(msg.message)
3177 case WM_KEYDOWN:
3178 case WM_SYSKEYDOWN:
3179 switch(msg.wParam)
3181 case VK_MENU:
3182 case VK_F10:
3183 fEndMenu = TRUE;
3184 break;
3186 case VK_HOME:
3187 case VK_END:
3188 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
3189 NO_SELECTED_ITEM, FALSE, 0 );
3190 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3191 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
3192 break;
3194 case VK_UP:
3195 case VK_DOWN: /* If on menu bar, pull-down the menu */
3197 menu = MENU_GetMenu( mt.hCurrentMenu );
3198 if (!(menu->wFlags & MF_POPUP))
3199 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
3200 else /* otherwise try to move selection */
3201 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3202 (msg.wParam == VK_UP)? ITEM_PREV : ITEM_NEXT );
3203 break;
3205 case VK_LEFT:
3206 MENU_KeyLeft( &mt, wFlags, msg.message );
3207 break;
3209 case VK_RIGHT:
3210 MENU_KeyRight( &mt, wFlags, msg.message );
3211 break;
3213 case VK_ESCAPE:
3214 fEndMenu = MENU_KeyEscape(&mt, wFlags);
3215 break;
3217 case VK_F1:
3219 HELPINFO hi;
3220 hi.cbSize = sizeof(HELPINFO);
3221 hi.iContextType = HELPINFO_MENUITEM;
3222 if (menu->FocusedItem == NO_SELECTED_ITEM)
3223 hi.iCtrlId = 0;
3224 else
3225 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
3226 hi.hItemHandle = hmenu;
3227 hi.dwContextId = menu->dwContextHelpID;
3228 hi.MousePos = msg.pt;
3229 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
3230 break;
3233 default:
3234 TranslateMessage( &msg );
3235 break;
3237 break; /* WM_KEYDOWN */
3239 case WM_CHAR:
3240 case WM_SYSCHAR:
3242 UINT pos;
3244 if (msg.wParam == '\r' || msg.wParam == ' ')
3246 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3247 fEndMenu = (executedMenuId != -2);
3249 break;
3252 /* Hack to avoid control chars. */
3253 /* We will find a better way real soon... */
3254 if (msg.wParam < 32) break;
3256 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3257 LOWORD(msg.wParam), FALSE );
3258 if (pos == (UINT)-2) fEndMenu = TRUE;
3259 else if (pos == (UINT)-1) MessageBeep(0);
3260 else
3262 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3263 TRUE, 0 );
3264 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3265 fEndMenu = (executedMenuId != -2);
3268 break;
3269 } /* switch(msg.message) - kbd */
3271 else
3273 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3274 DispatchMessageW( &msg );
3275 continue;
3278 if (!fEndMenu) fRemove = TRUE;
3280 /* finally remove message from the queue */
3282 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3283 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3284 else mt.trackFlags &= ~TF_SKIPREMOVE;
3286 __FINALLY( release_capture )
3288 /* If dropdown is still painted and the close box is clicked on
3289 then the menu will be destroyed as part of the DispatchMessage above.
3290 This will then invalidate the menu handle in mt.hTopMenu. We should
3291 check for this first. */
3292 if( IsMenu( mt.hTopMenu ) )
3294 menu = MENU_GetMenu( mt.hTopMenu );
3296 if( IsWindow( mt.hOwnerWnd ) )
3298 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE, wFlags );
3300 if (menu && (menu->wFlags & MF_POPUP))
3302 DestroyWindow( menu->hWnd );
3303 menu->hWnd = 0;
3305 if (!(wFlags & TPM_NONOTIFY))
3306 SendMessageW( mt.hOwnerWnd, WM_UNINITMENUPOPUP, (WPARAM)mt.hTopMenu,
3307 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3309 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3310 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKEWPARAM(0,0xffff), 0 );
3314 SetLastError( ERROR_SUCCESS );
3315 /* The return value is only used by TrackPopupMenu */
3316 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
3317 if (executedMenuId == -1) executedMenuId = 0;
3318 return executedMenuId;
3321 /***********************************************************************
3322 * MENU_InitTracking
3324 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3326 POPUPMENU *menu;
3328 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
3330 HideCaret(0);
3332 if (!(menu = MENU_GetMenu( hMenu ))) return FALSE;
3334 /* This makes the menus of applications built with Delphi work.
3335 * It also enables menus to be displayed in more than one window,
3336 * but there are some bugs left that need to be fixed in this case.
3338 if (!bPopup) menu->hWnd = hWnd;
3339 if (!top_popup)
3341 top_popup = menu->hWnd;
3342 top_popup_hmenu = hMenu;
3345 fEndMenu = FALSE;
3347 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3348 if (!(wFlags & TPM_NONOTIFY))
3349 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3351 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
3353 if (!(wFlags & TPM_NONOTIFY))
3355 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
3356 /* If an app changed/recreated menu bar entries in WM_INITMENU
3357 * menu sizes will be recalculated once the menu created/shown.
3361 return TRUE;
3364 /***********************************************************************
3365 * MENU_ExitTracking
3367 static BOOL MENU_ExitTracking(HWND hWnd, BOOL bPopup)
3369 TRACE("hwnd=%p\n", hWnd);
3371 SendMessageW( hWnd, WM_EXITMENULOOP, bPopup, 0 );
3372 ShowCaret(0);
3373 top_popup = 0;
3374 top_popup_hmenu = NULL;
3375 return TRUE;
3378 /***********************************************************************
3379 * MENU_TrackMouseMenuBar
3381 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3383 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3385 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3386 UINT wFlags = TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3388 TRACE("wnd=%p ht=0x%04x %s\n", hWnd, ht, wine_dbgstr_point( &pt));
3390 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) wFlags |= TPM_LAYOUTRTL;
3391 if (IsMenu(hMenu))
3393 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3395 /* fetch the window menu again, it may have changed */
3396 hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3397 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3398 MENU_ExitTracking(hWnd, FALSE);
3403 /***********************************************************************
3404 * MENU_TrackKbdMenuBar
3406 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3408 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3410 UINT uItem = NO_SELECTED_ITEM;
3411 HMENU hTrackMenu;
3412 UINT wFlags = TPM_LEFTALIGN | TPM_LEFTBUTTON;
3414 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3416 /* find window that has a menu */
3418 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3419 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3421 /* check if we have to track a system menu */
3423 hTrackMenu = GetMenu( hwnd );
3424 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3426 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3427 hTrackMenu = get_win_sys_menu( hwnd );
3428 uItem = 0;
3429 wParam |= HTSYSMENU; /* prevent item lookup */
3431 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) wFlags |= TPM_LAYOUTRTL;
3433 if (!IsMenu( hTrackMenu )) return;
3435 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3437 /* fetch the window menu again, it may have changed */
3438 hTrackMenu = (wParam & HTSYSMENU) ? get_win_sys_menu( hwnd ) : GetMenu( hwnd );
3440 if( wChar && wChar != ' ' )
3442 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3443 if ( uItem >= (UINT)(-2) )
3445 if( uItem == (UINT)(-1) ) MessageBeep(0);
3446 /* schedule end of menu tracking */
3447 wFlags |= TF_ENDMENU;
3448 goto track_menu;
3452 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3454 if (!(wParam & HTSYSMENU) || wChar == ' ')
3456 if( uItem == NO_SELECTED_ITEM )
3457 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3458 else
3459 PostMessageW( hwnd, WM_KEYDOWN, VK_RETURN, 0 );
3462 track_menu:
3463 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3464 MENU_ExitTracking( hwnd, FALSE );
3467 /**********************************************************************
3468 * TrackPopupMenuEx (USER32.@)
3470 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3471 HWND hWnd, LPTPMPARAMS lpTpm )
3473 POPUPMENU *menu;
3474 BOOL ret = FALSE;
3476 TRACE("hmenu %p flags %04x (%d,%d) hwnd %p lpTpm %p rect %s\n",
3477 hMenu, wFlags, x, y, hWnd, lpTpm,
3478 lpTpm ? wine_dbgstr_rect( &lpTpm->rcExclude) : "-" );
3480 /* Parameter check */
3481 /* FIXME: this check is performed several times, here and in the called
3482 functions. That could be optimized */
3483 if (!(menu = MENU_GetMenu( hMenu )))
3485 SetLastError( ERROR_INVALID_MENU_HANDLE );
3486 return FALSE;
3489 if (IsWindow(menu->hWnd))
3491 SetLastError( ERROR_POPUP_ALREADY_ACTIVE );
3492 return FALSE;
3495 if (MENU_InitPopup( hWnd, hMenu, wFlags ))
3497 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3499 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3500 if (!(wFlags & TPM_NONOTIFY))
3501 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3503 if (menu->wFlags & MF_SYSMENU)
3504 MENU_InitSysMenuPopup( hMenu, GetWindowLongW( hWnd, GWL_STYLE ),
3505 GetClassLongW( hWnd, GCL_STYLE));
3507 if (MENU_ShowPopup( hWnd, hMenu, 0, wFlags, x, y, 0, 0 ))
3508 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,
3509 lpTpm ? &lpTpm->rcExclude : NULL );
3510 MENU_ExitTracking(hWnd, TRUE);
3512 if (menu->hWnd)
3514 DestroyWindow( menu->hWnd );
3515 menu->hWnd = 0;
3517 if (!(wFlags & TPM_NONOTIFY))
3518 SendMessageW( hWnd, WM_UNINITMENUPOPUP, (WPARAM)hMenu,
3519 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3521 SetLastError(0);
3524 return ret;
3527 /**********************************************************************
3528 * TrackPopupMenu (USER32.@)
3530 * Like the win32 API, the function return the command ID only if the
3531 * flag TPM_RETURNCMD is on.
3534 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3535 INT nReserved, HWND hWnd, const RECT *lpRect )
3537 return TrackPopupMenuEx( hMenu, wFlags, x, y, hWnd, NULL);
3540 /***********************************************************************
3541 * PopupMenuWndProc
3543 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3545 LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3547 TRACE("hwnd=%p msg=0x%04x wp=0x%04lx lp=0x%08lx\n", hwnd, message, wParam, lParam);
3549 switch(message)
3551 case WM_CREATE:
3553 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3554 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)cs->lpCreateParams );
3555 return 0;
3558 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3559 return MA_NOACTIVATE;
3561 case WM_PAINT:
3563 PAINTSTRUCT ps;
3564 BeginPaint( hwnd, &ps );
3565 MENU_DrawPopupMenu( hwnd, ps.hdc,
3566 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3567 EndPaint( hwnd, &ps );
3568 return 0;
3571 case WM_PRINTCLIENT:
3573 MENU_DrawPopupMenu( hwnd, (HDC)wParam,
3574 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3575 return 0;
3578 case WM_ERASEBKGND:
3579 return 1;
3581 case WM_DESTROY:
3582 /* zero out global pointer in case resident popup window was destroyed. */
3583 if (hwnd == top_popup) {
3584 top_popup = 0;
3585 top_popup_hmenu = NULL;
3587 break;
3589 case WM_SHOWWINDOW:
3591 if( wParam )
3593 if (!GetWindowLongPtrW( hwnd, 0 )) ERR("no menu to display\n");
3595 else
3596 SetWindowLongPtrW( hwnd, 0, 0 );
3597 break;
3599 case MN_GETHMENU:
3600 return GetWindowLongPtrW( hwnd, 0 );
3602 default:
3603 return DefWindowProcW( hwnd, message, wParam, lParam );
3605 return 0;
3609 /***********************************************************************
3610 * MENU_GetMenuBarHeight
3612 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3614 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3615 INT orgX, INT orgY )
3617 HDC hdc;
3618 RECT rectBar;
3619 LPPOPUPMENU lppop;
3621 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3623 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3625 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3626 SelectObject( hdc, get_menu_font(FALSE));
3627 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3628 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3629 ReleaseDC( hwnd, hdc );
3630 return lppop->Height;
3634 /*******************************************************************
3635 * ChangeMenuA (USER32.@)
3637 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3638 UINT id, UINT flags )
3640 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3641 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3642 id, data );
3643 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3644 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3645 id, data );
3646 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3647 flags & MF_BYPOSITION ? pos : id,
3648 flags & ~MF_REMOVE );
3649 /* Default: MF_INSERT */
3650 return InsertMenuA( hMenu, pos, flags, id, data );
3654 /*******************************************************************
3655 * ChangeMenuW (USER32.@)
3657 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3658 UINT id, UINT flags )
3660 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3661 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3662 id, data );
3663 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3664 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3665 id, data );
3666 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3667 flags & MF_BYPOSITION ? pos : id,
3668 flags & ~MF_REMOVE );
3669 /* Default: MF_INSERT */
3670 return InsertMenuW( hMenu, pos, flags, id, data );
3674 /*******************************************************************
3675 * CheckMenuItem (USER32.@)
3677 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3679 MENUITEM *item;
3680 DWORD ret;
3682 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3683 ret = item->fState & MF_CHECKED;
3684 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3685 else item->fState &= ~MF_CHECKED;
3686 return ret;
3690 /**********************************************************************
3691 * EnableMenuItem (USER32.@)
3693 BOOL WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3695 UINT oldflags;
3696 MENUITEM *item;
3697 POPUPMENU *menu;
3699 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3701 /* Get the Popupmenu to access the owner menu */
3702 if (!(menu = MENU_GetMenu(hMenu)))
3703 return (UINT)-1;
3705 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3706 return (UINT)-1;
3708 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3709 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3711 /* If the close item in the system menu change update the close button */
3712 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3714 if (menu->hSysMenuOwner != 0)
3716 RECT rc;
3717 POPUPMENU* parentMenu;
3719 /* Get the parent menu to access*/
3720 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3721 return (UINT)-1;
3723 /* Refresh the frame to reflect the change */
3724 WIN_GetRectangles( parentMenu->hWnd, COORDS_CLIENT, &rc, NULL );
3725 rc.bottom = 0;
3726 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3730 return oldflags;
3734 /*******************************************************************
3735 * GetMenuStringA (USER32.@)
3737 INT WINAPI GetMenuStringA(
3738 HMENU hMenu, /* [in] menuhandle */
3739 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3740 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3741 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3742 UINT wFlags /* [in] MF_ flags */
3744 MENUITEM *item;
3746 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3747 if (str && nMaxSiz) str[0] = '\0';
3748 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3749 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3750 return 0;
3752 if (!item->text) return 0;
3753 if (!str || !nMaxSiz) return WideCharToMultiByte( CP_ACP, 0, item->text, -1, NULL, 0, NULL, NULL );
3754 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3755 str[nMaxSiz-1] = 0;
3756 TRACE("returning %s\n", debugstr_a(str));
3757 return strlen(str);
3761 /*******************************************************************
3762 * GetMenuStringW (USER32.@)
3764 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3765 LPWSTR str, INT nMaxSiz, UINT wFlags )
3767 MENUITEM *item;
3769 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3770 if (str && nMaxSiz) str[0] = '\0';
3771 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3772 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3773 return 0;
3775 if (!str || !nMaxSiz) return item->text ? strlenW(item->text) : 0;
3776 if( !(item->text)) {
3777 str[0] = 0;
3778 return 0;
3780 lstrcpynW( str, item->text, nMaxSiz );
3781 TRACE("returning %s\n", debugstr_w(str));
3782 return strlenW(str);
3786 /**********************************************************************
3787 * HiliteMenuItem (USER32.@)
3789 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3790 UINT wHilite )
3792 LPPOPUPMENU menu;
3793 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3794 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3795 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3796 if (menu->FocusedItem == wItemID) return TRUE;
3797 MENU_HideSubPopups( hWnd, hMenu, FALSE, 0 );
3798 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3799 return TRUE;
3803 /**********************************************************************
3804 * GetMenuState (USER32.@)
3806 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3808 MENUITEM *item;
3809 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3810 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3811 debug_print_menuitem (" item: ", item, "");
3812 if (item->fType & MF_POPUP)
3814 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3815 if (!menu) return -1;
3816 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3818 else
3820 /* We used to (from way back then) mask the result to 0xff. */
3821 /* I don't know why and it seems wrong as the documented */
3822 /* return flag MF_SEPARATOR is outside that mask. */
3823 return (item->fType | item->fState);
3828 /**********************************************************************
3829 * GetMenuItemCount (USER32.@)
3831 INT WINAPI GetMenuItemCount( HMENU hMenu )
3833 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3834 if (!menu) return -1;
3835 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3836 return menu->nItems;
3840 /**********************************************************************
3841 * GetMenuItemID (USER32.@)
3843 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3845 MENUITEM * lpmi;
3847 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
3848 if (lpmi->fType & MF_POPUP) return -1;
3849 return lpmi->wID;
3854 /**********************************************************************
3855 * MENU_mnu2mnuii
3857 * Uses flags, id and text ptr, passed by InsertMenu() and
3858 * ModifyMenu() to setup a MenuItemInfo structure.
3860 static void MENU_mnu2mnuii( UINT flags, UINT_PTR id, LPCWSTR str,
3861 LPMENUITEMINFOW pmii)
3863 ZeroMemory( pmii, sizeof( MENUITEMINFOW));
3864 pmii->cbSize = sizeof( MENUITEMINFOW);
3865 pmii->fMask = MIIM_STATE | MIIM_ID | MIIM_FTYPE;
3866 /* setting bitmap clears text and vice versa */
3867 if( IS_STRING_ITEM(flags)) {
3868 pmii->fMask |= MIIM_STRING | MIIM_BITMAP;
3869 if( !str)
3870 flags |= MF_SEPARATOR;
3871 /* Item beginning with a backspace is a help item */
3872 /* FIXME: wrong place, this is only true in win16 */
3873 else if( *str == '\b') {
3874 flags |= MF_HELP;
3875 str++;
3877 pmii->dwTypeData = (LPWSTR)str;
3878 } else if( flags & MFT_BITMAP){
3879 pmii->fMask |= MIIM_BITMAP | MIIM_STRING;
3880 pmii->hbmpItem = (HBITMAP)str;
3882 if( flags & MF_OWNERDRAW){
3883 pmii->fMask |= MIIM_DATA;
3884 pmii->dwItemData = (ULONG_PTR) str;
3886 if( flags & MF_POPUP && MENU_GetMenu((HMENU)id)) {
3887 pmii->fMask |= MIIM_SUBMENU;
3888 pmii->hSubMenu = (HMENU)id;
3890 if( flags & MF_SEPARATOR) flags |= MF_GRAYED | MF_DISABLED;
3891 pmii->fState = flags & MENUITEMINFO_STATE_MASK & ~MFS_DEFAULT;
3892 pmii->fType = flags & MENUITEMINFO_TYPE_MASK;
3893 pmii->wID = (UINT)id;
3897 /*******************************************************************
3898 * InsertMenuW (USER32.@)
3900 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3901 UINT_PTR id, LPCWSTR str )
3903 MENUITEM *item;
3904 MENUITEMINFOW mii;
3906 if (IS_STRING_ITEM(flags) && str)
3907 TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %s\n",
3908 hMenu, pos, flags, id, debugstr_w(str) );
3909 else TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %p (not a string)\n",
3910 hMenu, pos, flags, id, str );
3912 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3913 MENU_mnu2mnuii( flags, id, str, &mii);
3914 if (!(SetMenuItemInfo_common( item, &mii, TRUE)))
3916 RemoveMenu( hMenu, pos, flags );
3917 return FALSE;
3920 item->hCheckBit = item->hUnCheckBit = 0;
3921 return TRUE;
3925 /*******************************************************************
3926 * InsertMenuA (USER32.@)
3928 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3929 UINT_PTR id, LPCSTR str )
3931 BOOL ret = FALSE;
3933 if (IS_STRING_ITEM(flags) && str)
3935 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3936 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3937 if (newstr)
3939 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3940 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3941 HeapFree( GetProcessHeap(), 0, newstr );
3943 return ret;
3945 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3949 /*******************************************************************
3950 * AppendMenuA (USER32.@)
3952 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3953 UINT_PTR id, LPCSTR data )
3955 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3959 /*******************************************************************
3960 * AppendMenuW (USER32.@)
3962 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3963 UINT_PTR id, LPCWSTR data )
3965 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3969 /**********************************************************************
3970 * RemoveMenu (USER32.@)
3972 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3974 LPPOPUPMENU menu;
3975 MENUITEM *item;
3977 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3978 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3979 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3981 /* Remove item */
3983 MENU_FreeItemData( item );
3985 if (--menu->nItems == 0)
3987 HeapFree( GetProcessHeap(), 0, menu->items );
3988 menu->items = NULL;
3990 else
3992 MENUITEM *new_items;
3993 while(nPos < menu->nItems)
3995 *item = *(item+1);
3996 item++;
3997 nPos++;
3999 new_items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
4000 menu->nItems * sizeof(MENUITEM) );
4001 if (new_items) menu->items = new_items;
4003 return TRUE;
4007 /**********************************************************************
4008 * DeleteMenu (USER32.@)
4010 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
4012 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
4013 if (!item) return FALSE;
4014 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
4015 /* nPos is now the position of the item */
4016 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
4017 return TRUE;
4021 /*******************************************************************
4022 * ModifyMenuW (USER32.@)
4024 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
4025 UINT_PTR id, LPCWSTR str )
4027 MENUITEM *item;
4028 MENUITEMINFOW mii;
4030 if (IS_STRING_ITEM(flags))
4031 TRACE("%p %d %04x %04lx %s\n", hMenu, pos, flags, id, debugstr_w(str) );
4032 else
4033 TRACE("%p %d %04x %04lx %p\n", hMenu, pos, flags, id, str );
4035 if (!(item = MENU_FindItem( &hMenu, &pos, flags )))
4037 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4038 if (pos == SC_TASKLIST && !(flags & MF_BYPOSITION)) return TRUE;
4039 return FALSE;
4041 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
4042 MENU_mnu2mnuii( flags, id, str, &mii);
4043 return SetMenuItemInfo_common( item, &mii, TRUE);
4047 /*******************************************************************
4048 * ModifyMenuA (USER32.@)
4050 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
4051 UINT_PTR id, LPCSTR str )
4053 BOOL ret = FALSE;
4055 if (IS_STRING_ITEM(flags) && str)
4057 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4058 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
4059 if (newstr)
4061 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
4062 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
4063 HeapFree( GetProcessHeap(), 0, newstr );
4065 return ret;
4067 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
4071 /**********************************************************************
4072 * CreatePopupMenu (USER32.@)
4074 HMENU WINAPI CreatePopupMenu(void)
4076 HMENU hmenu;
4077 POPUPMENU *menu;
4079 if (!(hmenu = CreateMenu())) return 0;
4080 menu = MENU_GetMenu( hmenu );
4081 menu->wFlags |= MF_POPUP;
4082 return hmenu;
4086 /**********************************************************************
4087 * GetMenuCheckMarkDimensions (USER.417)
4088 * GetMenuCheckMarkDimensions (USER32.@)
4090 DWORD WINAPI GetMenuCheckMarkDimensions(void)
4092 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
4096 /**********************************************************************
4097 * SetMenuItemBitmaps (USER32.@)
4099 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
4100 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
4102 MENUITEM *item;
4104 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
4106 if (!hNewCheck && !hNewUnCheck)
4108 item->fState &= ~MF_USECHECKBITMAPS;
4110 else /* Install new bitmaps */
4112 item->hCheckBit = hNewCheck;
4113 item->hUnCheckBit = hNewUnCheck;
4114 item->fState |= MF_USECHECKBITMAPS;
4116 return TRUE;
4120 /**********************************************************************
4121 * CreateMenu (USER32.@)
4123 HMENU WINAPI CreateMenu(void)
4125 HMENU hMenu;
4126 LPPOPUPMENU menu;
4128 if (!(menu = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*menu) ))) return 0;
4129 menu->FocusedItem = NO_SELECTED_ITEM;
4131 if (!(hMenu = alloc_user_handle( &menu->obj, USER_MENU ))) HeapFree( GetProcessHeap(), 0, menu );
4133 TRACE("return %p\n", hMenu );
4135 return hMenu;
4139 /**********************************************************************
4140 * DestroyMenu (USER32.@)
4142 BOOL WINAPI DestroyMenu( HMENU hMenu )
4144 LPPOPUPMENU lppop;
4146 TRACE("(%p)\n", hMenu);
4148 if (!(lppop = free_user_handle( hMenu, USER_MENU ))) return FALSE;
4149 if (lppop == OBJ_OTHER_PROCESS) return FALSE;
4151 /* DestroyMenu should not destroy system menu popup owner */
4152 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
4154 DestroyWindow( lppop->hWnd );
4155 lppop->hWnd = 0;
4158 if (lppop->items) /* recursively destroy submenus */
4160 int i;
4161 MENUITEM *item = lppop->items;
4162 for (i = lppop->nItems; i > 0; i--, item++)
4164 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
4165 MENU_FreeItemData( item );
4167 HeapFree( GetProcessHeap(), 0, lppop->items );
4169 HeapFree( GetProcessHeap(), 0, lppop );
4170 return TRUE;
4174 /**********************************************************************
4175 * GetSystemMenu (USER32.@)
4177 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4179 WND *wndPtr = WIN_GetPtr( hWnd );
4180 HMENU retvalue = 0;
4182 if (wndPtr == WND_DESKTOP) return 0;
4183 if (wndPtr == WND_OTHER_PROCESS)
4185 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
4187 else if (wndPtr)
4189 if (wndPtr->hSysMenu && bRevert)
4191 DestroyMenu(wndPtr->hSysMenu);
4192 wndPtr->hSysMenu = 0;
4195 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
4196 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, 0 );
4198 if( wndPtr->hSysMenu )
4200 POPUPMENU *menu;
4201 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
4203 /* Store the dummy sysmenu handle to facilitate the refresh */
4204 /* of the close button if the SC_CLOSE item change */
4205 menu = MENU_GetMenu(retvalue);
4206 if ( menu )
4207 menu->hSysMenuOwner = wndPtr->hSysMenu;
4209 WIN_ReleasePtr( wndPtr );
4211 return bRevert ? 0 : retvalue;
4215 /*******************************************************************
4216 * SetSystemMenu (USER32.@)
4218 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4220 WND *wndPtr = WIN_GetPtr( hwnd );
4222 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
4224 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
4225 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
4226 WIN_ReleasePtr( wndPtr );
4227 return TRUE;
4229 return FALSE;
4233 /**********************************************************************
4234 * GetMenu (USER32.@)
4236 HMENU WINAPI GetMenu( HWND hWnd )
4238 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
4239 TRACE("for %p returning %p\n", hWnd, retvalue);
4240 return retvalue;
4243 /**********************************************************************
4244 * GetMenuBarInfo (USER32.@)
4246 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
4248 POPUPMENU *menu;
4249 HMENU hmenu = NULL;
4250 ATOM class_atom;
4252 TRACE( "(%p,0x%08x,0x%08x,%p)\n", hwnd, idObject, idItem, pmbi );
4254 switch (idObject)
4256 case OBJID_CLIENT:
4257 class_atom = GetClassLongW(hwnd, GCW_ATOM);
4258 if (!class_atom)
4259 return FALSE;
4260 if (class_atom != POPUPMENU_CLASS_ATOM)
4262 WARN("called on invalid window: %d\n", class_atom);
4263 SetLastError(ERROR_INVALID_MENU_HANDLE);
4264 return FALSE;
4267 hmenu = (HMENU)GetWindowLongPtrW(hwnd, 0);
4268 break;
4269 case OBJID_MENU:
4270 hmenu = GetMenu(hwnd);
4271 break;
4272 case OBJID_SYSMENU:
4273 hmenu = GetSystemMenu(hwnd, FALSE);
4274 break;
4275 default:
4276 return FALSE;
4279 if (!hmenu)
4280 return FALSE;
4282 if (pmbi->cbSize != sizeof(MENUBARINFO))
4284 SetLastError(ERROR_INVALID_PARAMETER);
4285 return FALSE;
4288 menu = MENU_GetMenu(hmenu);
4289 if (!menu)
4290 return FALSE;
4291 if (idItem < 0 || idItem > menu->nItems)
4292 return FALSE;
4294 if (!menu->Height)
4296 SetRectEmpty(&pmbi->rcBar);
4298 else if (idItem == 0)
4300 GetMenuItemRect(hwnd, hmenu, 0, &pmbi->rcBar);
4301 pmbi->rcBar.right = pmbi->rcBar.left + menu->Width;
4302 pmbi->rcBar.bottom = pmbi->rcBar.top + menu->Height;
4304 else
4306 GetMenuItemRect(hwnd, hmenu, idItem - 1, &pmbi->rcBar);
4309 pmbi->hMenu = hmenu;
4310 pmbi->hwndMenu = NULL;
4311 pmbi->fBarFocused = top_popup_hmenu == hmenu;
4312 if (idItem)
4314 pmbi->fFocused = menu->FocusedItem == idItem - 1;
4315 if (pmbi->fFocused && (menu->items[idItem - 1].fType & MF_POPUP))
4317 menu = MENU_GetMenu(menu->items[idItem - 1].hSubMenu);
4318 if (menu)
4319 pmbi->hwndMenu = menu->hWnd;
4322 else
4324 pmbi->fFocused = pmbi->fBarFocused;
4327 return TRUE;
4330 /**********************************************************************
4331 * MENU_SetMenu
4333 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
4334 * SetWindowPos call that would result if SetMenu were called directly.
4336 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
4338 TRACE("(%p, %p);\n", hWnd, hMenu);
4340 if (hMenu && !IsMenu(hMenu))
4342 WARN("hMenu %p is not a menu handle\n", hMenu);
4343 return FALSE;
4345 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4346 return FALSE;
4348 hWnd = WIN_GetFullHandle( hWnd );
4349 if (GetCapture() == hWnd)
4350 set_capture_window( 0, GUI_INMENUMODE, NULL ); /* release the capture */
4352 if (hMenu != 0)
4354 LPPOPUPMENU lpmenu;
4356 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
4358 lpmenu->hWnd = hWnd;
4359 lpmenu->Height = 0; /* Make sure we recalculate the size */
4361 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
4362 return TRUE;
4366 /**********************************************************************
4367 * SetMenu (USER32.@)
4369 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4371 if(!MENU_SetMenu(hWnd, hMenu))
4372 return FALSE;
4374 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4375 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4376 return TRUE;
4380 /**********************************************************************
4381 * GetSubMenu (USER32.@)
4383 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4385 MENUITEM * lpmi;
4387 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4388 if (!(lpmi->fType & MF_POPUP)) return 0;
4389 return lpmi->hSubMenu;
4393 /**********************************************************************
4394 * DrawMenuBar (USER32.@)
4396 BOOL WINAPI DrawMenuBar( HWND hWnd )
4398 LPPOPUPMENU lppop;
4399 HMENU hMenu;
4401 if (!IsWindow( hWnd ))
4402 return FALSE;
4403 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4404 return TRUE;
4406 if ((hMenu = GetMenu( hWnd )) && (lppop = MENU_GetMenu( hMenu ))) {
4407 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4408 lppop->hwndOwner = hWnd;
4411 return SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4412 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4415 /***********************************************************************
4416 * DrawMenuBarTemp (USER32.@)
4418 * UNDOCUMENTED !!
4420 * called by W98SE desk.cpl Control Panel Applet
4422 * Not 100% sure about the param names, but close.
4424 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
4426 LPPOPUPMENU lppop;
4427 UINT i,retvalue;
4428 HFONT hfontOld = 0;
4429 BOOL flat_menu = FALSE;
4431 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
4433 if (!hMenu)
4434 hMenu = GetMenu(hwnd);
4436 if (!hFont)
4437 hFont = get_menu_font(FALSE);
4439 lppop = MENU_GetMenu( hMenu );
4440 if (lppop == NULL || lprect == NULL)
4442 retvalue = GetSystemMetrics(SM_CYMENU);
4443 goto END;
4446 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
4448 hfontOld = SelectObject( hDC, hFont);
4450 if (lppop->Height == 0)
4451 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
4453 lprect->bottom = lprect->top + lppop->Height;
4455 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
4457 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
4458 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
4459 LineTo( hDC, lprect->right, lprect->bottom );
4461 if (lppop->nItems == 0)
4463 retvalue = GetSystemMetrics(SM_CYMENU);
4464 goto END;
4467 for (i = 0; i < lppop->nItems; i++)
4468 MENU_DrawMenuItem( hwnd, lppop, hwnd, hDC, &lppop->items[i], TRUE, ODA_DRAWENTIRE );
4470 retvalue = lppop->Height;
4472 END:
4473 if (hfontOld) SelectObject (hDC, hfontOld);
4474 return retvalue;
4477 /***********************************************************************
4478 * EndMenu (USER.187)
4479 * EndMenu (USER32.@)
4481 BOOL WINAPI EndMenu(void)
4483 /* if we are in the menu code, and it is active */
4484 if (!fEndMenu && top_popup)
4486 /* terminate the menu handling code */
4487 fEndMenu = TRUE;
4489 /* needs to be posted to wakeup the internal menu handler */
4490 /* which will now terminate the menu, in the event that */
4491 /* the main window was minimized, or lost focus, so we */
4492 /* don't end up with an orphaned menu */
4493 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
4495 return fEndMenu;
4499 /*****************************************************************
4500 * LoadMenuA (USER32.@)
4502 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4504 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
4505 if (!hrsrc) return 0;
4506 return LoadMenuIndirectA( LoadResource( instance, hrsrc ));
4510 /*****************************************************************
4511 * LoadMenuW (USER32.@)
4513 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4515 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
4516 if (!hrsrc) return 0;
4517 return LoadMenuIndirectW( LoadResource( instance, hrsrc ));
4521 /**********************************************************************
4522 * LoadMenuIndirectW (USER32.@)
4524 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4526 HMENU hMenu;
4527 WORD version, offset;
4528 LPCSTR p = template;
4530 version = GET_WORD(p);
4531 p += sizeof(WORD);
4532 TRACE("%p, ver %d\n", template, version );
4533 switch (version)
4535 case 0: /* standard format is version of 0 */
4536 offset = GET_WORD(p);
4537 p += sizeof(WORD) + offset;
4538 if (!(hMenu = CreateMenu())) return 0;
4539 if (!MENU_ParseResource( p, hMenu ))
4541 DestroyMenu( hMenu );
4542 return 0;
4544 return hMenu;
4545 case 1: /* extended format is version of 1 */
4546 offset = GET_WORD(p);
4547 p += sizeof(WORD) + offset;
4548 if (!(hMenu = CreateMenu())) return 0;
4549 if (!MENUEX_ParseResource( p, hMenu))
4551 DestroyMenu( hMenu );
4552 return 0;
4554 return hMenu;
4555 default:
4556 ERR("version %d not supported.\n", version);
4557 return 0;
4562 /**********************************************************************
4563 * LoadMenuIndirectA (USER32.@)
4565 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4567 return LoadMenuIndirectW( template );
4571 /**********************************************************************
4572 * IsMenu (USER32.@)
4574 BOOL WINAPI IsMenu(HMENU hmenu)
4576 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4578 if (!menu)
4580 SetLastError(ERROR_INVALID_MENU_HANDLE);
4581 return FALSE;
4583 return TRUE;
4586 /**********************************************************************
4587 * GetMenuItemInfo_common
4590 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4591 LPMENUITEMINFOW lpmii, BOOL unicode)
4593 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos ? MF_BYPOSITION : 0);
4595 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4597 if (!menu) {
4598 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
4599 return FALSE;
4602 if( lpmii->fMask & MIIM_TYPE) {
4603 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4604 WARN("invalid combination of fMask bits used\n");
4605 /* this does not happen on Win9x/ME */
4606 SetLastError( ERROR_INVALID_PARAMETER);
4607 return FALSE;
4609 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4610 if (menu->hbmpItem && !IS_MAGIC_BITMAP(menu->hbmpItem))
4611 lpmii->fType |= MFT_BITMAP;
4612 lpmii->hbmpItem = menu->hbmpItem; /* not on Win9x/ME */
4613 if( lpmii->fType & MFT_BITMAP) {
4614 lpmii->dwTypeData = (LPWSTR) menu->hbmpItem;
4615 lpmii->cch = 0;
4616 } else if( lpmii->fType & (MFT_OWNERDRAW | MFT_SEPARATOR)) {
4617 /* this does not happen on Win9x/ME */
4618 lpmii->dwTypeData = 0;
4619 lpmii->cch = 0;
4623 /* copy the text string */
4624 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING))) {
4625 if( !menu->text ) {
4626 if(lpmii->dwTypeData && lpmii->cch) {
4627 if( unicode)
4628 *((WCHAR *)lpmii->dwTypeData) = 0;
4629 else
4630 *((CHAR *)lpmii->dwTypeData) = 0;
4632 lpmii->cch = 0;
4633 } else {
4634 int len;
4635 if (unicode)
4637 len = strlenW(menu->text);
4638 if(lpmii->dwTypeData && lpmii->cch)
4639 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4641 else
4643 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL,
4644 0, NULL, NULL ) - 1;
4645 if(lpmii->dwTypeData && lpmii->cch)
4646 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4647 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4648 ((LPSTR)lpmii->dwTypeData)[lpmii->cch - 1] = 0;
4650 /* if we've copied a substring we return its length */
4651 if(lpmii->dwTypeData && lpmii->cch)
4652 if (lpmii->cch <= len + 1)
4653 lpmii->cch--;
4654 else
4655 lpmii->cch = len;
4656 else {
4657 /* return length of string */
4658 /* not on Win9x/ME if fType & MFT_BITMAP */
4659 lpmii->cch = len;
4664 if (lpmii->fMask & MIIM_FTYPE)
4665 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4667 if (lpmii->fMask & MIIM_BITMAP)
4668 lpmii->hbmpItem = menu->hbmpItem;
4670 if (lpmii->fMask & MIIM_STATE)
4671 lpmii->fState = menu->fState & MENUITEMINFO_STATE_MASK;
4673 if (lpmii->fMask & MIIM_ID)
4674 lpmii->wID = menu->wID;
4676 if (lpmii->fMask & MIIM_SUBMENU)
4677 lpmii->hSubMenu = menu->hSubMenu;
4678 else {
4679 /* hSubMenu is always cleared
4680 * (not on Win9x/ME ) */
4681 lpmii->hSubMenu = 0;
4684 if (lpmii->fMask & MIIM_CHECKMARKS) {
4685 lpmii->hbmpChecked = menu->hCheckBit;
4686 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4688 if (lpmii->fMask & MIIM_DATA)
4689 lpmii->dwItemData = menu->dwItemData;
4691 return TRUE;
4694 /**********************************************************************
4695 * GetMenuItemInfoA (USER32.@)
4697 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4698 LPMENUITEMINFOA lpmii)
4700 BOOL ret;
4701 MENUITEMINFOA mii;
4702 if( lpmii->cbSize != sizeof( mii) &&
4703 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4704 SetLastError( ERROR_INVALID_PARAMETER);
4705 return FALSE;
4707 memcpy( &mii, lpmii, lpmii->cbSize);
4708 mii.cbSize = sizeof( mii);
4709 ret = GetMenuItemInfo_common (hmenu, item, bypos,
4710 (LPMENUITEMINFOW)&mii, FALSE);
4711 mii.cbSize = lpmii->cbSize;
4712 memcpy( lpmii, &mii, mii.cbSize);
4713 return ret;
4716 /**********************************************************************
4717 * GetMenuItemInfoW (USER32.@)
4719 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4720 LPMENUITEMINFOW lpmii)
4722 BOOL ret;
4723 MENUITEMINFOW mii;
4724 if( lpmii->cbSize != sizeof( mii) &&
4725 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4726 SetLastError( ERROR_INVALID_PARAMETER);
4727 return FALSE;
4729 memcpy( &mii, lpmii, lpmii->cbSize);
4730 mii.cbSize = sizeof( mii);
4731 ret = GetMenuItemInfo_common (hmenu, item, bypos, &mii, TRUE);
4732 mii.cbSize = lpmii->cbSize;
4733 memcpy( lpmii, &mii, mii.cbSize);
4734 return ret;
4738 /* set a menu item text from a ASCII or Unicode string */
4739 static inline void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4741 if (!text)
4742 menu->text = NULL;
4743 else if (unicode)
4745 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4746 strcpyW( menu->text, text );
4748 else
4750 LPCSTR str = (LPCSTR)text;
4751 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4752 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4753 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4758 /**********************************************************************
4759 * MENU_depth
4761 * detect if there are loops in the menu tree (or the depth is too large)
4763 static int MENU_depth( POPUPMENU *pmenu, int depth)
4765 UINT i;
4766 MENUITEM *item;
4767 int subdepth;
4769 depth++;
4770 if( depth > MAXMENUDEPTH) return depth;
4771 item = pmenu->items;
4772 subdepth = depth;
4773 for( i = 0; i < pmenu->nItems && subdepth <= MAXMENUDEPTH; i++, item++){
4774 POPUPMENU *psubmenu = item->hSubMenu ? MENU_GetMenu( item->hSubMenu) : NULL;
4775 if( psubmenu){
4776 int bdepth = MENU_depth( psubmenu, depth);
4777 if( bdepth > subdepth) subdepth = bdepth;
4779 if( subdepth > MAXMENUDEPTH)
4780 TRACE("<- hmenu %p\n", item->hSubMenu);
4782 return subdepth;
4786 /**********************************************************************
4787 * SetMenuItemInfo_common
4789 * Note: does not support the MIIM_TYPE flag. Use the MIIM_FTYPE,
4790 * MIIM_BITMAP and MIIM_STRING flags instead.
4793 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4794 const MENUITEMINFOW *lpmii,
4795 BOOL unicode)
4797 if (!menu) return FALSE;
4799 debug_print_menuitem("SetMenuItemInfo_common from: ", menu, "");
4801 if (lpmii->fMask & MIIM_FTYPE ) {
4802 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4803 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4805 if (lpmii->fMask & MIIM_STRING ) {
4806 /* free the string when used */
4807 HeapFree(GetProcessHeap(), 0, menu->text);
4808 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4811 if (lpmii->fMask & MIIM_STATE)
4812 /* Other menu items having MFS_DEFAULT are not converted
4813 to normal items */
4814 menu->fState = lpmii->fState & MENUITEMINFO_STATE_MASK;
4816 if (lpmii->fMask & MIIM_ID)
4817 menu->wID = lpmii->wID;
4819 if (lpmii->fMask & MIIM_SUBMENU) {
4820 menu->hSubMenu = lpmii->hSubMenu;
4821 if (menu->hSubMenu) {
4822 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4823 if (subMenu) {
4824 if( MENU_depth( subMenu, 0) > MAXMENUDEPTH) {
4825 ERR( "Loop detected in menu hierarchy or maximum menu depth exceeded!\n");
4826 menu->hSubMenu = 0;
4827 return FALSE;
4829 subMenu->wFlags |= MF_POPUP;
4830 menu->fType |= MF_POPUP;
4831 } else {
4832 SetLastError( ERROR_INVALID_PARAMETER);
4833 return FALSE;
4836 else
4837 menu->fType &= ~MF_POPUP;
4840 if (lpmii->fMask & MIIM_CHECKMARKS)
4842 menu->hCheckBit = lpmii->hbmpChecked;
4843 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4845 if (lpmii->fMask & MIIM_DATA)
4846 menu->dwItemData = lpmii->dwItemData;
4848 if (lpmii->fMask & MIIM_BITMAP)
4849 menu->hbmpItem = lpmii->hbmpItem;
4851 if( !menu->text && !(menu->fType & MFT_OWNERDRAW) && !menu->hbmpItem)
4852 menu->fType |= MFT_SEPARATOR;
4854 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4855 return TRUE;
4858 /**********************************************************************
4859 * MENU_NormalizeMenuItemInfoStruct
4861 * Helper for SetMenuItemInfo and InsertMenuItemInfo:
4862 * check, copy and extend the MENUITEMINFO struct from the version that the application
4863 * supplied to the version used by wine source. */
4864 static BOOL MENU_NormalizeMenuItemInfoStruct( const MENUITEMINFOW *pmii_in,
4865 MENUITEMINFOW *pmii_out )
4867 /* do we recognize the size? */
4868 if( !pmii_in || (pmii_in->cbSize != sizeof( MENUITEMINFOW) &&
4869 pmii_in->cbSize != sizeof( MENUITEMINFOW) - sizeof( pmii_in->hbmpItem)) ) {
4870 SetLastError( ERROR_INVALID_PARAMETER);
4871 return FALSE;
4873 /* copy the fields that we have */
4874 memcpy( pmii_out, pmii_in, pmii_in->cbSize);
4875 /* if the hbmpItem member is missing then extend */
4876 if( pmii_in->cbSize != sizeof( MENUITEMINFOW)) {
4877 pmii_out->cbSize = sizeof( MENUITEMINFOW);
4878 pmii_out->hbmpItem = NULL;
4880 /* test for invalid bit combinations */
4881 if( (pmii_out->fMask & MIIM_TYPE &&
4882 pmii_out->fMask & (MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) ||
4883 (pmii_out->fMask & MIIM_FTYPE && pmii_out->fType & MFT_BITMAP)) {
4884 WARN("invalid combination of fMask bits used\n");
4885 /* this does not happen on Win9x/ME */
4886 SetLastError( ERROR_INVALID_PARAMETER);
4887 return FALSE;
4889 /* convert old style (MIIM_TYPE) to the new */
4890 if( pmii_out->fMask & MIIM_TYPE){
4891 pmii_out->fMask |= MIIM_FTYPE;
4892 if( IS_STRING_ITEM(pmii_out->fType)){
4893 pmii_out->fMask |= MIIM_STRING;
4894 } else if( (pmii_out->fType) & MFT_BITMAP){
4895 pmii_out->fMask |= MIIM_BITMAP;
4896 pmii_out->hbmpItem = UlongToHandle(LOWORD(pmii_out->dwTypeData));
4899 return TRUE;
4902 /**********************************************************************
4903 * SetMenuItemInfoA (USER32.@)
4905 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4906 const MENUITEMINFOA *lpmii)
4908 MENUITEM *menuitem;
4909 MENUITEMINFOW mii;
4911 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4913 if (!MENU_NormalizeMenuItemInfoStruct( (const MENUITEMINFOW *)lpmii, &mii )) return FALSE;
4915 if (!(menuitem = MENU_FindItem( &hmenu, &item, bypos? MF_BYPOSITION : 0 )))
4917 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4918 if (item == SC_TASKLIST && !bypos) return TRUE;
4919 return FALSE;
4921 return SetMenuItemInfo_common( menuitem, &mii, FALSE );
4924 /**********************************************************************
4925 * SetMenuItemInfoW (USER32.@)
4927 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4928 const MENUITEMINFOW *lpmii)
4930 MENUITEM *menuitem;
4931 MENUITEMINFOW mii;
4933 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4935 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
4936 if (!(menuitem = MENU_FindItem( &hmenu, &item, bypos? MF_BYPOSITION : 0 )))
4938 /* workaround for Word 95: pretend that SC_TASKLIST item exists */
4939 if (item == SC_TASKLIST && !bypos) return TRUE;
4940 return FALSE;
4942 return SetMenuItemInfo_common( menuitem, &mii, TRUE );
4945 /**********************************************************************
4946 * SetMenuDefaultItem (USER32.@)
4949 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4951 UINT i;
4952 POPUPMENU *menu;
4953 MENUITEM *item;
4955 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4957 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4959 /* reset all default-item flags */
4960 item = menu->items;
4961 for (i = 0; i < menu->nItems; i++, item++)
4963 item->fState &= ~MFS_DEFAULT;
4966 /* no default item */
4967 if ( -1 == uItem)
4969 return TRUE;
4972 item = menu->items;
4973 if ( bypos )
4975 if ( uItem >= menu->nItems ) return FALSE;
4976 item[uItem].fState |= MFS_DEFAULT;
4977 return TRUE;
4979 else
4981 for (i = 0; i < menu->nItems; i++, item++)
4983 if (item->wID == uItem)
4985 item->fState |= MFS_DEFAULT;
4986 return TRUE;
4991 return FALSE;
4994 /**********************************************************************
4995 * GetMenuDefaultItem (USER32.@)
4997 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4999 POPUPMENU *menu;
5000 MENUITEM * item;
5001 UINT i = 0;
5003 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
5005 if (!(menu = MENU_GetMenu(hmenu))) return -1;
5007 /* find default item */
5008 item = menu->items;
5010 /* empty menu */
5011 if (! item) return -1;
5013 while ( !( item->fState & MFS_DEFAULT ) )
5015 i++; item++;
5016 if (i >= menu->nItems ) return -1;
5019 /* default: don't return disabled items */
5020 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
5022 /* search rekursiv when needed */
5023 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
5025 UINT ret;
5026 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
5027 if ( -1 != ret ) return ret;
5029 /* when item not found in submenu, return the popup item */
5031 return ( bypos ) ? i : item->wID;
5036 /**********************************************************************
5037 * InsertMenuItemA (USER32.@)
5039 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
5040 const MENUITEMINFOA *lpmii)
5042 MENUITEM *item;
5043 MENUITEMINFOW mii;
5045 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
5047 if (!MENU_NormalizeMenuItemInfoStruct( (const MENUITEMINFOW *)lpmii, &mii )) return FALSE;
5049 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
5050 return SetMenuItemInfo_common(item, &mii, FALSE);
5054 /**********************************************************************
5055 * InsertMenuItemW (USER32.@)
5057 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
5058 const MENUITEMINFOW *lpmii)
5060 MENUITEM *item;
5061 MENUITEMINFOW mii;
5063 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
5065 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
5067 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
5068 return SetMenuItemInfo_common(item, &mii, TRUE);
5071 /**********************************************************************
5072 * CheckMenuRadioItem (USER32.@)
5075 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
5076 UINT first, UINT last, UINT check,
5077 UINT bypos)
5079 BOOL done = FALSE;
5080 UINT i;
5081 MENUITEM *mi_first = NULL, *mi_check;
5082 HMENU m_first, m_check;
5084 for (i = first; i <= last; i++)
5086 UINT pos = i;
5088 if (!mi_first)
5090 m_first = hMenu;
5091 mi_first = MENU_FindItem(&m_first, &pos, bypos);
5092 if (!mi_first) continue;
5093 mi_check = mi_first;
5094 m_check = m_first;
5096 else
5098 m_check = hMenu;
5099 mi_check = MENU_FindItem(&m_check, &pos, bypos);
5100 if (!mi_check) continue;
5103 if (m_first != m_check) continue;
5104 if (mi_check->fType == MFT_SEPARATOR) continue;
5106 if (i == check)
5108 mi_check->fType |= MFT_RADIOCHECK;
5109 mi_check->fState |= MFS_CHECKED;
5110 done = TRUE;
5112 else
5114 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
5115 mi_check->fState &= ~MFS_CHECKED;
5119 return done;
5123 /**********************************************************************
5124 * GetMenuItemRect (USER32.@)
5126 * ATTENTION: Here, the returned values in rect are the screen
5127 * coordinates of the item just like if the menu was
5128 * always on the upper left side of the application.
5131 BOOL WINAPI GetMenuItemRect(HWND hwnd, HMENU hMenu, UINT uItem, RECT *rect)
5133 POPUPMENU *menu;
5134 MENUITEM *item;
5136 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
5138 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
5139 if ((rect == NULL) || (item == NULL))
5140 return FALSE;
5142 menu = MENU_GetMenu(hMenu);
5143 if (!menu) return FALSE;
5145 if (!hwnd) hwnd = menu->hWnd;
5146 if (!hwnd) return FALSE;
5148 *rect = item->rect;
5150 OffsetRect(rect, menu->items_rect.left, menu->items_rect.top);
5151 MapWindowPoints(hwnd, 0, (POINT *)rect, 2);
5153 return TRUE;
5156 /**********************************************************************
5157 * SetMenuInfo (USER32.@)
5159 * FIXME
5160 * actually use the items to draw the menu
5161 * (recalculate and/or redraw)
5163 static BOOL menu_SetMenuInfo( HMENU hMenu, LPCMENUINFO lpmi)
5165 POPUPMENU *menu;
5166 if( !(menu = MENU_GetMenu(hMenu))) return FALSE;
5168 if (lpmi->fMask & MIM_BACKGROUND)
5169 menu->hbrBack = lpmi->hbrBack;
5171 if (lpmi->fMask & MIM_HELPID)
5172 menu->dwContextHelpID = lpmi->dwContextHelpID;
5174 if (lpmi->fMask & MIM_MAXHEIGHT)
5175 menu->cyMax = lpmi->cyMax;
5177 if (lpmi->fMask & MIM_MENUDATA)
5178 menu->dwMenuData = lpmi->dwMenuData;
5180 if (lpmi->fMask & MIM_STYLE)
5181 menu->dwStyle = lpmi->dwStyle;
5183 if( lpmi->fMask & MIM_APPLYTOSUBMENUS) {
5184 int i;
5185 MENUITEM *item = menu->items;
5186 for( i = menu->nItems; i; i--, item++)
5187 if( item->fType & MF_POPUP)
5188 menu_SetMenuInfo( item->hSubMenu, lpmi);
5190 return TRUE;
5193 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
5195 TRACE("(%p %p)\n", hMenu, lpmi);
5196 if( lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu_SetMenuInfo( hMenu, lpmi))) {
5197 if( lpmi->fMask & MIM_STYLE) {
5198 if (lpmi->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
5199 if (lpmi->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
5200 if (lpmi->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
5202 return TRUE;
5204 SetLastError( ERROR_INVALID_PARAMETER);
5205 return FALSE;
5208 /**********************************************************************
5209 * GetMenuInfo (USER32.@)
5211 * NOTES
5212 * win98/NT5.0
5215 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
5216 { POPUPMENU *menu;
5218 TRACE("(%p %p)\n", hMenu, lpmi);
5220 if (lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
5223 if (lpmi->fMask & MIM_BACKGROUND)
5224 lpmi->hbrBack = menu->hbrBack;
5226 if (lpmi->fMask & MIM_HELPID)
5227 lpmi->dwContextHelpID = menu->dwContextHelpID;
5229 if (lpmi->fMask & MIM_MAXHEIGHT)
5230 lpmi->cyMax = menu->cyMax;
5232 if (lpmi->fMask & MIM_MENUDATA)
5233 lpmi->dwMenuData = menu->dwMenuData;
5235 if (lpmi->fMask & MIM_STYLE)
5236 lpmi->dwStyle = menu->dwStyle;
5238 return TRUE;
5240 SetLastError( ERROR_INVALID_PARAMETER);
5241 return FALSE;
5245 /**********************************************************************
5246 * SetMenuContextHelpId (USER32.@)
5248 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
5250 LPPOPUPMENU menu;
5252 TRACE("(%p 0x%08x)\n", hMenu, dwContextHelpID);
5254 if ((menu = MENU_GetMenu(hMenu)))
5256 menu->dwContextHelpID = dwContextHelpID;
5257 return TRUE;
5259 return FALSE;
5263 /**********************************************************************
5264 * GetMenuContextHelpId (USER32.@)
5266 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
5268 LPPOPUPMENU menu;
5270 TRACE("(%p)\n", hMenu);
5272 if ((menu = MENU_GetMenu(hMenu)))
5274 return menu->dwContextHelpID;
5276 return 0;
5279 /**********************************************************************
5280 * MenuItemFromPoint (USER32.@)
5282 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
5284 POPUPMENU *menu = MENU_GetMenu(hMenu);
5285 UINT pos;
5287 /*FIXME: Do we have to handle hWnd here? */
5288 if (!menu) return -1;
5289 if (MENU_FindItemByCoords( menu, ptScreen, &pos ) != ht_item) return -1;
5290 return pos;
5294 /**********************************************************************
5295 * CalcMenuBar (USER32.@)
5297 DWORD WINAPI CalcMenuBar(HWND hwnd, DWORD left, DWORD right, DWORD top, RECT *rect)
5299 FIXME("(%p, %d, %d, %d, %p): stub\n", hwnd, left, right, top, rect);
5300 return 0;
5304 /**********************************************************************
5305 * translate_accelerator
5307 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
5308 BYTE fVirt, WORD key, WORD cmd )
5310 INT mask = 0;
5311 UINT mesg = 0;
5313 if (wParam != key) return FALSE;
5315 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
5316 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
5317 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
5319 if (message == WM_CHAR || message == WM_SYSCHAR)
5321 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
5323 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam) & 0xff);
5324 goto found;
5327 else
5329 if(fVirt & FVIRTKEY)
5331 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
5332 wParam, 0xff & HIWORD(lParam));
5334 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
5335 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
5337 else
5339 if (!(lParam & 0x01000000)) /* no special_key */
5341 if ((fVirt & FALT) && (lParam & 0x20000000))
5342 { /* ^^ ALT pressed */
5343 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(wParam) & 0xff);
5344 goto found;
5349 return FALSE;
5351 found:
5352 if (message == WM_KEYUP || message == WM_SYSKEYUP)
5353 mesg = 1;
5354 else
5356 HMENU hMenu, hSubMenu, hSysMenu;
5357 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
5359 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
5360 hSysMenu = get_win_sys_menu( hWnd );
5362 /* find menu item and ask application to initialize it */
5363 /* 1. in the system menu */
5364 hSubMenu = hSysMenu;
5365 nPos = cmd;
5366 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5368 if (GetCapture())
5369 mesg = 2;
5370 if (!IsWindowEnabled(hWnd))
5371 mesg = 3;
5372 else
5374 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
5375 if(hSubMenu != hSysMenu)
5377 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
5378 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
5379 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
5381 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
5384 else /* 2. in the window's menu */
5386 hSubMenu = hMenu;
5387 nPos = cmd;
5388 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5390 if (GetCapture())
5391 mesg = 2;
5392 if (!IsWindowEnabled(hWnd))
5393 mesg = 3;
5394 else
5396 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
5397 if(hSubMenu != hMenu)
5399 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
5400 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
5401 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
5403 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
5408 if (mesg == 0)
5410 if (uSysStat != (UINT)-1)
5412 if (uSysStat & (MF_DISABLED|MF_GRAYED))
5413 mesg=4;
5414 else
5415 mesg=WM_SYSCOMMAND;
5417 else
5419 if (uStat != (UINT)-1)
5421 if (IsIconic(hWnd))
5422 mesg=5;
5423 else
5425 if (uStat & (MF_DISABLED|MF_GRAYED))
5426 mesg=6;
5427 else
5428 mesg=WM_COMMAND;
5431 else
5432 mesg=WM_COMMAND;
5437 if( mesg==WM_COMMAND )
5439 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
5440 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
5442 else if( mesg==WM_SYSCOMMAND )
5444 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
5445 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
5447 else
5449 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
5450 * #0: unknown (please report!)
5451 * #1: for WM_KEYUP,WM_SYSKEYUP
5452 * #2: mouse is captured
5453 * #3: window is disabled
5454 * #4: it's a disabled system menu option
5455 * #5: it's a menu option, but window is iconic
5456 * #6: it's a menu option, but disabled
5458 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
5459 if(mesg==0)
5460 ERR_(accel)(" unknown reason - please report!\n");
5462 return TRUE;
5465 /**********************************************************************
5466 * TranslateAcceleratorA (USER32.@)
5467 * TranslateAccelerator (USER32.@)
5469 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
5471 switch (msg->message)
5473 case WM_KEYDOWN:
5474 case WM_SYSKEYDOWN:
5475 return TranslateAcceleratorW( hWnd, hAccel, msg );
5477 case WM_CHAR:
5478 case WM_SYSCHAR:
5480 MSG msgW = *msg;
5481 char ch = LOWORD(msg->wParam);
5482 WCHAR wch;
5483 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
5484 msgW.wParam = MAKEWPARAM(wch, HIWORD(msg->wParam));
5485 return TranslateAcceleratorW( hWnd, hAccel, &msgW );
5488 default:
5489 return 0;
5493 /**********************************************************************
5494 * TranslateAcceleratorW (USER32.@)
5496 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
5498 ACCEL data[32], *ptr = data;
5499 int i, count;
5501 if (!hWnd) return 0;
5503 if (msg->message != WM_KEYDOWN &&
5504 msg->message != WM_SYSKEYDOWN &&
5505 msg->message != WM_CHAR &&
5506 msg->message != WM_SYSCHAR)
5507 return 0;
5509 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5510 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5512 if (!(count = CopyAcceleratorTableW( hAccel, NULL, 0 ))) return 0;
5513 if (count > sizeof(data)/sizeof(data[0]))
5515 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*ptr) ))) return 0;
5517 count = CopyAcceleratorTableW( hAccel, ptr, count );
5518 for (i = 0; i < count; i++)
5520 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5521 ptr[i].fVirt, ptr[i].key, ptr[i].cmd))
5522 break;
5524 if (ptr != data) HeapFree( GetProcessHeap(), 0, ptr );
5525 return (i < count);