4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
10 * Note: the style MF_MOUSESELECT is used to mark popup items that
11 * have been selected, i.e. their popup menu is currently displayed.
12 * This is probably not the meaning this style has in MS-Windows.
21 #include "wine/winbase16.h"
22 #include "wine/winuser16.h"
23 #include "sysmetrics.h"
27 #include "nonclient.h"
36 /* internal popup menu window messages */
38 #define MM_SETMENUHANDLE (WM_USER + 0)
39 #define MM_GETMENUHANDLE (WM_USER + 1)
41 /* Menu item structure */
43 /* ----------- MENUITEMINFO Stuff ----------- */
44 UINT fType; /* Item type. */
45 UINT fState; /* Item state. */
46 UINT wID; /* Item id. */
47 HMENU hSubMenu; /* Pop-up menu. */
48 HBITMAP hCheckBit; /* Bitmap when checked. */
49 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
50 LPSTR text; /* Item text or bitmap handle. */
51 DWORD dwItemData; /* Application defined. */
52 /* ----------- Wine stuff ----------- */
53 RECT rect; /* Item area (relative to menu window) */
54 UINT xTab; /* X position of text after Tab */
57 /* Popup menu structure */
59 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
60 WORD wMagic; /* Magic number */
61 HQUEUE16 hTaskQ; /* Task queue for this menu */
62 WORD Width; /* Width of the whole menu */
63 WORD Height; /* Height of the whole menu */
64 WORD nItems; /* Number of items in the menu */
65 HWND hWnd; /* Window containing the menu */
66 MENUITEM *items; /* Array of menu items */
67 UINT FocusedItem; /* Currently focused item */
68 WORD defitem; /* default item position. Unused (except for set/get)*/
69 } POPUPMENU, *LPPOPUPMENU;
71 /* internal flags for menu tracking */
73 #define TF_ENDMENU 0x0001
74 #define TF_SUSPENDPOPUP 0x0002
75 #define TF_SKIPREMOVE 0x0004
80 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
81 HMENU hTopMenu; /* initial menu */
82 HWND hOwnerWnd; /* where notifications are sent */
86 #define MENU_MAGIC 0x554d /* 'MU' */
87 #define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
92 /* Internal MENU_TrackMenu() flags */
93 #define TPM_INTERNAL 0xF0000000
94 #define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
95 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
96 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
98 /* popup menu shade thickness */
99 #define POPUP_XSHADE 4
100 #define POPUP_YSHADE 4
102 /* Space between 2 menu bar items */
103 #define MENU_BAR_ITEMS_SPACE 12
105 /* Minimum width of a tab character */
106 #define MENU_TAB_SPACE 8
108 /* Height of a separator item */
109 #define SEPARATOR_HEIGHT 5
111 /* (other menu->FocusedItem values give the position of the focused item) */
112 #define NO_SELECTED_ITEM 0xffff
114 #define MENU_ITEM_TYPE(flags) \
115 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
117 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
119 #define IS_SYSTEM_MENU(menu) \
120 (!((menu)->wFlags & MF_POPUP) && (menu)->wFlags & MF_SYSMENU)
121 #define IS_SYSTEM_POPUP(menu) \
122 ((menu)->wFlags & MF_POPUP && (menu)->wFlags & MF_SYSMENU)
124 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
125 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
126 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
127 MF_POPUP | MF_SYSMENU | MF_HELP)
128 #define STATE_MASK (~TYPE_MASK)
130 /* Dimension of the menu bitmaps */
131 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
132 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
134 static HBITMAP hStdRadioCheck = 0;
135 static HBITMAP hStdCheck = 0;
136 static HBITMAP hStdMnArrow = 0;
138 // Minimze/restore/close buttons to be inserted in menubar
139 static HBITMAP hBmpMinimize = 0;
140 static HBITMAP hBmpMinimizeD = 0;
141 static HBITMAP hBmpMaximize = 0;
142 static HBITMAP hBmpMaximizeD = 0;
143 static HBITMAP hBmpClose = 0;
144 static HBITMAP hBmpCloseD = 0;
147 static HBRUSH hShadeBrush = 0;
148 static HMENU MENU_DefSysPopup = 0; /* Default system menu popup */
150 /* Use global popup window because there's no way 2 menus can
151 * be tracked at the same time. */
153 static WND* pTopPopupWnd = 0;
154 static UINT uSubPWndLevel = 0;
156 /* Flag set by EndMenu() to force an exit from menu tracking */
157 static BOOL fEndMenu = FALSE;
160 /***********************************************************************
161 * debug_print_menuitem
163 * Print a menuitem in readable form.
166 #define debug_print_menuitem(pre, mp, post) \
167 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
169 #define MENUOUT(text) \
170 dsprintf(menu, "%s%s", (count++ ? "," : ""), (text))
172 #define MENUFLAG(bit,text) \
174 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
177 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
180 dbg_decl_str(menu, 256);
183 UINT flags = mp->fType;
184 int typ = MENU_ITEM_TYPE(flags);
185 dsprintf(menu, "{ ID=0x%x", mp->wID);
186 if (flags & MF_POPUP)
187 dsprintf(menu, ", Sub=0x%x", mp->hSubMenu);
190 dsprintf(menu, ", Typ=");
191 if (typ == MFT_STRING)
193 else if (typ == MFT_SEPARATOR)
195 else if (typ == MFT_OWNERDRAW)
197 else if (typ == MFT_BITMAP)
203 MENUFLAG(MF_POPUP, "pop");
204 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
205 MENUFLAG(MFT_MENUBREAK, "brk");
206 MENUFLAG(MFT_RADIOCHECK, "radio");
207 MENUFLAG(MFT_RIGHTORDER, "rorder");
208 MENUFLAG(MF_SYSMENU, "sys");
209 MENUFLAG(MFT_RIGHTJUSTIFY, "right");
212 dsprintf(menu, "+0x%x", flags);
217 dsprintf(menu, ", State=");
218 MENUFLAG(MFS_GRAYED, "grey");
219 MENUFLAG(MFS_DISABLED, "dis");
220 MENUFLAG(MFS_CHECKED, "check");
221 MENUFLAG(MFS_HILITE, "hi");
222 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
223 MENUFLAG(MF_MOUSESELECT, "mouse");
225 dsprintf(menu, "+0x%x", flags);
228 dsprintf(menu, ", Chk=0x%x", mp->hCheckBit);
230 dsprintf(menu, ", Unc=0x%x", mp->hUnCheckBit);
232 if (typ == MFT_STRING) {
234 dsprintf(menu, ", Text=\"%s\"", mp->text);
236 dsprintf(menu, ", Text=Null");
237 } else if (mp->text == NULL)
240 dsprintf(menu, ", Text=%p", mp->text);
242 dsprintf(menu, ", ItemData=0x%08lx", mp->dwItemData);
243 dsprintf(menu, " }");
245 dsprintf(menu, "NULL");
248 TRACE(menu, "%s %s %s\n", prefix, dbg_str(menu), postfix);
254 /***********************************************************************
257 * Return the default system menu.
259 static HMENU MENU_CopySysPopup(void)
261 HMENU hMenu = LoadMenuIndirectA(SYSRES_GetResPtr(SYSRES_MENU_SYSMENU));
264 POPUPMENU* menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
265 menu->wFlags |= MF_SYSMENU | MF_POPUP;
269 ERR(menu, "Unable to load default system menu\n" );
272 TRACE(menu, "returning %x.\n", hMenu );
277 /***********************************************************************
278 * MENU_GetTopPopupWnd()
280 * Return the locked pointer pTopPopupWnd.
282 WND *MENU_GetTopPopupWnd()
284 return WIN_LockWndPtr(pTopPopupWnd);
286 /***********************************************************************
287 * MENU_ReleaseTopPopupWnd()
289 * Realease the locked pointer pTopPopupWnd.
291 void MENU_ReleaseTopPopupWnd()
293 WIN_ReleaseWndPtr(pTopPopupWnd);
295 /***********************************************************************
296 * MENU_DestroyTopPopupWnd()
298 * Destroy the locked pointer pTopPopupWnd.
300 void MENU_DestroyTopPopupWnd()
302 WND *tmpWnd = pTopPopupWnd;
304 WIN_ReleaseWndPtr(tmpWnd);
309 /**********************************************************************
312 * Create a copy of the system menu. System menu in Windows is
313 * a special menu-bar with the single entry - system menu popup.
314 * This popup is presented to the outside world as a "system menu".
315 * However, the real system menu handle is sometimes seen in the
316 * WM_MENUSELECT paramemters (and Word 6 likes it this way).
318 HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
322 if ((hMenu = CreateMenu()))
324 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
325 menu->wFlags = MF_SYSMENU;
328 if (hPopupMenu == (HMENU)(-1))
329 hPopupMenu = MENU_CopySysPopup();
330 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
334 InsertMenuA( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
336 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
337 menu->items[0].fState = 0;
338 menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hPopupMenu);
339 menu->wFlags |= MF_SYSMENU;
341 TRACE(menu,"GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
344 DestroyMenu( hMenu );
346 ERR(menu, "failed to load system menu!\n");
351 /***********************************************************************
354 * Menus initialisation.
359 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
364 /* Load menu bitmaps */
365 hStdCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECK));
366 hStdRadioCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_RADIOCHECK));
367 hStdMnArrow = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_MNARROW));
368 /* Load system buttons bitmaps */
369 hBmpMinimize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCE));
370 hBmpMinimizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCED));
371 hBmpMaximize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORE));
372 hBmpMaximizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORED));
373 hBmpClose = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSE));
374 hBmpCloseD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSED));
379 GetObjectA( hStdCheck, sizeof(bm), &bm );
380 check_bitmap_width = bm.bmWidth;
381 check_bitmap_height = bm.bmHeight;
385 /* Assume that radio checks have the same size as regular check. */
392 GetObjectA( hStdMnArrow, sizeof(bm), &bm );
393 arrow_bitmap_width = bm.bmWidth;
394 arrow_bitmap_height = bm.bmHeight;
398 if ((hBitmap = CreateBitmap( 8, 8, 1, 1, shade_bits)))
400 if((hShadeBrush = CreatePatternBrush( hBitmap )))
402 DeleteObject( hBitmap );
403 if ((MENU_DefSysPopup = MENU_CopySysPopup()))
411 /***********************************************************************
412 * MENU_InitSysMenuPopup
414 * Grey the appropriate items in System menu.
416 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
420 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
421 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
422 gray = ((style & WS_MAXIMIZE) != 0);
423 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
424 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
425 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
426 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
427 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
428 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
429 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
430 gray = (clsStyle & CS_NOCLOSE) != 0;
431 EnableMenuItem( hmenu, SC_CLOSE, (gray ? MF_GRAYED : MF_ENABLED) );
435 /******************************************************************************
437 * UINT32 MENU_GetStartOfNextColumn(
440 *****************************************************************************/
442 static UINT MENU_GetStartOfNextColumn(
445 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
446 UINT i = menu->FocusedItem + 1;
449 return NO_SELECTED_ITEM;
451 if( i == NO_SELECTED_ITEM )
454 for( ; i < menu->nItems; ++i ) {
455 if (menu->items[i].fType & MF_MENUBARBREAK)
459 return NO_SELECTED_ITEM;
463 /******************************************************************************
465 * UINT32 MENU_GetStartOfPrevColumn(
468 *****************************************************************************/
470 static UINT MENU_GetStartOfPrevColumn(
473 POPUPMENU const *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
477 return NO_SELECTED_ITEM;
479 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
480 return NO_SELECTED_ITEM;
482 /* Find the start of the column */
484 for(i = menu->FocusedItem; i != 0 &&
485 !(menu->items[i].fType & MF_MENUBARBREAK);
489 return NO_SELECTED_ITEM;
491 for(--i; i != 0; --i) {
492 if (menu->items[i].fType & MF_MENUBARBREAK)
496 TRACE(menu, "ret %d.\n", i );
503 /***********************************************************************
506 * Find a menu item. Return a pointer on the item, and modifies *hmenu
507 * in case the item was in a sub-menu.
509 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
514 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(*hmenu))) return NULL;
515 if (wFlags & MF_BYPOSITION)
517 if (*nPos >= menu->nItems) return NULL;
518 return &menu->items[*nPos];
522 MENUITEM *item = menu->items;
523 for (i = 0; i < menu->nItems; i++, item++)
525 if (item->wID == *nPos)
530 else if (item->fType & MF_POPUP)
532 HMENU hsubmenu = item->hSubMenu;
533 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
545 /***********************************************************************
548 static void MENU_FreeItemData( MENUITEM* item )
551 if (IS_STRING_ITEM(item->fType) && item->text)
552 HeapFree( SystemHeap, 0, item->text );
555 /***********************************************************************
556 * MENU_FindItemByCoords
558 * Find the item at the specified coordinates (screen coords). Does
559 * not work for child windows and therefore should not be called for
560 * an arbitrary system menu.
562 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu,
563 POINT pt, UINT *pos )
569 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
570 pt.x -= wrect.left;pt.y -= wrect.top;
572 for (i = 0; i < menu->nItems; i++, item++)
574 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
575 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
585 /***********************************************************************
588 * Find the menu item selected by a key press.
589 * Return item id, -1 if none, -2 if we should close the menu.
591 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
592 UINT key, BOOL forceMenuChar )
594 TRACE(menu,"\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
596 if (!IsMenu( hmenu ))
598 WND* w = WIN_FindWndPtr(hwndOwner);
599 hmenu = GetSubMenu(w->hSysMenu, 0);
600 WIN_ReleaseWndPtr(w);
605 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
606 MENUITEM *item = menu->items;
614 for (i = 0; i < menu->nItems; i++, item++)
616 if (item->text && (IS_STRING_ITEM(item->fType)))
618 char *p = item->text - 2;
621 p = strchr (p + 2, '&');
623 while (p != NULL && p [1] == '&');
624 if (p && (toupper(p[1]) == key)) return i;
628 menuchar = SendMessageA( hwndOwner, WM_MENUCHAR,
629 MAKEWPARAM( key, menu->wFlags ), hmenu );
630 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
631 if (HIWORD(menuchar) == 1) return (UINT)(-2);
635 /***********************************************************************
638 * Load the bitmap associated with the magic menu item and its style
641 static HBITMAP MENU_LoadMagicItem(UINT id,BOOL hilite)
643 // Magic menu item id's section
644 // These magic id's are used by windows to insert "standard" mdi
645 // buttons (minimize,restore,close) on menu. Under windows,
646 // these magic id's make sure the right things appear when those
647 // bitmap buttons are pressed/selected/released.
651 case MAGIC_REDUCE : return (hilite ? hBmpMinimizeD : hBmpMinimize);
652 case MAGIC_RESTORE : return (hilite ? hBmpMaximizeD: hBmpMaximize);
653 case MAGIC_CLOSE : return (hilite ? hBmpCloseD : hBmpClose);
659 /***********************************************************************
662 * Calculate the size of the menu item and store it in lpitem->rect.
664 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
665 INT orgX, INT orgY, BOOL menuBar )
670 TRACE(menu, "HDC 0x%x at (%d,%d)\n",
672 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
673 (menuBar ? " (MenuBar)" : ""));
675 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
677 if (lpitem->fType & MF_OWNERDRAW)
679 MEASUREITEMSTRUCT mis;
680 mis.CtlType = ODT_MENU;
682 mis.itemID = lpitem->wID;
683 mis.itemData = (DWORD)lpitem->dwItemData;
686 SendMessageA( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
687 lpitem->rect.bottom += mis.itemHeight;
688 lpitem->rect.right += mis.itemWidth;
689 TRACE(menu, "%08x %dx%d\n",
690 lpitem->wID, mis.itemWidth, mis.itemHeight);
694 if (lpitem->fType & MF_SEPARATOR)
696 lpitem->rect.bottom += SEPARATOR_HEIGHT;
702 lpitem->rect.right += 2 * check_bitmap_width;
703 if (lpitem->fType & MF_POPUP)
704 lpitem->rect.right += arrow_bitmap_width;
707 if (lpitem->fType & MF_BITMAP)
712 // Check if there is a magic menu item associated with this item
713 if((LOWORD((int)lpitem->text))<6)
715 resBmp = MENU_LoadMagicItem((int)lpitem->text,
716 (lpitem->fType & MF_HILITE));
719 resBmp = (HBITMAP)lpitem->text;
721 if (GetObjectA(resBmp, sizeof(bm), &bm ))
723 lpitem->rect.right += bm.bmWidth;
724 lpitem->rect.bottom += bm.bmHeight;
732 /* If we get here, then it must be a text item */
734 if (IS_STRING_ITEM( lpitem->fType ))
736 dwSize = GetTextExtent16( hdc, lpitem->text, strlen(lpitem->text) );
737 lpitem->rect.right += LOWORD(dwSize);
738 if (TWEAK_WineLook == WIN31_LOOK)
739 lpitem->rect.bottom += MAX( HIWORD(dwSize), SYSMETRICS_CYMENU );
741 lpitem->rect.bottom += MAX (HIWORD(dwSize), sysMetrics[SM_CYMENU]- 1);
744 if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
745 else if ((p = strchr( lpitem->text, '\t' )) != NULL)
747 /* Item contains a tab (only meaningful in popup menus) */
748 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE +
749 LOWORD( GetTextExtent16( hdc, lpitem->text,
750 (int)(p - lpitem->text) ));
751 lpitem->rect.right += MENU_TAB_SPACE;
755 if (strchr( lpitem->text, '\b' ))
756 lpitem->rect.right += MENU_TAB_SPACE;
757 lpitem->xTab = lpitem->rect.right - check_bitmap_width
758 - arrow_bitmap_width;
764 /***********************************************************************
765 * MENU_PopupMenuCalcSize
767 * Calculate the size of a popup menu.
769 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
774 int orgX, orgY, maxX, maxTab, maxTabWidth;
776 lppop->Width = lppop->Height = 0;
777 if (lppop->nItems == 0) return;
780 maxX = SYSMETRICS_CXBORDER;
781 while (start < lppop->nItems)
783 lpitem = &lppop->items[start];
785 orgY = SYSMETRICS_CYBORDER;
787 maxTab = maxTabWidth = 0;
789 /* Parse items until column break or end of menu */
790 for (i = start; i < lppop->nItems; i++, lpitem++)
793 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
795 if (TWEAK_WineLook > WIN31_LOOK)
798 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
800 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
801 maxX = MAX( maxX, lpitem->rect.right );
802 orgY = lpitem->rect.bottom;
803 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
805 maxTab = MAX( maxTab, lpitem->xTab );
806 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
810 /* Finish the column (set all items to the largest width found) */
811 maxX = MAX( maxX, maxTab + maxTabWidth );
812 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
814 lpitem->rect.right = maxX;
815 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
816 lpitem->xTab = maxTab;
818 lppop->Height = MAX( lppop->Height, orgY );
821 if(TWEAK_WineLook > WIN31_LOOK)
829 /***********************************************************************
830 * MENU_MenuBarCalcSize
832 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
833 * height is off by 1 pixel which causes lengthy window relocations when
834 * active document window is maximized/restored.
836 * Calculate the size of the menu bar.
838 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
839 LPPOPUPMENU lppop, HWND hwndOwner )
842 int start, i, orgX, orgY, maxY, helpPos;
844 if ((lprect == NULL) || (lppop == NULL)) return;
845 if (lppop->nItems == 0) return;
846 TRACE(menu,"left=%d top=%d right=%d bottom=%d\n",
847 lprect->left, lprect->top, lprect->right, lprect->bottom);
848 lppop->Width = lprect->right - lprect->left;
853 while (start < lppop->nItems)
855 lpitem = &lppop->items[start];
859 /* Parse items until line break or end of menu */
860 for (i = start; i < lppop->nItems; i++, lpitem++)
862 if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
864 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
866 TRACE(menu, "calling MENU_CalcItemSize org=(%d, %d)\n",
868 debug_print_menuitem (" item: ", lpitem, "");
869 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
871 if (lpitem->rect.right > lprect->right)
873 if (i != start) break;
874 else lpitem->rect.right = lprect->right;
876 maxY = MAX( maxY, lpitem->rect.bottom );
877 orgX = lpitem->rect.right;
880 /* Finish the line (set all items to the largest height found) */
881 while (start < i) lppop->items[start++].rect.bottom = maxY;
884 lprect->bottom = maxY;
885 lppop->Height = lprect->bottom - lprect->top;
887 /* Flush right all items between the MF_HELP and the last item */
888 /* (if several lines, only move the last line) */
891 lpitem = &lppop->items[lppop->nItems-1];
892 orgY = lpitem->rect.top;
893 orgX = lprect->right;
894 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
896 if (lpitem->rect.top != orgY) break; /* Other line */
897 if (lpitem->rect.right >= orgX) break; /* Too far right already */
898 lpitem->rect.left += orgX - lpitem->rect.right;
899 lpitem->rect.right = orgX;
900 orgX = lpitem->rect.left;
905 /***********************************************************************
908 * Draw a single menu item.
910 static void MENU_DrawMenuItem( HWND hwnd, HDC hdc, MENUITEM *lpitem,
911 UINT height, BOOL menuBar, UINT odaction )
915 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
917 if (lpitem->fType & MF_SYSMENU)
919 if( !IsIconic(hwnd) ) {
920 if (TWEAK_WineLook > WIN31_LOOK)
921 NC_DrawSysButton95( hwnd, hdc,
923 (MF_HILITE | MF_MOUSESELECT) );
925 NC_DrawSysButton( hwnd, hdc,
927 (MF_HILITE | MF_MOUSESELECT) );
933 if (lpitem->fType & MF_OWNERDRAW)
937 dis.CtlType = ODT_MENU;
939 dis.itemID = lpitem->wID;
940 dis.itemData = (DWORD)lpitem->dwItemData;
942 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
943 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED;
944 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
945 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
948 dis.rcItem = lpitem->rect;
949 TRACE(menu, "Ownerdraw: itemID=%d, itemState=%d, itemAction=%d, "
950 "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n",dis.itemID,
951 dis.itemState, dis.itemAction, dis.hwndItem, dis.hDC,
952 dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
954 SendMessageA( GetWindow(hwnd,GW_OWNER), WM_DRAWITEM, 0, (LPARAM)&dis );
958 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
961 /* Draw the background */
962 if (TWEAK_WineLook > WIN31_LOOK) {
972 InflateRect32( &rect, -1, -1 );
976 if ((lpitem->fState & MF_HILITE) && !(lpitem->fType & MF_BITMAP) )
977 FillRect( hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT) );
979 FillRect( hdc, &rect, GetSysColorBrush(COLOR_MENU) );
981 SetBkMode( hdc, TRANSPARENT );
983 /* Draw the separator bar (if any) */
985 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
987 /* vertical separator */
988 if (TWEAK_WineLook > WIN31_LOOK) {
991 rc.bottom = height - 3;
992 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
995 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
996 MoveTo16( hdc, rect.left, 0 );
997 LineTo( hdc, rect.left, height );
1000 if (lpitem->fType & MF_SEPARATOR)
1002 /* horizontal separator */
1003 if (TWEAK_WineLook > WIN31_LOOK) {
1007 rc.top += SEPARATOR_HEIGHT / 2;
1008 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1011 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1012 MoveTo16( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
1013 LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
1021 if ((lpitem->fState & MF_HILITE) && !(lpitem->fType & MF_BITMAP) )
1023 if (lpitem->fState & MF_GRAYED)
1024 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1026 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
1027 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
1031 if (lpitem->fState & MF_GRAYED)
1032 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1034 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1035 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
1040 INT y = rect.top + rect.bottom;
1042 /* Draw the check mark
1045 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1048 if (lpitem->fState & MF_CHECKED)
1051 lpitem->hCheckBit ? lpitem->hCheckBit :
1052 ((lpitem->fType & MFT_RADIOCHECK)
1053 ? hStdRadioCheck : hStdCheck);
1054 HDC hdcMem = CreateCompatibleDC( hdc );
1056 SelectObject( hdcMem, bm );
1057 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1058 check_bitmap_width, check_bitmap_height,
1059 hdcMem, 0, 0, SRCCOPY );
1061 } else if (lpitem->hUnCheckBit) {
1062 HDC hdcMem = CreateCompatibleDC( hdc );
1064 SelectObject( hdcMem, lpitem->hUnCheckBit );
1065 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1066 check_bitmap_width, check_bitmap_height,
1067 hdcMem, 0, 0, SRCCOPY );
1071 /* Draw the popup-menu arrow */
1073 if (lpitem->fType & MF_POPUP)
1075 HDC hdcMem = CreateCompatibleDC( hdc );
1077 SelectObject( hdcMem, hStdMnArrow );
1078 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1079 (y - arrow_bitmap_height) / 2,
1080 arrow_bitmap_width, arrow_bitmap_height,
1081 hdcMem, 0, 0, SRCCOPY );
1085 rect.left += check_bitmap_width;
1086 rect.right -= arrow_bitmap_width;
1089 /* Draw the item text or bitmap */
1091 if (lpitem->fType & MF_BITMAP)
1095 HDC hdcMem = CreateCompatibleDC( hdc );
1097 // Check if there is a magic menu item associated with this item
1098 // and load the appropriate bitmap
1099 if((LOWORD((int)lpitem->text)) < 6)
1101 resBmp = MENU_LoadMagicItem((int)lpitem->text,
1102 (lpitem->fState & MF_HILITE));
1105 resBmp = (HBITMAP)lpitem->text;
1107 SelectObject(hdcMem,resBmp );
1108 BitBlt( hdc, rect.left, rect.top+3, rect.right - rect.left+3,
1109 rect.bottom - rect.top, hdcMem, 0, 0, SRCCOPY );
1115 /* No bitmap - process text if present */
1116 else if (IS_STRING_ITEM(lpitem->fType))
1122 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1123 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1124 i = strlen( lpitem->text );
1128 for (i = 0; lpitem->text[i]; i++)
1129 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1133 if((TWEAK_WineLook == WIN31_LOOK) || !(lpitem->fState & MF_GRAYED)) {
1134 DrawTextA( hdc, lpitem->text, i, &rect,
1135 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1138 if (!(lpitem->fState & MF_HILITE))
1144 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1145 DrawTextA( hdc, lpitem->text, i, &rect,
1146 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1152 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1153 DrawTextA( hdc, lpitem->text, i, &rect,
1154 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1157 if (lpitem->text[i]) /* There's a tab or flush-right char */
1159 if (lpitem->text[i] == '\t')
1161 rect.left = lpitem->xTab;
1162 DrawTextA( hdc, lpitem->text + i + 1, -1, &rect,
1163 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1165 else DrawTextA( hdc, lpitem->text + i + 1, -1, &rect,
1166 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
1172 /***********************************************************************
1173 * MENU_DrawPopupMenu
1175 * Paint a popup menu.
1177 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1179 HBRUSH hPrevBrush = 0;
1182 GetClientRect( hwnd, &rect );
1184 if(TWEAK_WineLook == WIN31_LOOK)
1186 rect.bottom -= POPUP_YSHADE * SYSMETRICS_CYBORDER;
1187 rect.right -= POPUP_XSHADE * SYSMETRICS_CXBORDER;
1190 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) )))
1194 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1196 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1202 /* draw 3-d shade */
1203 if(TWEAK_WineLook == WIN31_LOOK) {
1204 SelectObject( hdc, hShadeBrush );
1205 SetBkMode( hdc, TRANSPARENT );
1206 ropPrev = SetROP2( hdc, R2_MASKPEN );
1208 i = rect.right; /* why SetBrushOrg() doesn't? */
1209 PatBlt( hdc, i & 0xfffffffe,
1210 rect.top + POPUP_YSHADE*SYSMETRICS_CYBORDER,
1211 i%2 + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1212 rect.bottom - rect.top, 0x00a000c9 );
1214 PatBlt( hdc, rect.left + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1215 i & 0xfffffffe,rect.right - rect.left,
1216 i%2 + POPUP_YSHADE*SYSMETRICS_CYBORDER, 0x00a000c9 );
1217 SelectObject( hdc, hPrevPen );
1218 SelectObject( hdc, hPrevBrush );
1219 SetROP2( hdc, ropPrev );
1222 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1224 /* draw menu items */
1226 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1227 if (menu && menu->nItems)
1232 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1233 MENU_DrawMenuItem( hwnd, hdc, item, menu->Height, FALSE,
1237 } else SelectObject( hdc, hPrevBrush );
1242 /***********************************************************************
1245 * Paint a menu bar. Returns the height of the menu bar.
1247 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1252 WND *wndPtr = WIN_FindWndPtr( hwnd );
1254 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR( (HMENU16)wndPtr->wIDmenu );
1255 if (lppop == NULL || lprect == NULL)
1257 retvalue = SYSMETRICS_CYMENU;
1260 TRACE(menu,"(%04x, %p, %p); !\n",
1261 hDC, lprect, lppop);
1262 if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1263 lprect->bottom = lprect->top + lppop->Height;
1266 retvalue = lppop->Height;
1270 FillRect(hDC, lprect, GetSysColorBrush(COLOR_MENU) );
1272 if (TWEAK_WineLook == WIN31_LOOK) {
1273 SelectObject( hDC, GetSysColorPen(COLOR_WINDOWFRAME) );
1274 MoveTo16( hDC, lprect->left, lprect->bottom );
1275 LineTo( hDC, lprect->right, lprect->bottom );
1278 SelectObject( hDC, GetSysColorPen(COLOR_3DFACE));
1279 MoveTo16( hDC, lprect->left, lprect->bottom );
1280 LineTo( hDC, lprect->right, lprect->bottom );
1283 if (lppop->nItems == 0)
1285 retvalue = SYSMETRICS_CYMENU;
1288 for (i = 0; i < lppop->nItems; i++)
1290 MENU_DrawMenuItem( hwnd, hDC, &lppop->items[i], lppop->Height, TRUE,
1293 retvalue = lppop->Height;
1295 WIN_ReleaseWndPtr(wndPtr);
1300 /***********************************************************************
1301 * MENU_PatchResidentPopup
1303 BOOL MENU_PatchResidentPopup( HQUEUE16 checkQueue, WND* checkWnd )
1305 WND *pTPWnd = MENU_GetTopPopupWnd();
1311 TRACE(menu,"patching resident popup: %04x %04x [%04x %04x]\n",
1312 checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTPWnd->hmemTaskQ,
1313 pTPWnd->owner ? pTPWnd->owner->hwndSelf : 0);
1315 switch( checkQueue )
1317 case 0: /* checkWnd is the new popup owner */
1320 pTPWnd->owner = checkWnd;
1321 if( pTPWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1322 hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1326 case 0xFFFF: /* checkWnd is destroyed */
1327 if( pTPWnd->owner == checkWnd )
1328 pTPWnd->owner = NULL;
1329 MENU_ReleaseTopPopupWnd();
1332 default: /* checkQueue is exiting */
1333 if( pTPWnd->hmemTaskQ == checkQueue )
1335 hTask = QUEUE_GetQueueTask( pTPWnd->hmemTaskQ );
1336 hTask = TASK_GetNextTask( hTask );
1343 TDB* task = (TDB*)GlobalLock16( hTask );
1346 pTPWnd->hInstance = task->hInstance;
1347 pTPWnd->hmemTaskQ = task->hQueue;
1348 MENU_ReleaseTopPopupWnd();
1351 else WARN(menu,"failed to patch resident popup.\n");
1354 MENU_ReleaseTopPopupWnd();
1358 /***********************************************************************
1361 * Display a popup menu.
1363 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1364 INT x, INT y, INT xanchor, INT yanchor )
1367 WND *wndOwner = NULL;
1369 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
1370 if (menu->FocusedItem != NO_SELECTED_ITEM)
1372 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1373 menu->FocusedItem = NO_SELECTED_ITEM;
1376 if( (wndOwner = WIN_FindWndPtr( hwndOwner )) )
1380 MENU_PopupMenuCalcSize( menu, hwndOwner );
1382 /* adjust popup menu pos so that it fits within the desktop */
1384 width = menu->Width + SYSMETRICS_CXBORDER;
1385 height = menu->Height + SYSMETRICS_CYBORDER;
1387 if( x + width > SYSMETRICS_CXSCREEN )
1390 x -= width - xanchor;
1391 if( x + width > SYSMETRICS_CXSCREEN)
1392 x = SYSMETRICS_CXSCREEN - width;
1396 if( y + height > SYSMETRICS_CYSCREEN )
1399 y -= height + yanchor;
1400 if( y + height > SYSMETRICS_CYSCREEN )
1401 y = SYSMETRICS_CYSCREEN - height;
1405 if( TWEAK_WineLook == WIN31_LOOK )
1407 width += POPUP_XSHADE * SYSMETRICS_CXBORDER; /* add space for shading */
1408 height += POPUP_YSHADE * SYSMETRICS_CYBORDER;
1411 /* NOTE: In Windows, top menu popup is not owned. */
1412 if (!pTopPopupWnd) /* create top level popup menu window */
1414 assert( uSubPWndLevel == 0 );
1416 pTopPopupWnd = WIN_FindWndPtr(CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1417 WS_POPUP, x, y, width, height,
1418 hwndOwner, 0, wndOwner->hInstance,
1422 WIN_ReleaseWndPtr(wndOwner);
1425 menu->hWnd = pTopPopupWnd->hwndSelf;
1426 MENU_ReleaseTopPopupWnd();
1431 /* create a new window for the submenu */
1433 menu->hWnd = CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1434 WS_POPUP, x, y, width, height,
1435 menu->hWnd, 0, wndOwner->hInstance,
1439 WIN_ReleaseWndPtr(wndOwner);
1443 else /* top level popup menu window already exists */
1445 WND *pTPWnd = MENU_GetTopPopupWnd();
1446 menu->hWnd = pTPWnd->hwndSelf;
1448 MENU_PatchResidentPopup( 0, wndOwner );
1449 SendMessage16( pTPWnd->hwndSelf, MM_SETMENUHANDLE, (WPARAM16)hmenu, 0L);
1451 /* adjust its size */
1453 SetWindowPos( menu->hWnd, 0, x, y, width, height,
1454 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1455 MENU_ReleaseTopPopupWnd();
1458 uSubPWndLevel++; /* menu level counter */
1460 /* Display the window */
1462 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1463 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1464 UpdateWindow( menu->hWnd );
1465 WIN_ReleaseWndPtr(wndOwner);
1472 /***********************************************************************
1475 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1476 BOOL sendMenuSelect )
1481 lppop = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1482 if (!lppop->nItems) return;
1484 if ((wIndex != NO_SELECTED_ITEM) &&
1485 (lppop->items[wIndex].fType & MF_SEPARATOR))
1486 wIndex = NO_SELECTED_ITEM;
1488 if (lppop->FocusedItem == wIndex) return;
1489 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1490 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1492 /* Clear previous highlighted item */
1493 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1495 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1496 MENU_DrawMenuItem(lppop->hWnd,hdc,&lppop->items[lppop->FocusedItem],
1497 lppop->Height, !(lppop->wFlags & MF_POPUP),
1501 /* Highlight new item (if any) */
1502 lppop->FocusedItem = wIndex;
1503 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1505 lppop->items[lppop->FocusedItem].fState |= MF_HILITE;
1506 MENU_DrawMenuItem( lppop->hWnd, hdc, &lppop->items[lppop->FocusedItem],
1507 lppop->Height, !(lppop->wFlags & MF_POPUP),
1511 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1512 SendMessage16( hwndOwner, WM_MENUSELECT, ip->wID,
1513 MAKELONG(ip->fType | (ip->fState | MF_MOUSESELECT),
1517 else if (sendMenuSelect) {
1518 SendMessage16( hwndOwner, WM_MENUSELECT, hmenu,
1519 MAKELONG( lppop->wFlags | MF_MOUSESELECT, hmenu ) );
1521 ReleaseDC( lppop->hWnd, hdc );
1525 /***********************************************************************
1526 * MENU_MoveSelection
1528 * Moves currently selected item according to the offset parameter.
1529 * If there is no selection then it should select the last item if
1530 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1532 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1537 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1538 if (!menu->items) return;
1540 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1542 if( menu->nItems == 1 ) return; else
1543 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1545 if (!(menu->items[i].fType & MF_SEPARATOR))
1547 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1552 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1553 i >= 0 && i < menu->nItems ; i += offset)
1554 if (!(menu->items[i].fType & MF_SEPARATOR))
1556 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1562 /**********************************************************************
1565 * Set an item flags, id and text ptr. Called by InsertMenu() and
1568 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT id,
1571 LPSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1573 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1575 if (IS_STRING_ITEM(flags))
1579 flags |= MF_SEPARATOR;
1585 /* Item beginning with a backspace is a help item */
1591 if (!(text = HEAP_strdupA( SystemHeap, 0, str ))) return FALSE;
1595 else if (flags & MF_BITMAP) item->text = (LPSTR)(HBITMAP)LOWORD(str);
1596 else item->text = NULL;
1598 if (flags & MF_OWNERDRAW)
1599 item->dwItemData = (DWORD)str;
1601 item->dwItemData = 0;
1603 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1604 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1606 if (flags & MF_POPUP)
1608 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)id);
1609 if (IS_A_MENU(menu)) menu->wFlags |= MF_POPUP;
1621 if (flags & MF_POPUP)
1622 item->hSubMenu = id;
1624 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1625 flags |= MF_POPUP; /* keep popup */
1627 item->fType = flags & TYPE_MASK;
1628 item->fState = (flags & STATE_MASK) &
1629 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1632 /* Don't call SetRectEmpty here! */
1635 if (prevText) HeapFree( SystemHeap, 0, prevText );
1637 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1642 /**********************************************************************
1645 * Insert a new item into a menu.
1647 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1652 if (!(menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu)))
1654 WARN(menu, "%04x not a menu handle\n",
1659 /* Find where to insert new item */
1661 if ((flags & MF_BYPOSITION) &&
1662 ((pos == (UINT)-1) || (pos == menu->nItems)))
1664 /* Special case: append to menu */
1665 /* Some programs specify the menu length to do that */
1670 if (!MENU_FindItem( &hMenu, &pos, flags ))
1672 WARN(menu, "item %x not found\n",
1676 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu)))
1678 WARN(menu,"%04x not a menu handle\n",
1684 /* Create new items array */
1686 newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1689 WARN(menu, "allocation failed\n" );
1692 if (menu->nItems > 0)
1694 /* Copy the old array into the new */
1695 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1696 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1697 (menu->nItems-pos)*sizeof(MENUITEM) );
1698 HeapFree( SystemHeap, 0, menu->items );
1700 menu->items = newItems;
1702 memset( &newItems[pos], 0, sizeof(*newItems) );
1703 return &newItems[pos];
1707 /**********************************************************************
1708 * MENU_ParseResource
1710 * Parse a standard menu resource and add items to the menu.
1711 * Return a pointer to the end of the resource.
1713 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1720 flags = GET_WORD(res);
1721 res += sizeof(WORD);
1722 if (!(flags & MF_POPUP))
1725 res += sizeof(WORD);
1727 if (!IS_STRING_ITEM(flags))
1728 ERR(menu, "not a string item %04x\n", flags );
1730 if (!unicode) res += strlen(str) + 1;
1731 else res += (lstrlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1732 if (flags & MF_POPUP)
1734 HMENU hSubMenu = CreatePopupMenu();
1735 if (!hSubMenu) return NULL;
1736 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1738 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1739 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1741 else /* Not a popup */
1743 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1744 else AppendMenuW( hMenu, flags, id,
1745 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1747 } while (!(flags & MF_END));
1752 /**********************************************************************
1753 * MENUEX_ParseResource
1755 * Parse an extended menu resource and add items to the menu.
1756 * Return a pointer to the end of the resource.
1758 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1764 mii.cbSize = sizeof(mii);
1765 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1766 mii.fType = GET_DWORD(res);
1767 res += sizeof(DWORD);
1768 mii.fState = GET_DWORD(res);
1769 res += sizeof(DWORD);
1770 mii.wID = GET_DWORD(res);
1771 res += sizeof(DWORD);
1772 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
1773 res += sizeof(WORD);
1774 /* Align the text on a word boundary. */
1775 res += (~((int)res - 1)) & 1;
1776 mii.dwTypeData = (LPWSTR) res;
1777 res += (1 + lstrlenW(mii.dwTypeData)) * sizeof(WCHAR);
1778 /* Align the following fields on a dword boundary. */
1779 res += (~((int)res - 1)) & 3;
1781 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
1783 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
1785 TRACE(menu, "Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1786 mii.fType, mii.fState, mii.wID, resinfo, newstr);
1787 HeapFree( GetProcessHeap(), 0, newstr );
1790 if (resinfo & 1) { /* Pop-up? */
1791 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
1792 res += sizeof(DWORD);
1793 mii.hSubMenu = CreatePopupMenu();
1796 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1797 DestroyMenu(mii.hSubMenu);
1800 mii.fMask |= MIIM_SUBMENU;
1801 mii.fType |= MF_POPUP;
1803 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
1804 } while (!(resinfo & MF_END));
1809 /***********************************************************************
1812 * Return the handle of the selected sub-popup menu (if any).
1814 static HMENU MENU_GetSubPopup( HMENU hmenu )
1819 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1821 if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
1823 item = &menu->items[menu->FocusedItem];
1824 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
1825 return item->hSubMenu;
1830 /***********************************************************************
1831 * MENU_HideSubPopups
1833 * Hide the sub-popup menus of this menu.
1835 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
1836 BOOL sendMenuSelect )
1838 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR( hmenu );;
1840 if (menu && uSubPWndLevel)
1846 if (menu->FocusedItem != NO_SELECTED_ITEM)
1848 item = &menu->items[menu->FocusedItem];
1849 if (!(item->fType & MF_POPUP) ||
1850 !(item->fState & MF_MOUSESELECT)) return;
1851 item->fState &= ~MF_MOUSESELECT;
1852 hsubmenu = item->hSubMenu;
1855 submenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hsubmenu );
1856 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
1857 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect );
1859 if (submenu->hWnd == MENU_GetTopPopupWnd()->hwndSelf )
1861 ShowWindow( submenu->hWnd, SW_HIDE );
1866 DestroyWindow( submenu->hWnd );
1869 MENU_ReleaseTopPopupWnd();
1874 /***********************************************************************
1877 * Display the sub-menu of the selected item of this menu.
1878 * Return the handle of the submenu, or hmenu if no submenu to display.
1880 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
1889 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return hmenu;
1891 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd )) ||
1892 (menu->FocusedItem == NO_SELECTED_ITEM))
1894 WIN_ReleaseWndPtr(wndPtr);
1898 item = &menu->items[menu->FocusedItem];
1899 if (!(item->fType & MF_POPUP) ||
1900 (item->fState & (MF_GRAYED | MF_DISABLED)))
1902 WIN_ReleaseWndPtr(wndPtr);
1906 /* message must be send before using item,
1907 because nearly everything may by changed by the application ! */
1909 SendMessage16( hwndOwner, WM_INITMENUPOPUP, (WPARAM16)item->hSubMenu,
1910 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
1912 item = &menu->items[menu->FocusedItem];
1915 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
1916 if (!(item->fState & MF_HILITE))
1918 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
1919 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1920 item->fState |= MF_HILITE;
1921 MENU_DrawMenuItem( menu->hWnd, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
1922 ReleaseDC( menu->hWnd, hdc );
1924 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
1927 item->fState |= MF_MOUSESELECT;
1929 if (IS_SYSTEM_MENU(menu))
1931 MENU_InitSysMenuPopup(item->hSubMenu, wndPtr->dwStyle, wndPtr->class->style);
1933 NC_GetSysPopupPos( wndPtr, &rect );
1934 rect.top = rect.bottom;
1935 rect.right = SYSMETRICS_CXSIZE;
1936 rect.bottom = SYSMETRICS_CYSIZE;
1940 if (menu->wFlags & MF_POPUP)
1942 rect.left = wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width;
1943 rect.top = wndPtr->rectWindow.top + item->rect.top;
1944 rect.right = item->rect.left - item->rect.right + 2*arrow_bitmap_width;
1945 rect.bottom = item->rect.top - item->rect.bottom;
1949 rect.left = wndPtr->rectWindow.left + item->rect.left;
1950 rect.top = wndPtr->rectWindow.top + item->rect.bottom;
1951 rect.right = item->rect.right - item->rect.left;
1952 rect.bottom = item->rect.bottom - item->rect.top;
1956 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
1957 rect.left, rect.top, rect.right, rect.bottom );
1959 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
1960 WIN_ReleaseWndPtr(wndPtr);
1961 return item->hSubMenu;
1964 /***********************************************************************
1967 * Walks menu chain trying to find a menu pt maps to.
1969 static HMENU MENU_PtMenu( HMENU hMenu, POINT16 pt )
1971 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
1972 register UINT ht = menu->FocusedItem;
1974 /* try subpopup first (if any) */
1975 ht = (ht != NO_SELECTED_ITEM &&
1976 (menu->items[ht].fType & MF_POPUP) &&
1977 (menu->items[ht].fState & MF_MOUSESELECT))
1978 ? (UINT) MENU_PtMenu(menu->items[ht].hSubMenu, pt) : 0;
1980 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
1982 ht = (UINT)NC_HandleNCHitTest( menu->hWnd, pt );
1983 if( menu->wFlags & MF_POPUP )
1984 ht = (ht != (UINT)HTNOWHERE &&
1985 ht != (UINT)HTERROR) ? (UINT)hMenu : 0;
1988 WND* wndPtr = WIN_FindWndPtr(menu->hWnd);
1990 ht = ( ht == HTSYSMENU ) ? (UINT)(wndPtr->hSysMenu)
1991 : ( ht == HTMENU ) ? (UINT)(wndPtr->wIDmenu) : 0;
1992 WIN_ReleaseWndPtr(wndPtr);
1997 /***********************************************************************
1998 * MENU_ExecFocusedItem
2000 * Execute a menu item (for instance when user pressed Enter).
2001 * Return the wID of the executed item. Otherwise, zero indicating
2002 * that no menu item wase executed;
2004 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu )
2007 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
2008 if (!menu || !menu->nItems ||
2009 (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2011 item = &menu->items[menu->FocusedItem];
2013 TRACE(menu, "%08x %08x %08x\n",
2014 hMenu, item->wID, item->hSubMenu);
2016 if (!(item->fType & MF_POPUP))
2018 if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
2020 if( menu->wFlags & MF_SYSMENU )
2022 PostMessage16( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2023 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2027 PostMessage16( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2035 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hMenu, TRUE );
2040 /***********************************************************************
2041 * MENU_SwitchTracking
2043 * Helper function for menu navigation routines.
2045 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2047 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2048 POPUPMENU *topmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2050 if( pmt->hTopMenu != hPtMenu &&
2051 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2053 /* both are top level menus (system and menu-bar) */
2055 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2056 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
2057 pmt->hTopMenu = hPtMenu;
2059 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2060 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE );
2064 /***********************************************************************
2067 * Return TRUE if we can go on with menu tracking.
2069 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu )
2074 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2077 if( IS_SYSTEM_MENU(ptmenu) )
2078 item = ptmenu->items;
2080 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2084 if( ptmenu->FocusedItem == id )
2086 /* nothing to do with already selected non-popup */
2087 if( !(item->fType & MF_POPUP) ) return TRUE;
2089 if( item->fState & MF_MOUSESELECT )
2091 if( ptmenu->wFlags & MF_POPUP )
2093 /* hide selected subpopup */
2095 MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, TRUE );
2096 pmt->hCurrentMenu = hPtMenu;
2099 return FALSE; /* shouldn't get here */
2102 else MENU_SwitchTracking( pmt, hPtMenu, id );
2104 /* try to display a subpopup */
2106 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
2109 else WARN(menu, "\tunable to find clicked item!\n");
2114 /***********************************************************************
2117 * Return the the value of MENU_ExecFocusedItem if
2118 * the selected item was not a popup
2119 * 1 if the item was a popup
2121 * A zero return value indicates that we can't go on with menu tracking.
2123 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu )
2128 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2131 if( IS_SYSTEM_MENU(ptmenu) )
2132 item = ptmenu->items;
2134 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2136 if( item && (ptmenu->FocusedItem == id ))
2138 if( !(item->fType & MF_POPUP) )
2139 return MENU_ExecFocusedItem( pmt, hPtMenu );
2140 hPtMenu = item->hSubMenu;
2141 if( hPtMenu == pmt->hCurrentMenu )
2143 /* Select first item of sub-popup */
2145 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, NO_SELECTED_ITEM, FALSE );
2146 MENU_MoveSelection( pmt->hOwnerWnd, hPtMenu, ITEM_NEXT );
2155 /***********************************************************************
2158 * Return TRUE if we can go on with menu tracking.
2160 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu )
2162 UINT id = NO_SELECTED_ITEM;
2163 POPUPMENU *ptmenu = NULL;
2167 ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2168 if( IS_SYSTEM_MENU(ptmenu) )
2171 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2174 if( id == NO_SELECTED_ITEM )
2176 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2177 NO_SELECTED_ITEM, TRUE );
2179 else if( ptmenu->FocusedItem != id )
2181 MENU_SwitchTracking( pmt, hPtMenu, id );
2182 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
2188 /***********************************************************************
2191 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2193 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2195 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2197 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2198 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2204 LRESULT l = SendMessage16( pmt->hOwnerWnd, WM_NEXTMENU, (WPARAM16)vk,
2205 (IS_SYSTEM_MENU(menu)) ? GetSubMenu16(pmt->hTopMenu,0) : pmt->hTopMenu );
2207 TRACE(menu,"%04x [%04x] -> %04x [%04x]\n",
2208 (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2212 wndPtr = WIN_FindWndPtr(pmt->hOwnerWnd);
2214 hNewWnd = pmt->hOwnerWnd;
2215 if( IS_SYSTEM_MENU(menu) )
2217 /* switch to the menu bar */
2219 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu )
2221 WIN_ReleaseWndPtr(wndPtr);
2225 hNewMenu = wndPtr->wIDmenu;
2228 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hNewMenu );
2229 id = menu->nItems - 1;
2232 else if( wndPtr->dwStyle & WS_SYSMENU )
2234 /* switch to the system menu */
2235 hNewMenu = wndPtr->hSysMenu;
2239 WIN_ReleaseWndPtr(wndPtr);
2242 WIN_ReleaseWndPtr(wndPtr);
2244 else /* application returned a new menu to switch to */
2246 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2248 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2250 wndPtr = WIN_FindWndPtr(hNewWnd);
2252 if( wndPtr->dwStyle & WS_SYSMENU &&
2253 GetSubMenu16(wndPtr->hSysMenu, 0) == hNewMenu )
2255 /* get the real system menu */
2256 hNewMenu = wndPtr->hSysMenu;
2258 else if( wndPtr->dwStyle & WS_CHILD || wndPtr->wIDmenu != hNewMenu )
2260 /* FIXME: Not sure what to do here, perhaps,
2261 * try to track hNewMenu as a popup? */
2263 TRACE(menu," -- got confused.\n");
2264 WIN_ReleaseWndPtr(wndPtr);
2267 WIN_ReleaseWndPtr(wndPtr);
2272 if( hNewMenu != pmt->hTopMenu )
2274 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
2275 if( pmt->hCurrentMenu != pmt->hTopMenu )
2276 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2279 if( hNewWnd != pmt->hOwnerWnd )
2282 pmt->hOwnerWnd = hNewWnd;
2283 EVENT_Capture( pmt->hOwnerWnd, HTMENU );
2286 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2287 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE );
2294 /***********************************************************************
2297 * The idea is not to show the popup if the next input message is
2298 * going to hide it anyway.
2300 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2304 msg.hwnd = pmt->hOwnerWnd;
2306 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2307 pmt->trackFlags |= TF_SKIPREMOVE;
2312 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2313 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2315 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2316 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2317 if( msg.message == WM_KEYDOWN &&
2318 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2320 pmt->trackFlags |= TF_SUSPENDPOPUP;
2327 /* failures go through this */
2328 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2332 /***********************************************************************
2335 * Handle a VK_LEFT key event in a menu.
2337 static void MENU_KeyLeft( MTRACKER* pmt )
2340 HMENU hmenutmp, hmenuprev;
2343 hmenuprev = hmenutmp = pmt->hTopMenu;
2344 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenutmp );
2346 /* Try to move 1 column left (if possible) */
2347 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2348 NO_SELECTED_ITEM ) {
2350 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2355 /* close topmost popup */
2356 while (hmenutmp != pmt->hCurrentMenu)
2358 hmenuprev = hmenutmp;
2359 hmenutmp = MENU_GetSubPopup( hmenuprev );
2362 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2363 pmt->hCurrentMenu = hmenuprev;
2365 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2367 /* move menu bar selection if no more popups are left */
2369 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2370 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2372 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2374 /* A sublevel menu was displayed - display the next one
2375 * unless there is another displacement coming up */
2377 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2378 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd,
2379 pmt->hTopMenu, TRUE );
2385 /***********************************************************************
2388 * Handle a VK_RIGHT key event in a menu.
2390 static void MENU_KeyRight( MTRACKER* pmt )
2393 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2396 TRACE(menu, "MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2398 ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2400 pmt->hTopMenu, menu->items[0].text );
2402 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2404 /* If already displaying a popup, try to display sub-popup */
2406 hmenutmp = pmt->hCurrentMenu;
2407 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hmenutmp, TRUE );
2409 /* if subpopup was displayed then we are done */
2410 if (hmenutmp != pmt->hCurrentMenu) return;
2413 /* Check to see if there's another column */
2414 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2415 NO_SELECTED_ITEM ) {
2416 TRACE(menu, "Going to %d.\n", nextcol );
2417 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2422 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2424 if( pmt->hCurrentMenu != pmt->hTopMenu )
2426 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2427 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2428 } else hmenutmp = 0;
2430 /* try to move to the next item */
2431 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2432 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2434 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2435 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2436 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd,
2437 pmt->hTopMenu, TRUE );
2441 /***********************************************************************
2444 * Menu tracking code.
2446 static INT MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
2447 HWND hwnd, const RECT *lprect )
2452 INT executedMenuId = 0;
2453 MTRACKER mt = { 0, hmenu, hmenu, hwnd, {x, y} }; /* control struct */
2456 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
2458 if (wFlags & TPM_BUTTONDOWN) MENU_ButtonDown( &mt, hmenu );
2460 EVENT_Capture( mt.hOwnerWnd, HTMENU );
2464 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2465 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2467 /* we have to keep the message in the queue until it's
2468 * clear that menu loop is not over yet. */
2470 if (!MSG_InternalGetMessage( &msg, msg.hwnd, mt.hOwnerWnd,
2471 MSGF_MENU, PM_NOREMOVE, TRUE )) break;
2473 TranslateMessage( &msg );
2477 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2479 /* Find a menu for this mouse event */
2481 CONV_POINT32TO16( &msg.pt, &pt16 );
2482 hmenu = MENU_PtMenu( mt.hTopMenu, pt16 );
2486 /* no WM_NC... messages in captured state */
2488 case WM_RBUTTONDBLCLK:
2489 case WM_RBUTTONDOWN:
2490 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2492 case WM_LBUTTONDBLCLK:
2493 case WM_LBUTTONDOWN:
2494 fEndMenu |= !MENU_ButtonDown( &mt, hmenu );
2498 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2501 /* Check if a menu was selected by the mouse */
2504 executedMenuId = MENU_ButtonUp( &mt, hmenu );
2506 /* the executedMenuId higher than one means that it contains
2507 the id of the selected item so we have to put the fEndMenu to TRUE.
2508 Otherwise, it contains a continue
2509 flag returned by MENU_ButtonUp indicating if we can continue with
2510 menu tracking or not*/
2511 fEndMenu = ((executedMenuId > 1) ? TRUE : FALSE);
2514 /* No menu was selected by the mouse */
2515 /* if the function was called by TrackPopupMenu, continue
2516 with the menu tracking. If not, stop it */
2518 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2523 /* In win95 winelook, the selected menu item must be changed every time the
2524 mouse moves. In Win31 winelook, the mouse button has to be held down */
2526 if ( (TWEAK_WineLook > WIN31_LOOK) ||
2527 ( (msg.wParam & MK_LBUTTON) ||
2528 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2530 fEndMenu |= !MENU_MouseMove( &mt, hmenu );
2532 } /* switch(msg.message) - mouse */
2534 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2536 fRemove = TRUE; /* Keyboard messages are always removed */
2544 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2545 NO_SELECTED_ITEM, FALSE );
2548 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2549 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2552 case VK_DOWN: /* If on menu bar, pull-down the menu */
2554 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2555 if (!(menu->wFlags & MF_POPUP))
2556 mt.hCurrentMenu = MENU_ShowSubPopup( mt.hOwnerWnd, mt.hTopMenu, TRUE );
2557 else /* otherwise try to move selection */
2558 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2562 MENU_KeyLeft( &mt );
2566 MENU_KeyRight( &mt );
2576 break; /* WM_KEYDOWN */
2586 break; /* WM_SYSKEYDOWN */
2592 if (msg.wParam == '\r' || msg.wParam == ' ')
2594 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu);
2595 fEndMenu = ((executedMenuId != 0) ? TRUE:FALSE);
2600 /* Hack to avoid control chars. */
2601 /* We will find a better way real soon... */
2602 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2604 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2605 LOWORD(msg.wParam), FALSE );
2606 if (pos == (UINT)-2) fEndMenu = TRUE;
2607 else if (pos == (UINT)-1) MessageBeep(0);
2610 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos, TRUE );
2611 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu);
2612 fEndMenu = ((executedMenuId != 0) ? TRUE:FALSE);
2616 } /* switch(msg.message) - kbd */
2620 DispatchMessageA( &msg );
2623 if (!fEndMenu) fRemove = TRUE;
2625 /* finally remove message from the queue */
2627 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2628 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
2629 else mt.trackFlags &= ~TF_SKIPREMOVE;
2633 if( IsWindow( mt.hOwnerWnd ) )
2635 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2637 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hTopMenu );
2638 if (menu && menu->wFlags & MF_POPUP)
2640 ShowWindow( menu->hWnd, SW_HIDE );
2643 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE );
2644 SendMessage16( mt.hOwnerWnd, WM_MENUSELECT, 0, MAKELONG( 0xffff, 0 ) );
2647 /* returning the id of the selected menu.
2648 The return value is only used by TrackPopupMenu */
2649 return executedMenuId;
2652 /***********************************************************************
2655 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu)
2658 SendMessage16( hWnd, WM_ENTERMENULOOP, 0, 0 );
2659 SendMessage16( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2660 SendMessage16( hWnd, WM_INITMENU, hMenu, 0 );
2664 /***********************************************************************
2665 * MENU_TrackMouseMenuBar
2667 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2669 void MENU_TrackMouseMenuBar( WND* wndPtr, INT ht, POINT pt )
2671 HWND hWnd = wndPtr->hwndSelf;
2672 HMENU hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
2676 MENU_InitTracking( hWnd, hMenu );
2677 MENU_TrackMenu( hMenu, TPM_ENTERIDLEEX | TPM_BUTTONDOWN |
2678 TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, hWnd, NULL );
2680 SendMessage16( hWnd, WM_EXITMENULOOP, 0, 0 );
2686 /***********************************************************************
2687 * MENU_TrackKbdMenuBar
2689 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
2691 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT wParam, INT vkey)
2693 UINT uItem = NO_SELECTED_ITEM;
2696 /* find window that has a menu */
2698 while( wndPtr->dwStyle & WS_CHILD && !(wndPtr->dwStyle & WS_SYSMENU) )
2699 if( !(wndPtr = wndPtr->parent) ) return;
2701 /* check if we have to track a system menu */
2703 if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) ||
2704 !wndPtr->wIDmenu || vkey == VK_SPACE )
2706 if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
2707 hTrackMenu = wndPtr->hSysMenu;
2709 wParam |= HTSYSMENU; /* prevent item lookup */
2712 hTrackMenu = wndPtr->wIDmenu;
2714 if (IsMenu( hTrackMenu ))
2716 MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu );
2718 if( vkey && vkey != VK_SPACE )
2720 uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu,
2721 vkey, (wParam & HTSYSMENU) );
2722 if( uItem >= (UINT)(-2) )
2724 if( uItem == (UINT)(-1) ) MessageBeep(0);
2731 MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE );
2733 if( uItem == NO_SELECTED_ITEM )
2734 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
2736 PostMessage16( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
2738 MENU_TrackMenu( hTrackMenu, TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON,
2739 0, 0, wndPtr->hwndSelf, NULL );
2741 SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
2747 /**********************************************************************
2748 * TrackPopupMenu16 (USER.416)
2750 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
2751 INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
2755 CONV_RECT16TO32( lpRect, &r );
2756 return TrackPopupMenu( hMenu, wFlags, x, y, nReserved, hWnd,
2757 lpRect ? &r : NULL );
2761 /**********************************************************************
2762 * TrackPopupMenu32 (USER32.549)
2764 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
2765 INT nReserved, HWND hWnd, const RECT *lpRect )
2770 SendMessage16( hWnd, WM_INITMENUPOPUP, (WPARAM16)hMenu, 0);
2771 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
2772 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
2777 /**********************************************************************
2778 * TrackPopupMenuEx (USER32.550)
2780 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
2781 HWND hWnd, LPTPMPARAMS lpTpm )
2783 FIXME(menu, "not fully implemented\n" );
2784 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
2785 lpTpm ? &lpTpm->rcExclude : NULL );
2788 /***********************************************************************
2791 * NOTE: Windows has totally different (and undocumented) popup wndproc.
2793 LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
2796 WND* wndPtr = WIN_FindWndPtr(hwnd);
2803 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
2804 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
2809 case WM_MOUSEACTIVATE: /* We don't want to be activated */
2810 retvalue = MA_NOACTIVATE;
2816 BeginPaint( hwnd, &ps );
2817 MENU_DrawPopupMenu( hwnd, ps.hdc,
2818 (HMENU)GetWindowLongA( hwnd, 0 ) );
2819 EndPaint( hwnd, &ps );
2829 /* zero out global pointer in case resident popup window
2830 * was somehow destroyed. */
2832 if(MENU_GetTopPopupWnd() )
2834 if( hwnd == pTopPopupWnd->hwndSelf )
2836 ERR(menu, "resident popup destroyed!\n");
2838 MENU_DestroyTopPopupWnd();
2843 MENU_ReleaseTopPopupWnd();
2851 if( !(*(HMENU*)wndPtr->wExtra) )
2852 ERR(menu,"no menu to display\n");
2855 *(HMENU*)wndPtr->wExtra = 0;
2858 case MM_SETMENUHANDLE:
2860 *(HMENU*)wndPtr->wExtra = (HMENU)wParam;
2863 case MM_GETMENUHANDLE:
2865 retvalue = *(HMENU*)wndPtr->wExtra;
2869 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
2874 WIN_ReleaseWndPtr(wndPtr);
2879 /***********************************************************************
2880 * MENU_GetMenuBarHeight
2882 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
2884 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
2885 INT orgX, INT orgY )
2893 TRACE(menu, "HWND 0x%x, width %d, "
2894 "at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
2896 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
2897 if (!(lppop = (LPPOPUPMENU)USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu)))
2899 WIN_ReleaseWndPtr(wndPtr);
2902 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
2903 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU);
2904 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
2905 ReleaseDC( hwnd, hdc );
2906 retvalue = lppop->Height;
2907 WIN_ReleaseWndPtr(wndPtr);
2912 /*******************************************************************
2913 * ChangeMenu16 (USER.153)
2915 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
2916 UINT16 id, UINT16 flags )
2918 TRACE(menu,"menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
2919 hMenu, pos, (DWORD)data, id, flags );
2920 if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
2923 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
2924 /* for MF_DELETE. We should check the parameters for all others */
2925 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
2927 if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
2928 if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
2930 if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
2931 flags & MF_BYPOSITION ? pos : id,
2932 flags & ~MF_REMOVE );
2933 /* Default: MF_INSERT */
2934 return InsertMenu16( hMenu, pos, flags, id, data );
2938 /*******************************************************************
2939 * ChangeMenu32A (USER32.23)
2941 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
2942 UINT id, UINT flags )
2944 TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2945 hMenu, pos, (DWORD)data, id, flags );
2946 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
2948 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
2949 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
2951 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
2952 flags & MF_BYPOSITION ? pos : id,
2953 flags & ~MF_REMOVE );
2954 /* Default: MF_INSERT */
2955 return InsertMenuA( hMenu, pos, flags, id, data );
2959 /*******************************************************************
2960 * ChangeMenu32W (USER32.24)
2962 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
2963 UINT id, UINT flags )
2965 TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2966 hMenu, pos, (DWORD)data, id, flags );
2967 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
2969 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
2970 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
2972 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
2973 flags & MF_BYPOSITION ? pos : id,
2974 flags & ~MF_REMOVE );
2975 /* Default: MF_INSERT */
2976 return InsertMenuW( hMenu, pos, flags, id, data );
2980 /*******************************************************************
2981 * CheckMenuItem16 (USER.154)
2983 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
2985 return (BOOL16)CheckMenuItem( hMenu, id, flags );
2989 /*******************************************************************
2990 * CheckMenuItem32 (USER32.46)
2992 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
2997 TRACE(menu,"%04x %04x %04x\n", hMenu, id, flags );
2998 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
2999 ret = item->fState & MF_CHECKED;
3000 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3001 else item->fState &= ~MF_CHECKED;
3006 /**********************************************************************
3007 * EnableMenuItem16 (USER.155)
3009 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3011 return EnableMenuItem( hMenu, wItemID, wFlags );
3015 /**********************************************************************
3016 * EnableMenuItem32 (USER32.170)
3018 UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3023 TRACE(menu,"(%04x, %04X, %04X) !\n",
3024 hMenu, wItemID, wFlags);
3026 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3029 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3030 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3035 /*******************************************************************
3036 * GetMenuString16 (USER.161)
3038 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
3039 LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
3041 return GetMenuStringA( hMenu, wItemID, str, nMaxSiz, wFlags );
3045 /*******************************************************************
3046 * GetMenuString32A (USER32.268)
3048 INT WINAPI GetMenuStringA( HMENU hMenu, UINT wItemID,
3049 LPSTR str, INT nMaxSiz, UINT wFlags )
3053 TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3054 hMenu, wItemID, str, nMaxSiz, wFlags );
3055 if (!str || !nMaxSiz) return 0;
3057 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3058 if (!IS_STRING_ITEM(item->fType)) return 0;
3059 lstrcpynA( str, item->text, nMaxSiz );
3060 TRACE(menu, "returning '%s'\n", str );
3065 /*******************************************************************
3066 * GetMenuString32W (USER32.269)
3068 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3069 LPWSTR str, INT nMaxSiz, UINT wFlags )
3073 TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3074 hMenu, wItemID, str, nMaxSiz, wFlags );
3075 if (!str || !nMaxSiz) return 0;
3077 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3078 if (!IS_STRING_ITEM(item->fType)) return 0;
3079 lstrcpynAtoW( str, item->text, nMaxSiz );
3080 return lstrlenW(str);
3084 /**********************************************************************
3085 * HiliteMenuItem16 (USER.162)
3087 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
3090 return HiliteMenuItem( hWnd, hMenu, wItemID, wHilite );
3094 /**********************************************************************
3095 * HiliteMenuItem32 (USER32.318)
3097 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3101 TRACE(menu,"(%04x, %04x, %04x, %04x);\n",
3102 hWnd, hMenu, wItemID, wHilite);
3103 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3104 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3105 if (menu->FocusedItem == wItemID) return TRUE;
3106 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3107 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE );
3112 /**********************************************************************
3113 * GetMenuState16 (USER.250)
3115 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3117 return GetMenuState( hMenu, wItemID, wFlags );
3121 /**********************************************************************
3122 * GetMenuState32 (USER32.267)
3124 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3127 TRACE(menu,"(%04x, %04x, %04x);\n",
3128 hMenu, wItemID, wFlags);
3129 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3130 debug_print_menuitem (" item: ", item, "");
3131 if (item->fType & MF_POPUP)
3133 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( item->hSubMenu );
3134 if (!menu) return -1;
3135 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3139 /* We used to (from way back then) mask the result to 0xff. */
3140 /* I don't know why and it seems wrong as the documented */
3141 /* return flag MF_SEPARATOR is outside that mask. */
3142 return (item->fType | item->fState);
3147 /**********************************************************************
3148 * GetMenuItemCount16 (USER.263)
3150 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
3152 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3153 if (!IS_A_MENU(menu)) return -1;
3154 TRACE(menu,"(%04x) returning %d\n",
3155 hMenu, menu->nItems );
3156 return menu->nItems;
3160 /**********************************************************************
3161 * GetMenuItemCount32 (USER32.262)
3163 INT WINAPI GetMenuItemCount( HMENU hMenu )
3165 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3166 if (!IS_A_MENU(menu)) return -1;
3167 TRACE(menu,"(%04x) returning %d\n",
3168 hMenu, menu->nItems );
3169 return menu->nItems;
3173 /**********************************************************************
3174 * GetMenuItemID16 (USER.264)
3176 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
3180 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
3181 if ((nPos < 0) || ((UINT16) nPos >= menu->nItems)) return -1;
3182 if (menu->items[nPos].fType & MF_POPUP) return -1;
3183 return menu->items[nPos].wID;
3187 /**********************************************************************
3188 * GetMenuItemID32 (USER32.263)
3190 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3194 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
3195 if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
3196 if (menu->items[nPos].fType & MF_POPUP) return -1;
3197 return menu->items[nPos].wID;
3201 /*******************************************************************
3202 * InsertMenu16 (USER.410)
3204 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3205 UINT16 id, SEGPTR data )
3207 UINT pos32 = (UINT)pos;
3208 if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT)-1;
3209 if (IS_STRING_ITEM(flags) && data)
3210 return InsertMenuA( hMenu, pos32, flags, id,
3211 (LPSTR)PTR_SEG_TO_LIN(data) );
3212 return InsertMenuA( hMenu, pos32, flags, id, (LPSTR)data );
3216 /*******************************************************************
3217 * InsertMenu32A (USER32.322)
3219 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3220 UINT id, LPCSTR str )
3224 if (IS_STRING_ITEM(flags) && str)
3225 TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3226 "id %04x, str '%s'\n",
3227 hMenu, pos, flags, id, str );
3228 else TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3229 "id %04x, str %08lx (not a string)\n",
3230 hMenu, pos, flags, id, (DWORD)str );
3232 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3234 if (!(MENU_SetItemData( item, flags, id, str )))
3236 RemoveMenu( hMenu, pos, flags );
3240 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3241 ((POPUPMENU *)USER_HEAP_LIN_ADDR((HMENU16)id))->wFlags |= MF_POPUP;
3243 item->hCheckBit = item->hUnCheckBit = 0;
3248 /*******************************************************************
3249 * InsertMenu32W (USER32.325)
3251 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3252 UINT id, LPCWSTR str )
3256 if (IS_STRING_ITEM(flags) && str)
3258 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3259 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3260 HeapFree( GetProcessHeap(), 0, newstr );
3263 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3267 /*******************************************************************
3268 * AppendMenu16 (USER.411)
3270 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3272 return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3276 /*******************************************************************
3277 * AppendMenu32A (USER32.5)
3279 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3280 UINT id, LPCSTR data )
3282 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3286 /*******************************************************************
3287 * AppendMenu32W (USER32.6)
3289 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3290 UINT id, LPCWSTR data )
3292 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3296 /**********************************************************************
3297 * RemoveMenu16 (USER.412)
3299 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3301 return RemoveMenu( hMenu, nPos, wFlags );
3305 /**********************************************************************
3306 * RemoveMenu32 (USER32.441)
3308 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3313 TRACE(menu,"(%04x, %04x, %04x)\n",hMenu, nPos, wFlags);
3314 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3315 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3319 MENU_FreeItemData( item );
3321 if (--menu->nItems == 0)
3323 HeapFree( SystemHeap, 0, menu->items );
3328 while(nPos < menu->nItems)
3334 menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3335 menu->nItems * sizeof(MENUITEM) );
3341 /**********************************************************************
3342 * DeleteMenu16 (USER.413)
3344 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3346 return DeleteMenu( hMenu, nPos, wFlags );
3350 /**********************************************************************
3351 * DeleteMenu32 (USER32.129)
3353 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3355 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3356 if (!item) return FALSE;
3357 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3358 /* nPos is now the position of the item */
3359 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3364 /*******************************************************************
3365 * ModifyMenu16 (USER.414)
3367 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3368 UINT16 id, SEGPTR data )
3370 if (IS_STRING_ITEM(flags))
3371 return ModifyMenuA( hMenu, pos, flags, id,
3372 (LPSTR)PTR_SEG_TO_LIN(data) );
3373 return ModifyMenuA( hMenu, pos, flags, id, (LPSTR)data );
3377 /*******************************************************************
3378 * ModifyMenu32A (USER32.397)
3380 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3381 UINT id, LPCSTR str )
3385 if (IS_STRING_ITEM(flags))
3387 TRACE(menu, "%04x %d %04x %04x '%s'\n",
3388 hMenu, pos, flags, id, str ? str : "#NULL#" );
3389 if (!str) return FALSE;
3393 TRACE(menu, "%04x %d %04x %04x %08lx\n",
3394 hMenu, pos, flags, id, (DWORD)str );
3397 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3398 return MENU_SetItemData( item, flags, id, str );
3402 /*******************************************************************
3403 * ModifyMenu32W (USER32.398)
3405 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3406 UINT id, LPCWSTR str )
3410 if (IS_STRING_ITEM(flags) && str)
3412 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3413 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3414 HeapFree( GetProcessHeap(), 0, newstr );
3417 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3421 /**********************************************************************
3422 * CreatePopupMenu16 (USER.415)
3424 HMENU16 WINAPI CreatePopupMenu16(void)
3426 return CreatePopupMenu();
3430 /**********************************************************************
3431 * CreatePopupMenu32 (USER32.82)
3433 HMENU WINAPI CreatePopupMenu(void)
3438 if (!(hmenu = CreateMenu())) return 0;
3439 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3440 menu->wFlags |= MF_POPUP;
3445 /**********************************************************************
3446 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3448 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3450 return MAKELONG( check_bitmap_width, check_bitmap_height );
3454 /**********************************************************************
3455 * SetMenuItemBitmaps16 (USER.418)
3457 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3458 HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3460 return SetMenuItemBitmaps( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3464 /**********************************************************************
3465 * SetMenuItemBitmaps32 (USER32.490)
3467 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
3468 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3471 TRACE(menu,"(%04x, %04x, %04x, %04x, %04x)\n",
3472 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3473 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3475 if (!hNewCheck && !hNewUnCheck)
3477 item->fState &= ~MF_USECHECKBITMAPS;
3479 else /* Install new bitmaps */
3481 item->hCheckBit = hNewCheck;
3482 item->hUnCheckBit = hNewUnCheck;
3483 item->fState |= MF_USECHECKBITMAPS;
3489 /**********************************************************************
3490 * CreateMenu16 (USER.151)
3492 HMENU16 WINAPI CreateMenu16(void)
3494 return CreateMenu();
3498 /**********************************************************************
3499 * CreateMenu32 (USER32.81)
3501 HMENU WINAPI CreateMenu(void)
3505 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3506 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3508 menu->wMagic = MENU_MAGIC;
3515 menu->FocusedItem = NO_SELECTED_ITEM;
3516 TRACE(menu, "return %04x\n", hMenu );
3521 /**********************************************************************
3522 * DestroyMenu16 (USER.152)
3524 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3526 return DestroyMenu( hMenu );
3530 /**********************************************************************
3531 * DestroyMenu32 (USER32.134)
3533 BOOL WINAPI DestroyMenu( HMENU hMenu )
3535 TRACE(menu,"(%04x)\n", hMenu);
3537 /* Silently ignore attempts to destroy default system popup */
3539 if (hMenu && hMenu != MENU_DefSysPopup)
3541 LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3542 WND *pTPWnd = MENU_GetTopPopupWnd();
3544 if( pTPWnd && (hMenu == *(HMENU*)pTPWnd->wExtra) )
3545 *(UINT*)pTPWnd->wExtra = 0;
3547 if (IS_A_MENU( lppop ))
3549 lppop->wMagic = 0; /* Mark it as destroyed */
3551 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3552 (!pTPWnd || (lppop->hWnd != pTPWnd->hwndSelf)))
3553 DestroyWindow( lppop->hWnd );
3555 if (lppop->items) /* recursively destroy submenus */
3558 MENUITEM *item = lppop->items;
3559 for (i = lppop->nItems; i > 0; i--, item++)
3561 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3562 MENU_FreeItemData( item );
3564 HeapFree( SystemHeap, 0, lppop->items );
3566 USER_HEAP_FREE( hMenu );
3567 MENU_ReleaseTopPopupWnd();
3571 MENU_ReleaseTopPopupWnd();
3575 return (hMenu != MENU_DefSysPopup);
3579 /**********************************************************************
3580 * GetSystemMenu16 (USER.156)
3582 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3584 return GetSystemMenu( hWnd, bRevert );
3588 /**********************************************************************
3589 * GetSystemMenu32 (USER32.291)
3591 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3593 WND *wndPtr = WIN_FindWndPtr( hWnd );
3597 if( wndPtr->hSysMenu )
3601 DestroyMenu(wndPtr->hSysMenu);
3602 wndPtr->hSysMenu = 0;
3606 POPUPMENU *menu = (POPUPMENU*)
3607 USER_HEAP_LIN_ADDR(wndPtr->hSysMenu);
3608 if( menu->items[0].hSubMenu == MENU_DefSysPopup )
3609 menu->items[0].hSubMenu = MENU_CopySysPopup();
3613 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3614 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
3616 if( wndPtr->hSysMenu )
3618 HMENU retvalue = GetSubMenu16(wndPtr->hSysMenu, 0);
3619 WIN_ReleaseWndPtr(wndPtr);
3622 WIN_ReleaseWndPtr(wndPtr);
3628 /*******************************************************************
3629 * SetSystemMenu16 (USER.280)
3631 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
3633 return SetSystemMenu( hwnd, hMenu );
3637 /*******************************************************************
3638 * SetSystemMenu32 (USER32.508)
3640 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
3642 WND *wndPtr = WIN_FindWndPtr(hwnd);
3646 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
3647 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3648 WIN_ReleaseWndPtr(wndPtr);
3655 /**********************************************************************
3656 * GetMenu16 (USER.157)
3658 HMENU16 WINAPI GetMenu16( HWND16 hWnd )
3661 WND * wndPtr = WIN_FindWndPtr(hWnd);
3662 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3664 retvalue = (HMENU16)wndPtr->wIDmenu;
3669 WIN_ReleaseWndPtr(wndPtr);
3674 /**********************************************************************
3675 * GetMenu32 (USER32.257)
3677 HMENU WINAPI GetMenu( HWND hWnd )
3680 WND * wndPtr = WIN_FindWndPtr(hWnd);
3681 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3683 retvalue = (HMENU)wndPtr->wIDmenu;
3688 WIN_ReleaseWndPtr(wndPtr);
3693 /**********************************************************************
3694 * SetMenu16 (USER.158)
3696 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
3698 return SetMenu( hWnd, hMenu );
3702 /**********************************************************************
3703 * SetMenu32 (USER32.487)
3705 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
3707 WND * wndPtr = WIN_FindWndPtr(hWnd);
3709 TRACE(menu,"(%04x, %04x);\n", hWnd, hMenu);
3711 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3713 if (GetCapture() == hWnd) ReleaseCapture();
3715 wndPtr->wIDmenu = (UINT)hMenu;
3720 if (!(lpmenu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu)))
3722 WIN_ReleaseWndPtr(wndPtr);
3725 lpmenu->hWnd = hWnd;
3726 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
3727 lpmenu->Height = 0; /* Make sure we recalculate the size */
3729 if (IsWindowVisible(hWnd))
3730 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3731 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3732 WIN_ReleaseWndPtr(wndPtr);
3735 WIN_ReleaseWndPtr(wndPtr);
3741 /**********************************************************************
3742 * GetSubMenu16 (USER.159)
3744 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
3746 return GetSubMenu( hMenu, nPos );
3750 /**********************************************************************
3751 * GetSubMenu32 (USER32.288)
3753 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
3757 if (!(lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return 0;
3758 if ((UINT)nPos >= lppop->nItems) return 0;
3759 if (!(lppop->items[nPos].fType & MF_POPUP)) return 0;
3760 return lppop->items[nPos].hSubMenu;
3764 /**********************************************************************
3765 * DrawMenuBar16 (USER.160)
3767 void WINAPI DrawMenuBar16( HWND16 hWnd )
3769 DrawMenuBar( hWnd );
3773 /**********************************************************************
3774 * DrawMenuBar32 (USER32.161)
3776 BOOL WINAPI DrawMenuBar( HWND hWnd )
3779 WND *wndPtr = WIN_FindWndPtr(hWnd);
3780 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
3782 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu);
3785 WIN_ReleaseWndPtr(wndPtr);
3789 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3790 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3791 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3792 WIN_ReleaseWndPtr(wndPtr);
3795 WIN_ReleaseWndPtr(wndPtr);
3800 /***********************************************************************
3801 * EndMenu (USER.187) (USER32.175)
3803 void WINAPI EndMenu(void)
3806 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
3813 /***********************************************************************
3814 * LookupMenuHandle (USER.217)
3816 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
3818 HMENU hmenu32 = hmenu;
3820 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3821 else return hmenu32;
3825 /**********************************************************************
3826 * LoadMenu16 (USER.150)
3828 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
3836 char *str = (char *)PTR_SEG_TO_LIN( name );
3837 TRACE(menu, "(%04x,'%s')\n", instance, str );
3838 if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
3841 TRACE(menu,"(%04x,%04x)\n",instance,LOWORD(name));
3843 if (!name) return 0;
3845 /* check for Win32 module */
3846 if (HIWORD(instance))
3847 return LoadMenuA(instance,PTR_SEG_TO_LIN(name));
3848 instance = GetExePtr( instance );
3850 if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
3851 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
3852 hMenu = LoadMenuIndirect16(LockResource16(handle));
3853 FreeResource16( handle );
3858 /*****************************************************************
3859 * LoadMenu32A (USER32.370)
3861 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
3863 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
3864 if (!hrsrc) return 0;
3865 return LoadMenuIndirectA( (LPCVOID)LoadResource( instance, hrsrc ));
3869 /*****************************************************************
3870 * LoadMenu32W (USER32.373)
3872 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
3874 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
3875 if (!hrsrc) return 0;
3876 return LoadMenuIndirectW( (LPCVOID)LoadResource( instance, hrsrc ));
3880 /**********************************************************************
3881 * LoadMenuIndirect16 (USER.220)
3883 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
3886 WORD version, offset;
3887 LPCSTR p = (LPCSTR)template;
3889 TRACE(menu,"(%p)\n", template );
3890 version = GET_WORD(p);
3894 WARN(menu, "version must be 0 for Win16\n" );
3897 offset = GET_WORD(p);
3898 p += sizeof(WORD) + offset;
3899 if (!(hMenu = CreateMenu())) return 0;
3900 if (!MENU_ParseResource( p, hMenu, FALSE ))
3902 DestroyMenu( hMenu );
3909 /**********************************************************************
3910 * LoadMenuIndirect32A (USER32.371)
3912 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
3915 WORD version, offset;
3916 LPCSTR p = (LPCSTR)template;
3918 TRACE(menu,"%p\n", template );
3919 version = GET_WORD(p);
3924 offset = GET_WORD(p);
3925 p += sizeof(WORD) + offset;
3926 if (!(hMenu = CreateMenu())) return 0;
3927 if (!MENU_ParseResource( p, hMenu, TRUE ))
3929 DestroyMenu( hMenu );
3934 offset = GET_WORD(p);
3935 p += sizeof(WORD) + offset;
3936 if (!(hMenu = CreateMenu())) return 0;
3937 if (!MENUEX_ParseResource( p, hMenu))
3939 DestroyMenu( hMenu );
3944 ERR(menu, "version %d not supported.\n", version);
3950 /**********************************************************************
3951 * LoadMenuIndirect32W (USER32.372)
3953 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
3955 /* FIXME: is there anything different between A and W? */
3956 return LoadMenuIndirectA( template );
3960 /**********************************************************************
3961 * IsMenu16 (USER.358)
3963 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
3965 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3966 return IS_A_MENU(menu);
3970 /**********************************************************************
3971 * IsMenu32 (USER32.346)
3973 BOOL WINAPI IsMenu(HMENU hmenu)
3975 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3976 return IS_A_MENU(menu);
3979 /**********************************************************************
3980 * GetMenuItemInfo32_common
3983 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item,
3985 LPMENUITEMINFOA lpmii,
3988 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
3989 debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
3993 if (lpmii->fMask & MIIM_TYPE) {
3994 lpmii->fType = menu->fType;
3995 switch (MENU_ITEM_TYPE(menu->fType)) {
3997 if (menu->text && lpmii->dwTypeData && lpmii->cch) {
3999 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData,
4003 lstrcpynA(lpmii->dwTypeData,
4010 lpmii->dwTypeData = menu->text;
4016 if (lpmii->fMask & MIIM_STATE)
4017 lpmii->fState = menu->fState;
4019 if (lpmii->fMask & MIIM_ID)
4020 lpmii->wID = menu->wID;
4022 if (lpmii->fMask & MIIM_SUBMENU)
4023 lpmii->hSubMenu = menu->hSubMenu;
4025 if (lpmii->fMask & MIIM_CHECKMARKS) {
4026 lpmii->hbmpChecked = menu->hCheckBit;
4027 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4029 if (lpmii->fMask & MIIM_DATA)
4030 lpmii->dwItemData = menu->dwItemData;
4035 /**********************************************************************
4036 * GetMenuItemInfo32A (USER32.264)
4038 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4039 LPMENUITEMINFOA lpmii)
4041 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4044 /**********************************************************************
4045 * GetMenuItemInfo32W (USER32.265)
4047 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4048 LPMENUITEMINFOW lpmii)
4050 return GetMenuItemInfo_common (hmenu, item, bypos,
4051 (LPMENUITEMINFOA)lpmii, TRUE);
4054 /**********************************************************************
4055 * SetMenuItemInfo32_common
4058 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4059 const MENUITEMINFOA *lpmii,
4062 if (!menu) return FALSE;
4064 if (lpmii->fMask & MIIM_TYPE) {
4065 /* Get rid of old string. */
4066 if (IS_STRING_ITEM(menu->fType) && menu->text)
4067 HeapFree(SystemHeap, 0, menu->text);
4069 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4070 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4071 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4073 menu->text = lpmii->dwTypeData;
4074 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4077 ? HEAP_strdupWtoA(SystemHeap, 0,
4078 (LPWSTR) lpmii->dwTypeData)
4079 : HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
4082 if (lpmii->fMask & MIIM_STATE)
4083 menu->fState = lpmii->fState;
4085 if (lpmii->fMask & MIIM_ID)
4086 menu->wID = lpmii->wID;
4088 if (lpmii->fMask & MIIM_SUBMENU) {
4089 menu->hSubMenu = lpmii->hSubMenu;
4090 if (menu->hSubMenu) {
4091 POPUPMENU *subMenu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)menu->hSubMenu);
4092 if (IS_A_MENU(subMenu)) {
4093 subMenu->wFlags |= MF_POPUP;
4094 menu->fType |= MF_POPUP;
4097 /* FIXME: Return an error ? */
4098 menu->fType &= ~MF_POPUP;
4101 menu->fType &= ~MF_POPUP;
4104 if (lpmii->fMask & MIIM_CHECKMARKS)
4106 menu->hCheckBit = lpmii->hbmpChecked;
4107 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4109 if (lpmii->fMask & MIIM_DATA)
4110 menu->dwItemData = lpmii->dwItemData;
4112 debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4116 /**********************************************************************
4117 * SetMenuItemInfo32A (USER32.491)
4119 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4120 const MENUITEMINFOA *lpmii)
4122 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4126 /**********************************************************************
4127 * SetMenuItemInfo32W (USER32.492)
4129 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4130 const MENUITEMINFOW *lpmii)
4132 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4133 (const MENUITEMINFOA*)lpmii, TRUE);
4136 /**********************************************************************
4137 * SetMenuDefaultItem32 (USER32.489)
4139 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT item, UINT bypos)
4141 MENUITEM *menuitem = MENU_FindItem(&hmenu, &item, bypos);
4144 if (!menuitem) return FALSE;
4145 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu))) return FALSE;
4147 menu->defitem = item; /* position */
4149 debug_print_menuitem("SetMenuDefaultItem32: ", menuitem, "");
4150 FIXME(menu, "(0x%x,%d,%d), empty stub!\n",
4151 hmenu, item, bypos);
4155 /**********************************************************************
4156 * GetMenuDefaultItem32 (USER32.260)
4158 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4162 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu)))
4165 FIXME(menu, "(0x%x,%d,%d), stub!\n", hmenu, bypos, flags);
4166 if (bypos & MF_BYPOSITION)
4167 return menu->defitem;
4169 FIXME (menu, "default item 0x%x\n", menu->defitem);
4170 if ((menu->defitem > 0) && (menu->defitem < menu->nItems))
4171 return menu->items[menu->defitem].wID;
4176 /*******************************************************************
4177 * InsertMenuItem16 (USER.441)
4181 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
4182 const MENUITEMINFO16 *mii )
4186 miia.cbSize = sizeof(miia);
4187 miia.fMask = mii->fMask;
4188 miia.dwTypeData = mii->dwTypeData;
4189 miia.fType = mii->fType;
4190 miia.fState = mii->fState;
4191 miia.wID = mii->wID;
4192 miia.hSubMenu = mii->hSubMenu;
4193 miia.hbmpChecked = mii->hbmpChecked;
4194 miia.hbmpUnchecked = mii->hbmpUnchecked;
4195 miia.dwItemData = mii->dwItemData;
4196 miia.cch = mii->cch;
4197 if (IS_STRING_ITEM(miia.fType))
4198 miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
4199 return InsertMenuItemA( hmenu, pos, byposition, &miia );
4203 /**********************************************************************
4204 * InsertMenuItem32A (USER32.323)
4206 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4207 const MENUITEMINFOA *lpmii)
4209 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4210 return SetMenuItemInfo_common(item, lpmii, FALSE);
4214 /**********************************************************************
4215 * InsertMenuItem32W (USER32.324)
4217 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4218 const MENUITEMINFOW *lpmii)
4220 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4221 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4224 /**********************************************************************
4225 * CheckMenuRadioItem32 (USER32.47)
4228 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4229 UINT first, UINT last, UINT check,
4232 MENUITEM *mifirst, *milast, *micheck;
4233 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4235 TRACE(menu, "ox%x: %d-%d, check %d, bypos=%d\n",
4236 hMenu, first, last, check, bypos);
4238 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4239 milast = MENU_FindItem (&mlast, &last, bypos);
4240 micheck = MENU_FindItem (&mcheck, &check, bypos);
4242 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4243 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4244 micheck > milast || micheck < mifirst)
4247 while (mifirst <= milast)
4249 if (mifirst == micheck)
4251 mifirst->fType |= MFT_RADIOCHECK;
4252 mifirst->fState |= MFS_CHECKED;
4254 mifirst->fType &= ~MFT_RADIOCHECK;
4255 mifirst->fState &= ~MFS_CHECKED;
4263 /**********************************************************************
4264 * CheckMenuRadioItem16 (not a Windows API)
4267 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4268 UINT16 first, UINT16 last, UINT16 check,
4271 return CheckMenuRadioItem (hMenu, first, last, check, bypos);
4274 /**********************************************************************
4275 * GetMenuItemRect32 (USER32.266)
4277 * ATTENTION: Here, the returned values in rect are the screen
4278 * coordinates of the item just like if the menu was
4279 * always on the upper left side of the application.
4282 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4285 POPUPMENU *itemMenu;
4289 TRACE(menu, "(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4291 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4292 referenceHwnd = hwnd;
4296 itemMenu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
4297 if (itemMenu == NULL)
4300 if(itemMenu->hWnd == 0)
4302 referenceHwnd = itemMenu->hWnd;
4305 if ((rect == NULL) || (item == NULL))
4310 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4315 /**********************************************************************
4316 * GetMenuItemRect16 (USER.665)
4319 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4325 if (!rect) return FALSE;
4326 res = GetMenuItemRect (hwnd, hMenu, uItem, &r32);
4327 CONV_RECT32TO16 (&r32, rect);
4331 /**********************************************************************
4332 * SetMenuContextHelpId16 (USER.384)
4334 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpId)
4336 return SetMenuContextHelpId( hMenu, dwContextHelpId );
4340 /**********************************************************************
4341 * SetMenuContextHelpId32 (USER32.488)
4343 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpId)
4345 FIXME(menu, "SetMenuContextHelpId, stub\n");
4349 /**********************************************************************
4350 * GetMenuContextHelpId16 (USER.385)
4352 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4354 return GetMenuContextHelpId16( hMenu );
4357 /**********************************************************************
4358 * GetMenuContextHelpId32 (USER32.488)
4360 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4362 FIXME(menu, "GetMenuContextHelpId, stub\n");