4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
9 * Note: the style MF_MOUSESELECT is used to mark popup items that
10 * have been selected, i.e. their popup menu is currently displayed.
11 * This is probably not the meaning this style has in MS-Windows.
20 #include "sysmetrics.h"
35 /* Menu item structure */
38 WORD item_flags; /* Item flags */
39 UINT item_id; /* Item or popup id */
40 RECT16 rect; /* Item area (relative to menu window) */
41 WORD xTab; /* X position of text after Tab */
42 HBITMAP hCheckBit; /* Bitmap for checked item */
43 HBITMAP hUnCheckBit; /* Bitmap for unchecked item */
44 LPSTR text; /* Item text or bitmap handle */
47 /* Popup menu structure */
50 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
51 WORD wMagic; /* Magic number */
52 HANDLE hTaskQ; /* Task queue for this menu */
53 WORD Width; /* Width of the whole menu */
54 WORD Height; /* Height of the whole menu */
55 WORD nItems; /* Number of items in the menu */
56 HWND hWnd; /* Window containing the menu */
57 MENUITEM *items; /* Array of menu items */
58 UINT FocusedItem; /* Currently focused item */
59 } POPUPMENU, *LPPOPUPMENU;
61 #define MENU_MAGIC 0x554d /* 'MU' */
63 /* Dimension of the menu bitmaps */
64 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
65 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
67 /* Flag set by EndMenu() to force an exit from menu tracking */
68 static BOOL fEndMenuCalled = FALSE;
70 /* Space between 2 menu bar items */
71 #define MENU_BAR_ITEMS_SPACE 16
73 /* Minimum width of a tab character */
74 #define MENU_TAB_SPACE 8
76 /* Height of a separator item */
77 #define SEPARATOR_HEIGHT 5
79 /* Values for menu->FocusedItem */
80 /* (other values give the position of the focused item) */
81 #define NO_SELECTED_ITEM 0xffff
82 #define SYSMENU_SELECTED 0xfffe /* Only valid on menu-bars */
84 #define IS_STRING_ITEM(flags) (!((flags) & (MF_BITMAP | MF_OWNERDRAW | \
85 MF_MENUBARBREAK | MF_MENUBREAK | MF_SEPARATOR)))
87 extern void NC_DrawSysButton(HWND hwnd, HDC hdc, BOOL down); /* nonclient.c */
89 static HBITMAP hStdCheck = 0;
90 static HBITMAP hStdMnArrow = 0;
91 static HMENU MENU_DefSysMenu = 0; /* Default system menu */
94 /* we _can_ use global popup window because there's no way 2 menues can
95 * be tracked at the same time.
98 static WND* pTopPWnd = 0;
99 static UINT uSubPWndLevel = 0;
102 /**********************************************************************
105 * Load a copy of the system menu.
107 static HMENU MENU_CopySysMenu(void)
113 if (!(handle = SYSRES_LoadResource( SYSRES_MENU_SYSMENU ))) return 0;
114 hMenu = LoadMenuIndirect16( GlobalLock16( handle ) );
115 SYSRES_FreeResource( handle );
118 dprintf_menu(stddeb,"No SYSMENU\n");
121 menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
122 menu->wFlags |= MF_SYSMENU | MF_POPUP;
123 dprintf_menu(stddeb,"CopySysMenu hMenu=%04x !\n", hMenu);
128 /***********************************************************************
131 * Menus initialisation.
139 if (!(hStdCheck = LoadBitmap( 0, MAKEINTRESOURCE(OBM_CHECK) )))
141 GetObject( hStdCheck, sizeof(BITMAP), (LPSTR)&bm );
142 check_bitmap_width = bm.bmWidth;
143 check_bitmap_height = bm.bmHeight;
144 if (!(hStdMnArrow = LoadBitmap( 0, MAKEINTRESOURCE(OBM_MNARROW) )))
146 GetObject( hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm );
147 arrow_bitmap_width = bm.bmWidth;
148 arrow_bitmap_height = bm.bmHeight;
150 if (!(MENU_DefSysMenu = MENU_CopySysMenu()))
152 fprintf( stderr, "Unable to create default system menu\n" );
159 /***********************************************************************
162 * Return the default system menu.
164 HMENU MENU_GetDefSysMenu(void)
166 return MENU_DefSysMenu;
170 /***********************************************************************
173 * Check whether the window owning the menu bar has a system menu.
175 static BOOL MENU_HasSysMenu( POPUPMENU *menu )
179 if (menu->wFlags & MF_POPUP) return FALSE;
180 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
181 return (wndPtr->dwStyle & WS_SYSMENU) != 0;
185 /***********************************************************************
188 * Check whether the point (in screen coords) is in the system menu
189 * of the window owning the given menu.
191 static BOOL MENU_IsInSysMenu( POPUPMENU *menu, POINT16 pt )
195 if (menu->wFlags & MF_POPUP) return FALSE;
196 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
197 if (!(wndPtr->dwStyle & WS_SYSMENU)) return FALSE;
198 if ((pt.x < wndPtr->rectClient.left) ||
199 (pt.x >= wndPtr->rectClient.left+SYSMETRICS_CXSIZE+SYSMETRICS_CXBORDER))
201 if ((pt.y >= wndPtr->rectClient.top - menu->Height) ||
202 (pt.y < wndPtr->rectClient.top - menu->Height -
203 SYSMETRICS_CYSIZE - SYSMETRICS_CYBORDER)) return FALSE;
208 /***********************************************************************
211 * Find a menu item. Return a pointer on the item, and modifies *hmenu
212 * in case the item was in a sub-menu.
214 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
219 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(*hmenu))) return NULL;
220 if (wFlags & MF_BYPOSITION)
222 if (*nPos >= menu->nItems) return NULL;
223 return &menu->items[*nPos];
227 MENUITEM *item = menu->items;
228 for (i = 0; i < menu->nItems; i++, item++)
230 if (item->item_id == *nPos)
235 else if (item->item_flags & MF_POPUP)
237 HMENU hsubmenu = (HMENU)item->item_id;
238 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
251 /***********************************************************************
252 * MENU_FindItemByCoords
254 * Find the item at the specified coordinates (screen coords).
256 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu, int x, int y, UINT *pos )
262 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return NULL;
263 x -= wndPtr->rectWindow.left;
264 y -= wndPtr->rectWindow.top;
266 for (i = 0; i < menu->nItems; i++, item++)
268 if ((x >= item->rect.left) && (x < item->rect.right) &&
269 (y >= item->rect.top) && (y < item->rect.bottom))
279 /***********************************************************************
282 * Find the menu item selected by a key press.
283 * Return item id, -1 if none, -2 if we should close the menu.
285 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu, UINT key )
292 if (!IsMenu( hmenu )) hmenu = WIN_FindWndPtr(hwndOwner)->hSysMenu;
293 if (!hmenu) return -1;
295 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
298 for (i = 0; i < menu->nItems; i++, item++)
300 if (IS_STRING_ITEM(item->item_flags))
302 char *p = strchr( item->text, '&' );
303 if (p && (p[1] != '&') && (toupper(p[1]) == key)) return i;
307 menuchar = SendMessage32A( hwndOwner, WM_MENUCHAR,
308 MAKEWPARAM(key,menu->wFlags), hmenu );
310 menuchar = SendMessage16( hwndOwner, WM_MENUCHAR, key,
311 MAKELONG( menu->wFlags, hmenu ) );
313 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
314 if (HIWORD(menuchar) == 1) return -2;
319 /***********************************************************************
322 * Calculate the size of the menu item and store it in lpitem->rect.
324 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
325 int orgX, int orgY, BOOL menuBar )
330 SetRect16( &lpitem->rect, orgX, orgY, orgX, orgY );
332 if (lpitem->item_flags & MF_OWNERDRAW)
334 MEASUREITEMSTRUCT *mis;
335 if (!(mis = SEGPTR_NEW(MEASUREITEMSTRUCT))) return;
336 mis->CtlType = ODT_MENU;
337 mis->itemID = lpitem->item_id;
338 mis->itemData = (DWORD)lpitem->text;
339 mis->itemHeight = 16;
341 SendMessage16( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)SEGPTR_GET(mis) );
342 lpitem->rect.bottom += mis->itemHeight;
343 lpitem->rect.right += mis->itemWidth;
344 dprintf_menu( stddeb, "DrawMenuItem: MeasureItem %04x %dx%d!\n",
345 lpitem->item_id, mis->itemWidth, mis->itemHeight );
350 if (lpitem->item_flags & MF_SEPARATOR)
352 lpitem->rect.bottom += SEPARATOR_HEIGHT;
358 lpitem->rect.right += 2 * check_bitmap_width;
359 if (lpitem->item_flags & MF_POPUP)
360 lpitem->rect.right += arrow_bitmap_width;
363 if (lpitem->item_flags & MF_BITMAP)
366 if (GetObject( (HBITMAP16)(UINT32)lpitem->text,
367 sizeof(BITMAP), (LPSTR)&bm ))
369 lpitem->rect.right += bm.bmWidth;
370 lpitem->rect.bottom += bm.bmHeight;
375 /* If we get here, then it must be a text item */
377 if (IS_STRING_ITEM( lpitem->item_flags ))
379 dwSize = GetTextExtent( hdc, lpitem->text, strlen(lpitem->text) );
380 lpitem->rect.right += LOWORD(dwSize);
381 lpitem->rect.bottom += MAX( HIWORD(dwSize), SYSMETRICS_CYMENU );
384 if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
385 else if ((p = strchr( lpitem->text, '\t' )) != NULL)
387 /* Item contains a tab (only meaningful in popup menus) */
388 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE +
389 LOWORD( GetTextExtent( hdc, lpitem->text,
390 (int)(p - lpitem->text) ));
391 lpitem->rect.right += MENU_TAB_SPACE;
395 if (strchr( lpitem->text, '\b' ))
396 lpitem->rect.right += MENU_TAB_SPACE;
397 lpitem->xTab = lpitem->rect.right - check_bitmap_width
398 - arrow_bitmap_width;
404 /***********************************************************************
405 * MENU_PopupMenuCalcSize
407 * Calculate the size of a popup menu.
409 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
414 int orgX, orgY, maxX, maxTab, maxTabWidth;
416 lppop->Width = lppop->Height = 0;
417 if (lppop->nItems == 0) return;
420 while (start < lppop->nItems)
422 lpitem = &lppop->items[start];
425 maxTab = maxTabWidth = 0;
427 /* Parse items until column break or end of menu */
428 for (i = start; i < lppop->nItems; i++, lpitem++)
431 (lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
432 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
433 if (lpitem->item_flags & MF_MENUBARBREAK) orgX++;
434 maxX = MAX( maxX, lpitem->rect.right );
435 orgY = lpitem->rect.bottom;
436 if (IS_STRING_ITEM(lpitem->item_flags) && lpitem->xTab)
438 maxTab = MAX( maxTab, lpitem->xTab );
439 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
443 /* Finish the column (set all items to the largest width found) */
444 maxX = MAX( maxX, maxTab + maxTabWidth );
445 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
447 lpitem->rect.right = maxX;
448 if (IS_STRING_ITEM(lpitem->item_flags) && lpitem->xTab)
449 lpitem->xTab = maxTab;
451 lppop->Height = MAX( lppop->Height, orgY );
459 /***********************************************************************
460 * MENU_MenuBarCalcSize
462 * Calculate the size of the menu bar.
464 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT16 lprect, LPPOPUPMENU lppop,
468 int start, i, orgX, orgY, maxY, helpPos;
470 if ((lprect == NULL) || (lppop == NULL)) return;
471 if (lppop->nItems == 0) return;
472 dprintf_menu(stddeb,"MENU_MenuBarCalcSize left=%d top=%d right=%d bottom=%d\n",
473 lprect->left, lprect->top, lprect->right, lprect->bottom);
474 lppop->Width = lprect->right - lprect->left;
479 while (start < lppop->nItems)
481 lpitem = &lppop->items[start];
485 /* Parse items until line break or end of menu */
486 for (i = start; i < lppop->nItems; i++, lpitem++)
488 if ((helpPos == -1) && (lpitem->item_flags & MF_HELP)) helpPos = i;
490 (lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
491 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
492 if (lpitem->rect.right > lprect->right)
494 if (i != start) break;
495 else lpitem->rect.right = lprect->right;
497 maxY = MAX( maxY, lpitem->rect.bottom );
498 orgX = lpitem->rect.right;
501 /* Finish the line (set all items to the largest height found) */
502 while (start < i) lppop->items[start++].rect.bottom = maxY;
505 lprect->bottom = maxY;
506 lppop->Height = lprect->bottom - lprect->top;
508 /* Flush right all items between the MF_HELP and the last item */
509 /* (if several lines, only move the last line) */
512 lpitem = &lppop->items[lppop->nItems-1];
513 orgY = lpitem->rect.top;
514 orgX = lprect->right;
515 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
517 if (lpitem->rect.top != orgY) break; /* Other line */
518 if (lpitem->rect.right >= orgX) break; /* Too far right already */
519 lpitem->rect.left += orgX - lpitem->rect.right;
520 lpitem->rect.right = orgX;
521 orgX = lpitem->rect.left;
527 /***********************************************************************
530 * Draw a single menu item.
532 static void MENU_DrawMenuItem( HWND hwnd, HDC hdc, MENUITEM *lpitem,
533 UINT height, BOOL menuBar )
537 if (lpitem->item_flags & MF_OWNERDRAW)
539 DRAWITEMSTRUCT32 dis;
541 dprintf_menu( stddeb, "DrawMenuItem: Ownerdraw!\n" );
542 dis.CtlType = ODT_MENU;
543 dis.itemID = lpitem->item_id;
544 dis.itemData = (DWORD)lpitem->text;
546 if (lpitem->item_flags & MF_CHECKED) dis.itemState |= ODS_CHECKED;
547 if (lpitem->item_flags & MF_GRAYED) dis.itemState |= ODS_GRAYED;
548 if (lpitem->item_flags & MF_HILITE) dis.itemState |= ODS_SELECTED;
549 dis.itemAction = ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS;
552 CONV_RECT16TO32( &lpitem->rect, &dis.rcItem );
553 SendMessage32A( hwnd, WM_DRAWITEM, 0, (LPARAM)&dis );
557 if (menuBar && (lpitem->item_flags & MF_SEPARATOR)) return;
560 /* Draw the background */
562 if (lpitem->item_flags & MF_HILITE)
563 FillRect16( hdc, &rect, sysColorObjects.hbrushHighlight );
564 else FillRect16( hdc, &rect, sysColorObjects.hbrushMenu );
565 SetBkMode( hdc, TRANSPARENT );
567 /* Draw the separator bar (if any) */
569 if (!menuBar && (lpitem->item_flags & MF_MENUBARBREAK))
571 SelectObject( hdc, sysColorObjects.hpenWindowFrame );
572 MoveTo( hdc, rect.left, 0 );
573 LineTo( hdc, rect.left, height );
575 if (lpitem->item_flags & MF_SEPARATOR)
577 SelectObject( hdc, sysColorObjects.hpenWindowFrame );
578 MoveTo( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
579 LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
585 if (lpitem->item_flags & MF_HILITE)
587 if (lpitem->item_flags & MF_GRAYED)
588 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
590 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
591 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
595 if (lpitem->item_flags & MF_GRAYED)
596 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
598 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
599 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
604 /* Draw the check mark */
606 if (lpitem->item_flags & MF_CHECKED)
608 GRAPH_DrawBitmap(hdc, lpitem->hCheckBit ? lpitem->hCheckBit :
609 hStdCheck, rect.left,
610 (rect.top+rect.bottom-check_bitmap_height) / 2,
611 0, 0, check_bitmap_width, check_bitmap_height );
613 else if (lpitem->hUnCheckBit != 0) /* Not checked */
615 GRAPH_DrawBitmap(hdc, lpitem->hUnCheckBit, rect.left,
616 (rect.top+rect.bottom-check_bitmap_height) / 2,
617 0, 0, check_bitmap_width, check_bitmap_height );
620 /* Draw the popup-menu arrow */
622 if (lpitem->item_flags & MF_POPUP)
624 GRAPH_DrawBitmap( hdc, hStdMnArrow,
625 rect.right-arrow_bitmap_width-1,
626 (rect.top+rect.bottom-arrow_bitmap_height) / 2,
627 0, 0, arrow_bitmap_width, arrow_bitmap_height );
630 rect.left += check_bitmap_width;
631 rect.right -= arrow_bitmap_width;
634 /* Draw the item text or bitmap */
636 if (lpitem->item_flags & MF_BITMAP)
638 GRAPH_DrawBitmap( hdc, (HBITMAP16)(UINT32)lpitem->text,
639 rect.left, rect.top, 0, 0,
640 rect.right-rect.left, rect.bottom-rect.top );
643 /* No bitmap - process text if present */
644 else if (IS_STRING_ITEM(lpitem->item_flags))
650 rect.left += MENU_BAR_ITEMS_SPACE / 2;
651 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
652 i = strlen( lpitem->text );
656 for (i = 0; lpitem->text[i]; i++)
657 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
661 DrawText16( hdc, lpitem->text, i, &rect,
662 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
664 if (lpitem->text[i]) /* There's a tab or flush-right char */
666 if (lpitem->text[i] == '\t')
668 rect.left = lpitem->xTab;
669 DrawText16( hdc, lpitem->text + i + 1, -1, &rect,
670 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
672 else DrawText16( hdc, lpitem->text + i + 1, -1, &rect,
673 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
679 /***********************************************************************
682 * Paint a popup menu.
684 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
691 GetClientRect16( hwnd, &rect );
692 FillRect16( hdc, &rect, sysColorObjects.hbrushMenu );
693 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
694 if (!menu || !menu->nItems) return;
695 for (i = menu->nItems, item = menu->items; i > 0; i--, item++)
696 MENU_DrawMenuItem( hwnd, hdc, item, menu->Height, FALSE );
700 /***********************************************************************
703 * Paint a menu bar. Returns the height of the menu bar.
705 UINT MENU_DrawMenuBar(HDC hDC, LPRECT16 lprect, HWND hwnd, BOOL suppress_draw)
709 WND *wndPtr = WIN_FindWndPtr( hwnd );
711 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR( (HMENU)wndPtr->wIDmenu );
712 if (lppop == NULL || lprect == NULL) return SYSMETRICS_CYMENU;
713 dprintf_menu(stddeb,"MENU_DrawMenuBar(%04x, %p, %p); !\n",
715 if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
716 lprect->bottom = lprect->top + lppop->Height;
717 if (suppress_draw) return lppop->Height;
719 FillRect16(hDC, lprect, sysColorObjects.hbrushMenu );
720 SelectObject( hDC, sysColorObjects.hpenWindowFrame );
721 MoveTo( hDC, lprect->left, lprect->bottom );
722 LineTo( hDC, lprect->right, lprect->bottom );
724 if (lppop->nItems == 0) return SYSMETRICS_CYMENU;
725 for (i = 0; i < lppop->nItems; i++)
727 MENU_DrawMenuItem( hwnd, hDC, &lppop->items[i], lppop->Height, TRUE );
729 return lppop->Height;
733 /***********************************************************************
736 BOOL MENU_SwitchTPWndTo( HTASK hTask)
738 /* This is supposed to be called when popup is hidden */
740 TDB* task = (TDB*)GlobalLock16(hTask);
742 if( !task ) return 0;
744 /* if this task got as far as menu tracking it must have a queue */
746 pTopPWnd->hInstance = task->hInstance;
747 pTopPWnd->hmemTaskQ = task->hQueue;
751 /***********************************************************************
754 * Display a popup menu.
756 static BOOL MENU_ShowPopup(HWND hwndOwner, HMENU hmenu, UINT id, int x, int y)
762 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
763 if (menu->FocusedItem != NO_SELECTED_ITEM)
765 menu->items[menu->FocusedItem].item_flags &= ~(MF_HILITE|MF_MOUSESELECT);
766 menu->FocusedItem = NO_SELECTED_ITEM;
768 SendMessage16( hwndOwner, WM_INITMENUPOPUP, (WPARAM)hmenu,
769 MAKELONG( id, (menu->wFlags & MF_SYSMENU) ? 1 : 0 ));
770 MENU_PopupMenuCalcSize( menu, hwndOwner );
772 wndPtr = WIN_FindWndPtr( hwndOwner );
773 if (!wndPtr) return FALSE;
777 pTopPWnd = WIN_FindWndPtr(CreateWindow16( POPUPMENU_CLASS_ATOM, NULL,
778 WS_POPUP | WS_BORDER, x, y,
779 menu->Width + 2*SYSMETRICS_CXBORDER,
780 menu->Height + 2*SYSMETRICS_CYBORDER,
781 0, 0, wndPtr->hInstance,
782 (LPVOID)(HMENU32)hmenu ));
783 if (!pTopPWnd) return FALSE;
789 /* create new window for the submenu */
790 HWND hWnd = CreateWindow16( POPUPMENU_CLASS_ATOM, NULL,
791 WS_POPUP | WS_BORDER, x, y,
792 menu->Width + 2*SYSMETRICS_CXBORDER,
793 menu->Height + 2*SYSMETRICS_CYBORDER,
794 menu->hWnd, 0, wndPtr->hInstance,
795 (LPVOID)(HMENU32)hmenu );
796 if( !hWnd ) return FALSE;
803 MENU_SwitchTPWndTo(GetCurrentTask());
804 SendMessage16( pTopPWnd->hwndSelf, WM_USER, (WPARAM)hmenu, 0L);
806 menu->hWnd = pTopPWnd->hwndSelf;
811 SetWindowPos(menu->hWnd, 0, x, y, menu->Width + 2*SYSMETRICS_CXBORDER,
812 menu->Height + 2*SYSMETRICS_CYBORDER,
813 SWP_NOACTIVATE | SWP_NOZORDER );
815 /* Display the window */
817 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
818 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
819 UpdateWindow( menu->hWnd );
824 /***********************************************************************
827 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
828 BOOL sendMenuSelect )
833 lppop = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
834 if (!lppop->nItems) return;
835 if ((wIndex != NO_SELECTED_ITEM) &&
836 (wIndex != SYSMENU_SELECTED) &&
837 (lppop->items[wIndex].item_flags & MF_SEPARATOR))
838 wIndex = NO_SELECTED_ITEM;
839 if (lppop->FocusedItem == wIndex) return;
840 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
841 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
843 /* Clear previous highlighted item */
844 if (lppop->FocusedItem != NO_SELECTED_ITEM)
846 if (lppop->FocusedItem == SYSMENU_SELECTED)
847 NC_DrawSysButton( lppop->hWnd, hdc, FALSE );
850 lppop->items[lppop->FocusedItem].item_flags &=~(MF_HILITE|MF_MOUSESELECT);
851 MENU_DrawMenuItem(lppop->hWnd,hdc,&lppop->items[lppop->FocusedItem],
852 lppop->Height, !(lppop->wFlags & MF_POPUP) );
856 /* Highlight new item (if any) */
857 lppop->FocusedItem = wIndex;
858 if (lppop->FocusedItem != NO_SELECTED_ITEM)
860 if (lppop->FocusedItem == SYSMENU_SELECTED)
862 NC_DrawSysButton( lppop->hWnd, hdc, TRUE );
866 SendMessage32A( hwndOwner, WM_MENUSELECT,
867 MAKEWPARAM( WIN_FindWndPtr(lppop->hWnd)->hSysMenu,
868 lppop->wFlags | MF_MOUSESELECT ),
871 SendMessage16( hwndOwner, WM_MENUSELECT,
872 WIN_FindWndPtr(lppop->hWnd)->hSysMenu,
873 MAKELONG(lppop->wFlags | MF_MOUSESELECT, hmenu));
878 lppop->items[lppop->FocusedItem].item_flags |= MF_HILITE;
879 MENU_DrawMenuItem( lppop->hWnd, hdc, &lppop->items[lppop->FocusedItem],
880 lppop->Height, !(lppop->wFlags & MF_POPUP) );
883 SendMessage32A( hwndOwner, WM_MENUSELECT,
884 MAKEWPARAM( lppop->items[lppop->FocusedItem].item_id,
885 lppop->items[lppop->FocusedItem].item_flags|
889 SendMessage16( hwndOwner, WM_MENUSELECT,
890 lppop->items[lppop->FocusedItem].item_id,
891 MAKELONG( lppop->items[lppop->FocusedItem].item_flags | MF_MOUSESELECT, hmenu));
897 else if (sendMenuSelect)
898 SendMessage32A( hwndOwner, WM_MENUSELECT,
899 MAKEWPARAM( (DWORD)hmenu, lppop->wFlags | MF_MOUSESELECT),
902 else if (sendMenuSelect)
903 SendMessage16( hwndOwner, WM_MENUSELECT, hmenu,
904 MAKELONG( lppop->wFlags | MF_MOUSESELECT, hmenu ) );
907 ReleaseDC( lppop->hWnd, hdc );
911 /***********************************************************************
912 * MENU_SelectNextItem
914 static void MENU_SelectNextItem( HWND hwndOwner, HMENU hmenu )
919 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
920 if (!menu->items) return;
921 if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
922 (menu->FocusedItem != SYSMENU_SELECTED))
924 for (i = menu->FocusedItem+1; i < menu->nItems; i++)
926 if (!(menu->items[i].item_flags & MF_SEPARATOR))
928 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
932 if (MENU_HasSysMenu( menu ))
934 MENU_SelectItem( hwndOwner, hmenu, SYSMENU_SELECTED, TRUE );
938 for (i = 0; i < menu->nItems; i++)
940 if (!(menu->items[i].item_flags & MF_SEPARATOR))
942 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
946 if (MENU_HasSysMenu( menu ))
947 MENU_SelectItem( hwndOwner, hmenu, SYSMENU_SELECTED, TRUE );
951 /***********************************************************************
952 * MENU_SelectPrevItem
954 static void MENU_SelectPrevItem( HWND hwndOwner, HMENU hmenu )
959 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
960 if (!menu->items) return;
961 if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
962 (menu->FocusedItem != SYSMENU_SELECTED))
964 for (i = menu->FocusedItem - 1; i >= 0; i--)
966 if (!(menu->items[i].item_flags & MF_SEPARATOR))
968 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
972 if (MENU_HasSysMenu( menu ))
974 MENU_SelectItem( hwndOwner, hmenu, SYSMENU_SELECTED, TRUE );
978 for (i = menu->nItems - 1; i > 0; i--)
980 if (!(menu->items[i].item_flags & MF_SEPARATOR))
982 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
986 if (MENU_HasSysMenu( menu ))
987 MENU_SelectItem( hwndOwner, hmenu, SYSMENU_SELECTED, TRUE );
991 /**********************************************************************
994 * Set an item flags, id and text ptr.
996 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT id, LPCSTR str )
998 LPSTR prevText = IS_STRING_ITEM(item->item_flags) ? item->text : NULL;
1000 if (IS_STRING_ITEM(flags))
1004 flags |= MF_SEPARATOR;
1010 /* Item beginning with a backspace is a help item */
1016 if (!(text = HEAP_strdupA( SystemHeap, 0, str ))) return FALSE;
1020 else if ((flags & MF_BITMAP) || (flags & MF_OWNERDRAW))
1021 item->text = (LPSTR)str;
1022 else item->text = NULL;
1024 item->item_flags = flags & ~(MF_HILITE | MF_MOUSESELECT);
1026 SetRectEmpty16( &item->rect );
1027 if (prevText) HeapFree( SystemHeap, 0, prevText );
1032 /**********************************************************************
1035 * Insert a new item into a menu.
1037 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1042 if (!(menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu)))
1044 dprintf_menu( stddeb, "MENU_InsertItem: %04x not a menu handle\n",
1049 /* Find where to insert new item */
1051 if ((flags & MF_BYPOSITION) &&
1052 ((pos == (UINT)-1) || (pos == menu->nItems)))
1054 /* Special case: append to menu */
1055 /* Some programs specify the menu length to do that */
1060 if (!MENU_FindItem( &hMenu, &pos, flags ))
1062 dprintf_menu( stddeb, "MENU_InsertItem: item %x not found\n",
1066 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu)))
1068 dprintf_menu(stddeb,"MENU_InsertItem: %04x not a menu handle\n",
1074 /* Create new items array */
1076 newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1079 dprintf_menu( stddeb, "MENU_InsertItem: allocation failed\n" );
1082 if (menu->nItems > 0)
1084 /* Copy the old array into the new */
1085 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1086 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1087 (menu->nItems-pos)*sizeof(MENUITEM) );
1088 HeapFree( SystemHeap, 0, menu->items );
1090 menu->items = newItems;
1092 memset( &newItems[pos], 0, sizeof(*newItems) );
1093 return &newItems[pos];
1097 /**********************************************************************
1098 * MENU_ParseResource
1100 * Parse a standard menu resource and add items to the menu.
1101 * Return a pointer to the end of the resource.
1103 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1110 flags = GET_WORD(res);
1111 res += sizeof(WORD);
1112 if (!(flags & MF_POPUP))
1115 res += sizeof(WORD);
1117 if (!IS_STRING_ITEM(flags))
1118 fprintf( stderr, "MENU_ParseResource: not a string item %04x\n",
1121 if (!unicode) res += strlen(str) + 1;
1122 else res += (STRING32_lstrlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1123 if (flags & MF_POPUP)
1125 HMENU hSubMenu = CreatePopupMenu();
1126 if (!hSubMenu) return NULL;
1127 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1129 if (!unicode) AppendMenu32A( hMenu, flags, (UINT)hSubMenu, str );
1130 else AppendMenu32W( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1132 else /* Not a popup */
1134 if (!unicode) AppendMenu32A( hMenu, flags, id, *str ? str : NULL );
1135 else AppendMenu32W( hMenu, flags, id,
1136 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1138 } while (!(flags & MF_END));
1143 /***********************************************************************
1146 * Return the handle of the selected sub-popup menu (if any).
1148 static HMENU MENU_GetSubPopup( HMENU hmenu )
1153 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1154 if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
1155 else if (menu->FocusedItem == SYSMENU_SELECTED)
1156 return WIN_FindWndPtr(menu->hWnd)->hSysMenu;
1158 item = &menu->items[menu->FocusedItem];
1159 if (!(item->item_flags & MF_POPUP) || !(item->item_flags & MF_MOUSESELECT))
1161 return (HMENU)item->item_id;
1165 /***********************************************************************
1166 * MENU_HideSubPopups
1168 * Hide the sub-popup menus of this menu.
1170 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
1171 BOOL sendMenuSelect )
1174 POPUPMENU *menu, *submenu;
1177 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return;
1178 if (menu->FocusedItem == NO_SELECTED_ITEM) return;
1179 if (menu->FocusedItem == SYSMENU_SELECTED)
1181 hsubmenu = WIN_FindWndPtr(menu->hWnd)->hSysMenu;
1185 item = &menu->items[menu->FocusedItem];
1186 if (!(item->item_flags & MF_POPUP) ||
1187 !(item->item_flags & MF_MOUSESELECT)) return;
1188 item->item_flags &= ~MF_MOUSESELECT;
1189 hsubmenu = (HMENU)item->item_id;
1191 submenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hsubmenu );
1192 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
1193 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect );
1194 if (submenu->hWnd == pTopPWnd->hwndSelf )
1196 ShowWindow( submenu->hWnd, SW_HIDE );
1201 DestroyWindow( submenu->hWnd );
1207 /***********************************************************************
1210 * Display the sub-menu of the selected item of this menu.
1211 * Return the handle of the submenu, or hmenu if no submenu to display.
1213 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu, BOOL selectFirst )
1219 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return hmenu;
1220 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return hmenu;
1221 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
1222 if (menu->FocusedItem == SYSMENU_SELECTED)
1224 MENU_ShowPopup(hwndOwner, wndPtr->hSysMenu, 0, wndPtr->rectClient.left,
1225 wndPtr->rectClient.top - menu->Height - 2*SYSMETRICS_CYBORDER);
1226 if (selectFirst) MENU_SelectNextItem( hwndOwner, wndPtr->hSysMenu );
1227 return wndPtr->hSysMenu;
1229 item = &menu->items[menu->FocusedItem];
1230 if (!(item->item_flags & MF_POPUP) ||
1231 (item->item_flags & (MF_GRAYED | MF_DISABLED))) return hmenu;
1232 item->item_flags |= MF_MOUSESELECT;
1233 if (menu->wFlags & MF_POPUP)
1235 MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
1236 wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width,
1237 wndPtr->rectWindow.top + item->rect.top );
1241 MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
1242 wndPtr->rectWindow.left + item->rect.left,
1243 wndPtr->rectWindow.top + item->rect.bottom );
1245 if (selectFirst) MENU_SelectNextItem( hwndOwner, (HMENU)item->item_id );
1246 return (HMENU)item->item_id;
1250 /***********************************************************************
1251 * MENU_FindMenuByCoords
1253 * Find the menu containing a given point (in screen coords).
1255 static HMENU MENU_FindMenuByCoords( HMENU hmenu, POINT16 pt )
1260 if (!(hwnd = WindowFromPoint16( pt ))) return 0;
1263 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1264 if (menu->hWnd == hwnd)
1266 if (!(menu->wFlags & MF_POPUP))
1268 /* Make sure it's in the menu bar (or in system menu) */
1269 WND *wndPtr = WIN_FindWndPtr( menu->hWnd );
1270 if ((pt.x < wndPtr->rectClient.left) ||
1271 (pt.x >= wndPtr->rectClient.right) ||
1272 (pt.y >= wndPtr->rectClient.top)) return 0;
1273 if (pt.y < wndPtr->rectClient.top - menu->Height)
1275 if (!MENU_IsInSysMenu( menu, pt )) return 0;
1277 /* else it's in the menu bar */
1281 hmenu = MENU_GetSubPopup( hmenu );
1287 /***********************************************************************
1288 * MENU_ExecFocusedItem
1290 * Execute a menu item (for instance when user pressed Enter).
1291 * Return TRUE if we can go on with menu tracking.
1293 static BOOL MENU_ExecFocusedItem( HWND hwndOwner, HMENU hmenu,
1294 HMENU *hmenuCurrent )
1297 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1298 if (!menu || !menu->nItems || (menu->FocusedItem == NO_SELECTED_ITEM) ||
1299 (menu->FocusedItem == SYSMENU_SELECTED)) return TRUE;
1300 item = &menu->items[menu->FocusedItem];
1301 if (!(item->item_flags & MF_POPUP))
1303 if (!(item->item_flags & (MF_GRAYED | MF_DISABLED)))
1305 PostMessage( hwndOwner, (menu->wFlags & MF_SYSMENU) ?
1306 WM_SYSCOMMAND : WM_COMMAND, item->item_id, 0 );
1313 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
1319 /***********************************************************************
1322 * Handle a button-down event in a menu. Point is in screen coords.
1323 * hmenuCurrent is the top-most visible popup.
1324 * Return TRUE if we can go on with menu tracking.
1326 static BOOL MENU_ButtonDown( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
1333 if (!hmenu) return FALSE; /* Outside all menus */
1334 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1335 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
1336 if (!item) /* Maybe in system menu */
1338 if (!MENU_IsInSysMenu( menu, pt )) return FALSE;
1339 id = SYSMENU_SELECTED;
1342 if (menu->FocusedItem == id)
1344 if (id == SYSMENU_SELECTED) return FALSE;
1345 if (item->item_flags & MF_POPUP)
1347 if (item->item_flags & MF_MOUSESELECT)
1349 if (menu->wFlags & MF_POPUP)
1351 MENU_HideSubPopups( hwndOwner, hmenu, TRUE );
1352 *hmenuCurrent = hmenu;
1356 else *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
1361 MENU_HideSubPopups( hwndOwner, hmenu, FALSE );
1362 MENU_SelectItem( hwndOwner, hmenu, id, TRUE );
1363 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
1369 /***********************************************************************
1372 * Handle a button-up event in a menu. Point is in screen coords.
1373 * hmenuCurrent is the top-most visible popup.
1374 * Return TRUE if we can go on with menu tracking.
1376 static BOOL MENU_ButtonUp( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
1384 if (!hmenu) return FALSE; /* Outside all menus */
1385 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1386 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
1387 if (!item) /* Maybe in system menu */
1389 if (!MENU_IsInSysMenu( menu, pt )) return FALSE;
1390 id = SYSMENU_SELECTED;
1391 hsubmenu = WIN_FindWndPtr(menu->hWnd)->hSysMenu;
1394 if (menu->FocusedItem != id) return FALSE;
1396 if (id != SYSMENU_SELECTED)
1398 if (!(item->item_flags & MF_POPUP))
1400 return MENU_ExecFocusedItem( hwndOwner, hmenu, hmenuCurrent );
1402 hsubmenu = (HMENU)item->item_id;
1404 /* Select first item of sub-popup */
1405 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, FALSE );
1406 MENU_SelectNextItem( hwndOwner, hsubmenu );
1411 /***********************************************************************
1414 * Handle a motion event in a menu. Point is in screen coords.
1415 * hmenuCurrent is the top-most visible popup.
1416 * Return TRUE if we can go on with menu tracking.
1418 static BOOL MENU_MouseMove( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
1422 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1423 UINT id = NO_SELECTED_ITEM;
1427 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
1428 if (!item) /* Maybe in system menu */
1430 if (!MENU_IsInSysMenu( menu, pt ))
1431 id = NO_SELECTED_ITEM; /* Outside all items */
1432 else id = SYSMENU_SELECTED;
1435 if (id == NO_SELECTED_ITEM)
1437 MENU_SelectItem( hwndOwner, *hmenuCurrent, NO_SELECTED_ITEM, TRUE );
1439 else if (menu->FocusedItem != id)
1441 MENU_HideSubPopups( hwndOwner, hmenu, FALSE );
1442 MENU_SelectItem( hwndOwner, hmenu, id, TRUE );
1443 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
1449 /***********************************************************************
1452 * Handle a VK_LEFT key event in a menu.
1453 * hmenuCurrent is the top-most visible popup.
1455 static void MENU_KeyLeft( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent )
1458 HMENU hmenutmp, hmenuprev;
1460 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1461 hmenuprev = hmenutmp = hmenu;
1462 while (hmenutmp != *hmenuCurrent)
1464 hmenutmp = MENU_GetSubPopup( hmenuprev );
1465 if (hmenutmp != *hmenuCurrent) hmenuprev = hmenutmp;
1467 MENU_HideSubPopups( hwndOwner, hmenuprev, TRUE );
1469 if ((hmenuprev == hmenu) && !(menu->wFlags & MF_POPUP))
1471 /* Select previous item on the menu bar */
1472 MENU_SelectPrevItem( hwndOwner, hmenu );
1473 if (*hmenuCurrent != hmenu)
1475 /* A popup menu was displayed -> display the next one */
1476 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
1479 else *hmenuCurrent = hmenuprev;
1483 /***********************************************************************
1486 * Handle a VK_RIGHT key event in a menu.
1487 * hmenuCurrent is the top-most visible popup.
1489 static void MENU_KeyRight( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent )
1494 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1496 if ((menu->wFlags & MF_POPUP) || (*hmenuCurrent != hmenu))
1498 /* If already displaying a popup, try to display sub-popup */
1499 hmenutmp = MENU_ShowSubPopup( hwndOwner, *hmenuCurrent, TRUE );
1500 if (hmenutmp != *hmenuCurrent) /* Sub-popup displayed */
1502 *hmenuCurrent = hmenutmp;
1507 /* If on menu-bar, go to next item */
1508 if (!(menu->wFlags & MF_POPUP))
1510 MENU_HideSubPopups( hwndOwner, hmenu, FALSE );
1511 MENU_SelectNextItem( hwndOwner, hmenu );
1512 if (*hmenuCurrent != hmenu)
1514 /* A popup menu was displayed -> display the next one */
1515 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
1518 else if (*hmenuCurrent != hmenu) /* Hide last level popup */
1521 hmenuprev = hmenutmp = hmenu;
1522 while (hmenutmp != *hmenuCurrent)
1524 hmenutmp = MENU_GetSubPopup( hmenuprev );
1525 if (hmenutmp != *hmenuCurrent) hmenuprev = hmenutmp;
1527 MENU_HideSubPopups( hwndOwner, hmenuprev, TRUE );
1528 *hmenuCurrent = hmenuprev;
1533 /***********************************************************************
1536 * Menu tracking code.
1537 * If 'x' and 'y' are not 0, we simulate a button-down event at (x,y)
1538 * before beginning tracking. This is to help menu-bar tracking.
1540 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, int x, int y,
1541 HWND hwnd, const RECT16 *lprect )
1546 HMENU hmenuCurrent = hmenu;
1547 BOOL fClosed = FALSE, fRemove;
1550 fEndMenuCalled = FALSE;
1551 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
1554 POINT16 pt = { x, y };
1555 MENU_ButtonDown( hwnd, hmenu, &hmenuCurrent, pt );
1558 hMsg = USER_HEAP_ALLOC( sizeof(MSG) );
1559 msg = (MSG *)USER_HEAP_LIN_ADDR( hMsg );
1562 if (!MSG_InternalGetMessage( (SEGPTR)USER_HEAP_SEG_ADDR(hMsg), 0,
1563 hwnd, MSGF_MENU, 0, TRUE ))
1567 if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
1569 /* Find the sub-popup for this mouse event (if any) */
1570 HMENU hsubmenu = MENU_FindMenuByCoords( hmenu, msg->pt );
1572 switch(msg->message)
1574 case WM_RBUTTONDOWN:
1575 case WM_NCRBUTTONDOWN:
1576 if (!(wFlags & TPM_RIGHTBUTTON)) break;
1578 case WM_LBUTTONDOWN:
1579 case WM_NCLBUTTONDOWN:
1580 fClosed = !MENU_ButtonDown( hwnd, hsubmenu,
1581 &hmenuCurrent, msg->pt );
1585 case WM_NCRBUTTONUP:
1586 if (!(wFlags & TPM_RIGHTBUTTON)) break;
1589 case WM_NCLBUTTONUP:
1590 /* If outside all menus but inside lprect, ignore it */
1591 if (!hsubmenu && lprect && PtInRect16(lprect, msg->pt)) break;
1592 fClosed = !MENU_ButtonUp( hwnd, hsubmenu,
1593 &hmenuCurrent, msg->pt );
1594 fRemove = TRUE; /* Remove event even if outside menu */
1598 case WM_NCMOUSEMOVE:
1599 if ((msg->wParam & MK_LBUTTON) ||
1600 ((wFlags & TPM_RIGHTBUTTON) && (msg->wParam & MK_RBUTTON)))
1602 fClosed = !MENU_MouseMove( hwnd, hsubmenu,
1603 &hmenuCurrent, msg->pt );
1608 else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
1610 fRemove = TRUE; /* Keyboard messages are always removed */
1611 switch(msg->message)
1617 MENU_SelectItem( hwnd, hmenuCurrent, NO_SELECTED_ITEM, FALSE );
1618 MENU_SelectNextItem( hwnd, hmenuCurrent );
1622 MENU_SelectItem( hwnd, hmenuCurrent, NO_SELECTED_ITEM, FALSE );
1623 MENU_SelectPrevItem( hwnd, hmenuCurrent );
1627 MENU_SelectPrevItem( hwnd, hmenuCurrent );
1631 /* If on menu bar, pull-down the menu */
1632 if (!(menu->wFlags & MF_POPUP) && (hmenuCurrent == hmenu))
1633 hmenuCurrent = MENU_ShowSubPopup( hwnd, hmenu, TRUE );
1635 MENU_SelectNextItem( hwnd, hmenuCurrent );
1639 MENU_KeyLeft( hwnd, hmenu, &hmenuCurrent );
1643 MENU_KeyRight( hwnd, hmenu, &hmenuCurrent );
1648 fClosed = !MENU_ExecFocusedItem( hwnd, hmenuCurrent,
1659 break; /* WM_KEYDOWN */
1669 break; /* WM_SYSKEYDOWN */
1673 /* Hack to avoid control chars. */
1674 /* We will find a better way real soon... */
1675 if ((msg->wParam <= 32) || (msg->wParam >= 127)) break;
1676 pos = MENU_FindItemByKey( hwnd, hmenuCurrent, msg->wParam );
1677 if (pos == (UINT)-2) fClosed = TRUE;
1678 else if (pos == (UINT)-1) MessageBeep(0);
1681 MENU_SelectItem( hwnd, hmenuCurrent, pos, TRUE );
1682 fClosed = !MENU_ExecFocusedItem( hwnd, hmenuCurrent,
1687 break; /* WM_CHAR */
1688 } /* switch(msg->message) */
1692 DispatchMessage( msg );
1694 if (fEndMenuCalled) fClosed = TRUE;
1695 if (!fClosed) fRemove = TRUE;
1697 if (fRemove) /* Remove the message from the queue */
1698 PeekMessage( msg, 0, msg->message, msg->message, PM_REMOVE );
1700 USER_HEAP_FREE( hMsg );
1702 MENU_HideSubPopups( hwnd, hmenu, FALSE );
1703 if (menu->wFlags & MF_POPUP)
1705 ShowWindow( menu->hWnd, SW_HIDE );
1708 MENU_SelectItem( hwnd, hmenu, NO_SELECTED_ITEM, FALSE );
1709 SendMessage16( hwnd, WM_MENUSELECT, 0, MAKELONG( 0xffff, 0 ) );
1710 fEndMenuCalled = FALSE;
1715 /***********************************************************************
1716 * MENU_TrackMouseMenuBar
1718 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
1720 void MENU_TrackMouseMenuBar( HWND hwnd, POINT16 pt )
1722 WND *wndPtr = WIN_FindWndPtr( hwnd );
1724 SendMessage16( hwnd, WM_ENTERMENULOOP, 0, 0 );
1725 SendMessage16( hwnd, WM_INITMENU, wndPtr->wIDmenu, 0 );
1726 MENU_TrackMenu( (HMENU)wndPtr->wIDmenu, TPM_LEFTALIGN | TPM_LEFTBUTTON,
1727 pt.x, pt.y, hwnd, NULL );
1728 SendMessage16( hwnd, WM_EXITMENULOOP, 0, 0 );
1733 /***********************************************************************
1734 * MENU_TrackKbdMenuBar
1736 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
1738 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT wParam, INT vkey)
1740 UINT uItem = NO_SELECTED_ITEM;
1743 /* find window that has a menu
1746 if( !(wndPtr->dwStyle & WS_CHILD) )
1748 wndPtr = WIN_FindWndPtr( GetActiveWindow() );
1749 if( !wndPtr ) return;
1752 while( wndPtr->dwStyle & WS_CHILD &&
1753 !(wndPtr->dwStyle & WS_SYSMENU) )
1754 if( !(wndPtr = wndPtr->parent) ) return;
1756 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu )
1757 if( !(wndPtr->dwStyle & WS_SYSMENU) )
1760 hTrackMenu = ( IsMenu( wndPtr->wIDmenu ) )? wndPtr->wIDmenu:
1764 SendMessage16( wndPtr->hwndSelf, WM_ENTERMENULOOP, 0, 0 );
1765 SendMessage16( wndPtr->hwndSelf, WM_INITMENU, wndPtr->wIDmenu, 0 );
1767 /* find suitable menu entry
1770 if( vkey == VK_SPACE )
1771 uItem = SYSMENU_SELECTED;
1774 uItem = MENU_FindItemByKey( wndPtr->hwndSelf, wndPtr->wIDmenu, vkey );
1775 if( uItem >= 0xFFFE )
1777 if( uItem == 0xFFFF )
1779 SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
1785 MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE );
1786 if( uItem == NO_SELECTED_ITEM )
1787 MENU_SelectNextItem( wndPtr->hwndSelf, hTrackMenu );
1789 PostMessage( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
1791 MENU_TrackMenu( hTrackMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON,
1792 0, 0, wndPtr->hwndSelf, NULL );
1794 SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
1799 /**********************************************************************
1800 * TrackPopupMenu16 (USER.416)
1802 BOOL16 TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
1803 INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
1808 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y ))
1809 ret = MENU_TrackMenu( hMenu, wFlags, 0, 0, hWnd, lpRect );
1815 /**********************************************************************
1816 * TrackPopupMenu32 (USER32.548)
1818 BOOL32 TrackPopupMenu32( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
1819 INT32 nReserved, HWND32 hWnd, const RECT32 *lpRect )
1822 CONV_RECT32TO16( lpRect, &r );
1823 return TrackPopupMenu16( hMenu, wFlags, x, y, nReserved, hWnd, &r );
1827 /***********************************************************************
1830 LRESULT PopupMenuWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
1836 CREATESTRUCT16 *cs = (CREATESTRUCT16*)PTR_SEG_TO_LIN(lParam);
1837 SetWindowLong32A( hwnd, 0, (LONG)cs->lpCreateParams );
1841 case WM_MOUSEACTIVATE: /* We don't want to be activated */
1842 return MA_NOACTIVATE;
1847 BeginPaint16( hwnd, &ps );
1848 MENU_DrawPopupMenu( hwnd, ps.hdc,
1849 (HMENU)GetWindowLong32A( hwnd, 0 ) );
1850 EndPaint16( hwnd, &ps );
1855 /* zero out global pointer in case system popup
1856 * was destroyed by AppExit
1859 if( hwnd == pTopPWnd->hwndSelf )
1866 if (wParam) SetWindowLong32A( hwnd, 0, (HMENU)wParam );
1869 return DefWindowProc16(hwnd, message, wParam, lParam);
1875 /***********************************************************************
1876 * MENU_GetMenuBarHeight
1878 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
1880 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth, int orgX, int orgY )
1887 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1888 if (!(lppop = (LPPOPUPMENU)USER_HEAP_LIN_ADDR((HMENU)wndPtr->wIDmenu)))
1890 hdc = GetDC( hwnd );
1891 SetRect16(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU);
1892 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
1893 ReleaseDC( hwnd, hdc );
1894 return lppop->Height;
1898 /*******************************************************************
1899 * ChangeMenu16 (USER.153)
1901 BOOL16 ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
1902 UINT16 id, UINT16 flags )
1904 dprintf_menu( stddeb,"ChangeMenu16: menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
1905 hMenu, pos, (DWORD)data, id, flags );
1906 if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
1908 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
1909 /* for MF_DELETE. We should check the parameters for all others */
1910 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
1911 if (flags & MF_DELETE) return DeleteMenu( hMenu, pos, flags & ~MF_DELETE );
1912 if (flags & MF_CHANGE) return ModifyMenu16( hMenu, pos, flags & ~MF_CHANGE,
1914 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
1915 flags & MF_BYPOSITION ? pos : id,
1916 flags & ~MF_REMOVE );
1917 /* Default: MF_INSERT */
1918 return InsertMenu16( hMenu, pos, flags, id, data );
1922 /*******************************************************************
1923 * ChangeMenu32A (USER32.22)
1925 BOOL32 ChangeMenu32A( HMENU32 hMenu, UINT32 pos, LPCSTR data,
1926 UINT32 id, UINT32 flags )
1928 dprintf_menu( stddeb,"ChangeMenu32A: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1929 hMenu, pos, (DWORD)data, id, flags );
1930 if (flags & MF_APPEND) return AppendMenu32A( hMenu, flags & ~MF_APPEND,
1932 if (flags & MF_DELETE) return DeleteMenu( hMenu, pos, flags & ~MF_DELETE );
1933 if (flags & MF_CHANGE) return ModifyMenu32A(hMenu, pos, flags & ~MF_CHANGE,
1935 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
1936 flags & MF_BYPOSITION ? pos : id,
1937 flags & ~MF_REMOVE );
1938 /* Default: MF_INSERT */
1939 return InsertMenu32A( hMenu, pos, flags, id, data );
1943 /*******************************************************************
1944 * ChangeMenu32W (USER32.23)
1946 BOOL32 ChangeMenu32W( HMENU32 hMenu, UINT32 pos, LPCWSTR data,
1947 UINT32 id, UINT32 flags )
1949 dprintf_menu( stddeb,"ChangeMenu32W: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1950 hMenu, pos, (DWORD)data, id, flags );
1951 if (flags & MF_APPEND) return AppendMenu32W( hMenu, flags & ~MF_APPEND,
1953 if (flags & MF_DELETE) return DeleteMenu( hMenu, pos, flags & ~MF_DELETE );
1954 if (flags & MF_CHANGE) return ModifyMenu32W(hMenu, pos, flags & ~MF_CHANGE,
1956 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
1957 flags & MF_BYPOSITION ? pos : id,
1958 flags & ~MF_REMOVE );
1959 /* Default: MF_INSERT */
1960 return InsertMenu32W( hMenu, pos, flags, id, data );
1964 /*******************************************************************
1965 * CheckMenuItem (USER.154)
1967 INT CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
1972 dprintf_menu( stddeb,"CheckMenuItem: %04x %04x %04x\n", hMenu, id, flags );
1973 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
1974 ret = item->item_flags & MF_CHECKED;
1975 if (flags & MF_CHECKED) item->item_flags |= MF_CHECKED;
1976 else item->item_flags &= ~MF_CHECKED;
1981 /**********************************************************************
1982 * EnableMenuItem [USER.155]
1984 BOOL EnableMenuItem(HMENU hMenu, UINT wItemID, UINT wFlags)
1987 dprintf_menu(stddeb,"EnableMenuItem (%04x, %04X, %04X) !\n",
1988 hMenu, wItemID, wFlags);
1989 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return FALSE;
1991 /* We can't have MF_GRAYED and MF_DISABLED together */
1992 if (wFlags & MF_GRAYED)
1994 item->item_flags = (item->item_flags & ~MF_DISABLED) | MF_GRAYED;
1996 else if (wFlags & MF_DISABLED)
1998 item->item_flags = (item->item_flags & ~MF_GRAYED) | MF_DISABLED;
2000 else /* MF_ENABLED */
2002 item->item_flags &= ~(MF_GRAYED | MF_DISABLED);
2008 /*******************************************************************
2009 * GetMenuString (USER.161)
2011 int GetMenuString( HMENU hMenu, UINT wItemID,
2012 LPSTR str, short nMaxSiz, UINT wFlags )
2016 dprintf_menu( stddeb, "GetMenuString: menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2017 hMenu, wItemID, str, nMaxSiz, wFlags );
2018 if (!str || !nMaxSiz) return 0;
2020 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2021 if (!IS_STRING_ITEM(item->item_flags)) return 0;
2022 lstrcpyn( str, item->text, nMaxSiz );
2023 dprintf_menu( stddeb, "GetMenuString: returning '%s'\n", str );
2028 /**********************************************************************
2029 * HiliteMenuItem [USER.162]
2031 BOOL HiliteMenuItem(HWND hWnd, HMENU hMenu, UINT wItemID, UINT wHilite)
2034 dprintf_menu(stddeb,"HiliteMenuItem(%04x, %04x, %04x, %04x);\n",
2035 hWnd, hMenu, wItemID, wHilite);
2036 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
2037 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
2038 if (menu->FocusedItem == wItemID) return TRUE;
2039 MENU_HideSubPopups( hWnd, hMenu, FALSE );
2040 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE );
2045 /**********************************************************************
2046 * GetMenuState [USER.250]
2048 UINT GetMenuState(HMENU hMenu, UINT wItemID, UINT wFlags)
2051 dprintf_menu(stddeb,"GetMenuState(%04x, %04x, %04x);\n",
2052 hMenu, wItemID, wFlags);
2053 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
2054 if (item->item_flags & MF_POPUP)
2056 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( (HMENU)item->item_id );
2057 if (!menu) return -1;
2058 else return (menu->nItems << 8) | (menu->wFlags & 0xff);
2060 else return item->item_flags;
2064 /**********************************************************************
2065 * GetMenuItemCount [USER.263]
2067 INT GetMenuItemCount(HMENU hMenu)
2070 dprintf_menu(stddeb,"GetMenuItemCount(%04x);\n", hMenu);
2071 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2072 if (menu == NULL) return (UINT)-1;
2073 dprintf_menu(stddeb,"GetMenuItemCount(%04x) return %d \n",
2074 hMenu, menu->nItems);
2075 return menu->nItems;
2079 /**********************************************************************
2080 * GetMenuItemID [USER.264]
2082 UINT GetMenuItemID(HMENU hMenu, int nPos)
2086 dprintf_menu(stddeb,"GetMenuItemID(%04x, %d);\n", hMenu, nPos);
2087 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
2088 if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
2089 if (menu->items[nPos].item_flags & MF_POPUP) return -1;
2090 return menu->items[nPos].item_id;
2094 /*******************************************************************
2095 * InsertMenu16 (USER.410)
2097 BOOL16 InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
2098 UINT16 id, SEGPTR data )
2100 if (IS_STRING_ITEM(flags) && data)
2101 return InsertMenu32A( hMenu, (INT32)(INT16)pos, flags, id,
2102 (LPSTR)PTR_SEG_TO_LIN(data) );
2103 return InsertMenu32A( hMenu, (INT32)(INT16)pos, flags, id, (LPSTR)data );
2107 /*******************************************************************
2108 * InsertMenu32A (USER32.321)
2110 BOOL32 InsertMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
2111 UINT32 id, LPCSTR str )
2115 if (IS_STRING_ITEM(flags) && str)
2116 dprintf_menu( stddeb, "InsertMenu: %04x %d %04x %04x '%s'\n",
2117 hMenu, pos, flags, id, str );
2118 else dprintf_menu( stddeb, "InsertMenu: %04x %d %04x %04x %08lx\n",
2119 hMenu, pos, flags, id, (DWORD)str );
2121 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
2123 if (!(MENU_SetItemData( item, flags, id, str )))
2125 RemoveMenu( hMenu, pos, flags );
2129 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
2130 ((POPUPMENU *)USER_HEAP_LIN_ADDR((HMENU)id))->wFlags |= MF_POPUP;
2132 item->hCheckBit = hStdCheck;
2133 item->hUnCheckBit = 0;
2138 /*******************************************************************
2139 * InsertMenu32W (USER32.324)
2141 BOOL32 InsertMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
2142 UINT32 id, LPCWSTR str )
2146 if (IS_STRING_ITEM(flags) && str)
2148 LPSTR newstr = STRING32_DupUniToAnsi( str );
2149 ret = InsertMenu32A( hMenu, pos, flags, id, newstr );
2153 else return InsertMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
2157 /*******************************************************************
2158 * AppendMenu16 (USER.411)
2160 BOOL16 AppendMenu16( HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data )
2162 return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
2166 /*******************************************************************
2167 * AppendMenu32A (USER32.4)
2169 BOOL32 AppendMenu32A( HMENU32 hMenu, UINT32 flags, UINT32 id, LPCSTR data )
2171 return InsertMenu32A( hMenu, -1, flags | MF_BYPOSITION, id, data );
2175 /*******************************************************************
2176 * AppendMenu32W (USER32.5)
2178 BOOL32 AppendMenu32W( HMENU32 hMenu, UINT32 flags, UINT32 id, LPCWSTR data )
2180 return InsertMenu32W( hMenu, -1, flags | MF_BYPOSITION, id, data );
2184 /**********************************************************************
2185 * RemoveMenu [USER.412]
2187 BOOL RemoveMenu(HMENU hMenu, UINT nPos, UINT wFlags)
2192 dprintf_menu(stddeb,"RemoveMenu (%04x, %04x, %04x)\n",hMenu, nPos, wFlags);
2193 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
2194 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
2198 if (IS_STRING_ITEM(item->item_flags) && item->text)
2199 HeapFree( SystemHeap, 0, item->text );
2200 if (--menu->nItems == 0)
2202 HeapFree( SystemHeap, 0, menu->items );
2207 while(nPos < menu->nItems)
2213 menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
2214 menu->nItems * sizeof(MENUITEM) );
2220 /**********************************************************************
2221 * DeleteMenu [USER.413]
2223 BOOL DeleteMenu(HMENU hMenu, UINT nPos, UINT wFlags)
2225 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
2226 if (!item) return FALSE;
2227 if (item->item_flags & MF_POPUP) DestroyMenu( (HMENU)item->item_id );
2228 /* nPos is now the position of the item */
2229 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
2234 /*******************************************************************
2235 * ModifyMenu16 (USER.414)
2237 BOOL16 ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
2238 UINT16 id, SEGPTR data )
2240 if (IS_STRING_ITEM(flags))
2241 return ModifyMenu32A( hMenu, (INT32)(INT16)pos, flags, id,
2242 (LPSTR)PTR_SEG_TO_LIN(data) );
2243 return ModifyMenu32A( hMenu, (INT32)(INT16)pos, flags, id, (LPSTR)data );
2247 /*******************************************************************
2248 * ModifyMenu32A (USER32.396)
2250 BOOL32 ModifyMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
2251 UINT32 id, LPCSTR str )
2254 HMENU hMenu16 = hMenu;
2257 if (IS_STRING_ITEM(flags))
2259 dprintf_menu( stddeb, "ModifyMenu: %04x %d %04x %04x '%s'\n",
2260 hMenu, pos, flags, id, str ? str : "#NULL#" );
2261 if (!str) return FALSE;
2265 dprintf_menu( stddeb, "ModifyMenu: %04x %d %04x %04x %08lx\n",
2266 hMenu, pos, flags, id, (DWORD)str );
2269 if (!(item = MENU_FindItem( &hMenu16, &pos16, flags ))) return FALSE;
2270 return MENU_SetItemData( item, flags, id, str );
2274 /*******************************************************************
2275 * ModifyMenu32W (USER32.397)
2277 BOOL32 ModifyMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
2278 UINT32 id, LPCWSTR str )
2282 if (IS_STRING_ITEM(flags) && str)
2284 LPSTR newstr = STRING32_DupUniToAnsi( str );
2285 ret = ModifyMenu32A( hMenu, pos, flags, id, newstr );
2289 else return ModifyMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
2293 /**********************************************************************
2294 * CreatePopupMenu [USER.415]
2296 HMENU CreatePopupMenu()
2301 if (!(hmenu = CreateMenu())) return 0;
2302 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
2303 menu->wFlags |= MF_POPUP;
2308 /**********************************************************************
2309 * GetMenuCheckMarkDimensions [USER.417]
2311 DWORD GetMenuCheckMarkDimensions()
2313 return MAKELONG( check_bitmap_width, check_bitmap_height );
2317 /**********************************************************************
2318 * SetMenuItemBitmaps [USER.418]
2320 BOOL SetMenuItemBitmaps(HMENU hMenu, UINT nPos, UINT wFlags,
2321 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
2324 dprintf_menu(stddeb,"SetMenuItemBitmaps(%04x, %04x, %04x, %04x, %04x)\n",
2325 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
2326 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
2328 if (!hNewCheck && !hNewUnCheck)
2330 /* If both are NULL, restore default bitmaps */
2331 item->hCheckBit = hStdCheck;
2332 item->hUnCheckBit = 0;
2333 item->item_flags &= ~MF_USECHECKBITMAPS;
2335 else /* Install new bitmaps */
2337 item->hCheckBit = hNewCheck;
2338 item->hUnCheckBit = hNewUnCheck;
2339 item->item_flags |= MF_USECHECKBITMAPS;
2345 /**********************************************************************
2346 * CreateMenu [USER.151]
2352 dprintf_menu(stddeb,"CreateMenu !\n");
2353 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) )))
2355 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2357 menu->wMagic = MENU_MAGIC;
2364 menu->FocusedItem = NO_SELECTED_ITEM;
2365 dprintf_menu(stddeb,"CreateMenu // return %04x\n", hMenu);
2370 /**********************************************************************
2371 * DestroyMenu [USER.152]
2373 BOOL DestroyMenu(HMENU hMenu)
2376 dprintf_menu(stddeb,"DestroyMenu (%04x) !\n", hMenu);
2378 if (hMenu == 0) return FALSE;
2379 /* Silently ignore attempts to destroy default system menu */
2380 if (hMenu == MENU_DefSysMenu) return TRUE;
2381 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2382 if (!lppop || (lppop->wMagic != MENU_MAGIC)) return FALSE;
2383 lppop->wMagic = 0; /* Mark it as destroyed */
2384 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd && lppop->hWnd != pTopPWnd->hwndSelf )
2385 DestroyWindow( lppop->hWnd );
2390 MENUITEM *item = lppop->items;
2391 for (i = lppop->nItems; i > 0; i--, item++)
2393 if (item->item_flags & MF_POPUP)
2394 DestroyMenu( (HMENU)item->item_id );
2395 if (IS_STRING_ITEM(item->item_flags) && item->text)
2396 HeapFree( SystemHeap, 0, item->text );
2398 HeapFree( SystemHeap, 0, lppop->items );
2400 USER_HEAP_FREE( hMenu );
2401 dprintf_menu(stddeb,"DestroyMenu (%04x) // End !\n", hMenu);
2405 /**********************************************************************
2406 * GetSystemMenu [USER.156]
2408 HMENU GetSystemMenu(HWND hWnd, BOOL bRevert)
2410 WND *wndPtr = WIN_FindWndPtr( hWnd );
2411 if (!wndPtr) return 0;
2413 if (!wndPtr->hSysMenu || (wndPtr->hSysMenu == MENU_DefSysMenu))
2415 wndPtr->hSysMenu = MENU_CopySysMenu();
2416 return wndPtr->hSysMenu;
2418 if (!bRevert) return wndPtr->hSysMenu;
2419 if (wndPtr->hSysMenu) DestroyMenu(wndPtr->hSysMenu);
2420 wndPtr->hSysMenu = MENU_CopySysMenu();
2421 return wndPtr->hSysMenu;
2425 /*******************************************************************
2426 * SetSystemMenu (USER.280)
2428 BOOL SetSystemMenu( HWND hwnd, HMENU hMenu )
2432 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
2433 if (wndPtr->hSysMenu && (wndPtr->hSysMenu != MENU_DefSysMenu))
2434 DestroyMenu( wndPtr->hSysMenu );
2435 wndPtr->hSysMenu = hMenu;
2440 /**********************************************************************
2441 * GetMenu [USER.157]
2443 HMENU GetMenu(HWND hWnd)
2445 WND * wndPtr = WIN_FindWndPtr(hWnd);
2446 if (wndPtr == NULL) return 0;
2447 return (HMENU)wndPtr->wIDmenu;
2451 /**********************************************************************
2452 * SetMenu [USER.158]
2454 BOOL SetMenu(HWND hWnd, HMENU hMenu)
2457 WND * wndPtr = WIN_FindWndPtr(hWnd);
2458 if (wndPtr == NULL) {
2459 fprintf(stderr,"SetMenu(%04x, %04x) // Bad window handle !\n",
2463 dprintf_menu(stddeb,"SetMenu(%04x, %04x);\n", hWnd, hMenu);
2464 if (GetCapture() == hWnd) ReleaseCapture();
2465 wndPtr->wIDmenu = (UINT)hMenu;
2468 lpmenu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2469 if (lpmenu == NULL) {
2470 fprintf(stderr,"SetMenu(%04x, %04x) // Bad menu handle !\n",
2474 lpmenu->hWnd = hWnd;
2475 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
2476 lpmenu->Height = 0; /* Make sure we recalculate the size */
2478 if (IsWindowVisible(hWnd))
2479 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
2480 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
2486 /**********************************************************************
2487 * GetSubMenu [USER.159]
2489 HMENU GetSubMenu(HMENU hMenu, short nPos)
2493 dprintf_menu(stddeb,"GetSubMenu (%04x, %04X) !\n", hMenu, nPos);
2494 if (!(lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return 0;
2495 if ((UINT)nPos >= lppop->nItems) return 0;
2496 if (!(lppop->items[nPos].item_flags & MF_POPUP)) return 0;
2497 return (HMENU)lppop->items[nPos].item_id;
2501 /**********************************************************************
2502 * DrawMenuBar [USER.160]
2504 void DrawMenuBar(HWND hWnd)
2508 dprintf_menu(stddeb,"DrawMenuBar (%04x)\n", hWnd);
2509 wndPtr = WIN_FindWndPtr(hWnd);
2510 if (wndPtr != NULL && (wndPtr->dwStyle & WS_CHILD) == 0 &&
2511 wndPtr->wIDmenu != 0) {
2512 dprintf_menu(stddeb,"DrawMenuBar wIDmenu=%04X \n",
2514 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR((HMENU)wndPtr->wIDmenu);
2515 if (lppop == NULL) return;
2517 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
2518 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
2519 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
2524 /***********************************************************************
2525 * EndMenu (USER.187)
2529 /* FIXME: this won't work when we have multiple tasks... */
2530 fEndMenuCalled = TRUE;
2534 /***********************************************************************
2535 * LookupMenuHandle (USER.217)
2537 HMENU LookupMenuHandle( HMENU hmenu, INT id )
2539 if (!MENU_FindItem( &hmenu, &id, MF_BYCOMMAND )) return 0;
2544 /**********************************************************************
2545 * LoadMenu (USER.150)
2547 HMENU LoadMenu( HINSTANCE instance, SEGPTR name )
2555 char *str = (char *)PTR_SEG_TO_LIN( name );
2556 dprintf_menu( stddeb, "LoadMenu(%04x,'%s')\n", instance, str );
2557 if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
2560 dprintf_resource(stddeb,"LoadMenu(%04x,%04x)\n",instance,LOWORD(name));
2562 if (!name) return 0;
2564 /* check for Win32 module */
2565 instance = GetExePtr( instance );
2566 if (MODULE_GetPtr(instance)->flags & NE_FFLAGS_WIN32)
2567 return WIN32_LoadMenuA(instance,PTR_SEG_TO_LIN(name));
2569 if (!(hRsrc = FindResource( instance, name, RT_MENU ))) return 0;
2570 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2571 hMenu = LoadMenuIndirect16( LockResource(handle) );
2572 FreeResource( handle );
2577 /**********************************************************************
2578 * LoadMenuIndirect16 (USER.220)
2580 HMENU16 LoadMenuIndirect16( LPCVOID template )
2583 WORD version, offset;
2584 LPCSTR p = (LPCSTR)template;
2586 dprintf_menu(stddeb,"LoadMenuIndirect32A: %p\n", template );
2587 version = GET_WORD(p);
2591 fprintf( stderr, "LoadMenuIndirect16: version must be 0 for Win16\n" );
2594 offset = GET_WORD(p);
2595 p += sizeof(WORD) + offset;
2596 if (!(hMenu = CreateMenu())) return 0;
2597 if (!MENU_ParseResource( p, hMenu, FALSE ))
2599 DestroyMenu( hMenu );
2606 /**********************************************************************
2607 * LoadMenuIndirect32A (USER32.370)
2609 HMENU32 LoadMenuIndirect32A( LPCVOID template )
2612 WORD version, offset;
2613 LPCSTR p = (LPCSTR)template;
2615 dprintf_menu(stddeb,"LoadMenuIndirect32A: %p\n", template );
2616 version = GET_WORD(p);
2620 fprintf( stderr, "LoadMenuIndirect32A: version %d not supported.\n",
2624 offset = GET_WORD(p);
2625 p += sizeof(WORD) + offset;
2626 if (!(hMenu = CreateMenu())) return 0;
2627 if (!MENU_ParseResource( p, hMenu, TRUE ))
2629 DestroyMenu( hMenu );
2636 /**********************************************************************
2637 * LoadMenuIndirect32W (USER32.371)
2639 HMENU32 LoadMenuIndirect32W( LPCVOID template )
2641 /* FIXME: is there anything different between A and W? */
2642 return LoadMenuIndirect32A( template );
2646 /**********************************************************************
2649 BOOL IsMenu( HMENU hmenu )
2652 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
2653 return (menu->wMagic == MENU_MAGIC);