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"
28 /* #define DEBUG_MENU */
29 /* #define DEBUG_MENUCALC */
30 /* #define DEBUG_MENUSHORTCUT */
34 /* Dimension of the menu bitmaps */
35 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
36 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
38 /* Flag set by EndMenu() to force an exit from menu tracking */
39 static BOOL fEndMenuCalled = FALSE;
41 /* Space between 2 menu bar items */
42 #define MENU_BAR_ITEMS_SPACE 16
44 /* Minimum width of a tab character */
45 #define MENU_TAB_SPACE 8
47 /* Height of a separator item */
48 #define SEPARATOR_HEIGHT 5
50 /* Values for menu->FocusedItem */
51 /* (other values give the position of the focused item) */
52 #define NO_SELECTED_ITEM 0xffff
53 #define SYSMENU_SELECTED 0xfffe /* Only valid on menu-bars */
55 #define IS_STRING_ITEM(flags) (!((flags) & (MF_BITMAP | MF_OWNERDRAW | \
56 MF_MENUBARBREAK | MF_MENUBREAK | MF_SEPARATOR)))
59 extern void NC_DrawSysButton(HWND hwnd, HDC hdc, BOOL down); /* nonclient.c */
61 static HBITMAP hStdCheck = 0;
62 static HBITMAP hStdMnArrow = 0;
65 WORD * ParseMenuResource(WORD *first_item, int level, HMENU hMenu);
68 /***********************************************************************
71 * Menus initialisation.
79 if (!(hStdCheck = LoadBitmap( 0, MAKEINTRESOURCE(OBM_CHECK) )))
81 GetObject( hStdCheck, sizeof(BITMAP), (LPSTR)&bm );
82 check_bitmap_width = bm.bmWidth;
83 check_bitmap_height = bm.bmHeight;
84 if (!(hStdMnArrow = LoadBitmap( 0, MAKEINTRESOURCE(OBM_MNARROW) )))
86 GetObject( hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm );
87 arrow_bitmap_width = bm.bmWidth;
88 arrow_bitmap_height = bm.bmHeight;
94 /***********************************************************************
97 * Check whether the window owning the menu bar has a system menu.
99 static BOOL MENU_HasSysMenu( POPUPMENU *menu )
103 if (menu->wFlags & MF_POPUP) return FALSE;
104 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
105 return (wndPtr->dwStyle & WS_SYSMENU) != 0;
109 /***********************************************************************
112 * Check whether the point (in screen coords) is in the system menu
113 * of the window owning the given menu.
115 static BOOL MENU_IsInSysMenu( POPUPMENU *menu, POINT pt )
119 if (menu->wFlags & MF_POPUP) return FALSE;
120 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
121 if (!(wndPtr->dwStyle & WS_SYSMENU)) return FALSE;
122 if ((pt.x < wndPtr->rectClient.left) ||
123 (pt.x >= wndPtr->rectClient.left+SYSMETRICS_CXSIZE+SYSMETRICS_CXBORDER))
125 if ((pt.y >= wndPtr->rectClient.top - menu->Height) ||
126 (pt.y < wndPtr->rectClient.top - menu->Height -
127 SYSMETRICS_CYSIZE - SYSMETRICS_CYBORDER)) return FALSE;
132 /***********************************************************************
135 * Find a menu item. Return a pointer on the item, and modifies *hmenu
136 * in case the item was in a sub-menu.
138 static MENUITEM *MENU_FindItem( HMENU *hmenu, WORD *nPos, WORD wFlags )
144 if (!(menu = (POPUPMENU *) USER_HEAP_ADDR(*hmenu))) return NULL;
145 item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
146 if (wFlags & MF_BYPOSITION)
148 if (*nPos >= menu->nItems) return NULL;
153 for (i = 0; i < menu->nItems; i++, item++)
155 if (item->item_id == *nPos)
160 else if (item->item_flags & MF_POPUP)
162 HMENU hsubmenu = (HMENU)item->item_id;
163 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
176 /***********************************************************************
177 * MENU_FindItemByCoords
179 * Find the item at the specified coordinates (screen coords).
181 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu, int x, int y, WORD *pos )
187 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return NULL;
188 x -= wndPtr->rectWindow.left;
189 y -= wndPtr->rectWindow.top;
190 item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
191 for (i = 0; i < menu->nItems; i++, item++)
193 if ((x >= item->rect.left) && (x < item->rect.right) &&
194 (y >= item->rect.top) && (y < item->rect.bottom))
204 /***********************************************************************
207 * Find the menu item selected by a key press.
208 * Return item id, -1 if none, -2 if we should close the menu.
210 static WORD MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu, WORD key )
217 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
218 lpitem = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
220 for (i = 0; i < menu->nItems; i++, lpitem++)
222 if (IS_STRING_ITEM(lpitem->item_flags))
224 char *p = strchr( lpitem->item_text, '&' );
225 if (p && (p[1] != '&') && (toupper(p[1]) == key)) return i;
228 menuchar = SendMessage( hwndOwner, WM_MENUCHAR, key,
229 MAKELONG( menu->wFlags, hmenu ) );
230 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
231 if (HIWORD(menuchar) == 1) return -2;
236 /***********************************************************************
239 * Calculate the size of the menu item and store it in lpitem->rect.
241 static void MENU_CalcItemSize( HDC hdc, LPMENUITEM lpitem, HWND hwndOwner,
242 int orgX, int orgY, BOOL menuBar )
247 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
250 if (lpitem->item_flags & MF_SEPARATOR)
252 lpitem->rect.bottom += SEPARATOR_HEIGHT;
258 lpitem->rect.right += 2 * check_bitmap_width;
259 if (lpitem->item_flags & MF_POPUP)
260 lpitem->rect.right += arrow_bitmap_width;
263 if (lpitem->item_flags & MF_BITMAP)
266 GetObject( (HBITMAP)lpitem->hText, sizeof(BITMAP), (LPSTR)&bm );
267 lpitem->rect.right += bm.bmWidth;
268 lpitem->rect.bottom += bm.bmHeight;
272 /* If we get here, then it is a text item */
274 dwSize = (lpitem->item_text == NULL) ? 0 : GetTextExtent( hdc, lpitem->item_text, strlen(lpitem->item_text));
275 lpitem->rect.right += LOWORD(dwSize);
276 lpitem->rect.bottom += max( HIWORD(dwSize), SYSMETRICS_CYMENU );
278 if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
279 else if ((p = strchr( lpitem->item_text, '\t' )) != NULL)
281 /* Item contains a tab (only meaningful in popup menus) */
282 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE +
283 LOWORD( GetTextExtent( hdc, lpitem->item_text,
284 (int)(p - lpitem->item_text) ));
285 lpitem->rect.right += MENU_TAB_SPACE;
289 if (strchr( lpitem->item_text, '\b' ))
290 lpitem->rect.right += MENU_TAB_SPACE;
291 lpitem->xTab = lpitem->rect.right - check_bitmap_width
292 - arrow_bitmap_width;
297 /***********************************************************************
298 * MENU_PopupMenuCalcSize
300 * Calculate the size of a popup menu.
302 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
304 LPMENUITEM items, lpitem;
307 int orgX, orgY, maxX, maxTab, maxTabWidth;
309 lppop->Width = lppop->Height = 0;
310 if (lppop->nItems == 0) return;
311 items = (MENUITEM *)USER_HEAP_ADDR( lppop->hItems );
314 while (start < lppop->nItems)
316 lpitem = &items[start];
319 maxTab = maxTabWidth = 0;
321 /* Parse items until column break or end of menu */
322 for (i = start; i < lppop->nItems; i++, lpitem++)
325 (lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
326 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
327 maxX = max( maxX, lpitem->rect.right );
328 orgY = lpitem->rect.bottom;
331 maxTab = max( maxTab, lpitem->xTab );
332 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
336 /* Finish the column (set all items to the largest width found) */
337 maxX = max( maxX, maxTab + maxTabWidth );
338 for (lpitem = &items[start]; start < i; start++, lpitem++)
340 lpitem->rect.right = maxX;
341 if (lpitem->xTab) lpitem->xTab = maxTab;
343 lppop->Height = max( lppop->Height, orgY );
351 /***********************************************************************
352 * MENU_MenuBarCalcSize
354 * Calculate the size of the menu bar.
356 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect, LPPOPUPMENU lppop,
359 LPMENUITEM lpitem, items;
360 int start, i, orgX, orgY, maxY, helpPos;
362 if ((lprect == NULL) || (lppop == NULL)) return;
363 if (lppop->nItems == 0) return;
364 dprintf_menucalc(stddeb,"MenuBarCalcSize left=%d top=%d right=%d bottom=%d !\n",
365 lprect->left, lprect->top, lprect->right, lprect->bottom);
366 items = (MENUITEM *)USER_HEAP_ADDR( lppop->hItems );
367 lppop->Width = lprect->right - lprect->left;
372 while (start < lppop->nItems)
374 lpitem = &items[start];
378 /* Parse items until line break or end of menu */
379 for (i = start; i < lppop->nItems; i++, lpitem++)
381 if ((helpPos == -1) && (lpitem->item_flags & MF_HELP)) helpPos = i;
383 (lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
384 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
385 if (lpitem->rect.right > lprect->right)
387 if (i != start) break;
388 else lpitem->rect.right = lprect->right;
390 maxY = max( maxY, lpitem->rect.bottom );
391 orgX = lpitem->rect.right;
394 /* Finish the line (set all items to the largest height found) */
395 while (start < i) items[start++].rect.bottom = maxY;
398 lprect->bottom = maxY;
399 lppop->Height = lprect->bottom - lprect->top;
401 /* Flush right all items between the MF_HELP and the last item */
402 /* (if several lines, only move the last line) */
405 lpitem = &items[lppop->nItems-1];
406 orgY = lpitem->rect.top;
407 orgX = lprect->right;
408 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
410 if (lpitem->rect.top != orgY) break; /* Other line */
411 if (lpitem->rect.right >= orgX) break; /* Too far right already */
412 lpitem->rect.left += orgX - lpitem->rect.right;
413 lpitem->rect.right = orgX;
414 orgX = lpitem->rect.left;
420 /***********************************************************************
423 * Draw a single menu item.
425 static void MENU_DrawMenuItem( HDC hdc, LPMENUITEM lpitem,
426 WORD height, BOOL menuBar )
430 if (menuBar && (lpitem->item_flags & MF_SEPARATOR)) return;
433 /* Draw the background */
435 if (lpitem->item_flags & MF_HILITE)
436 FillRect( hdc, &rect, sysColorObjects.hbrushHighlight );
437 else FillRect( hdc, &rect, sysColorObjects.hbrushMenu );
438 SetBkMode( hdc, TRANSPARENT );
440 /* Draw the separator bar (if any) */
442 if (!menuBar && (lpitem->item_flags & MF_MENUBARBREAK))
444 SelectObject( hdc, sysColorObjects.hpenWindowFrame );
445 MoveTo( hdc, rect.left, 0 );
446 LineTo( hdc, rect.left, height );
448 if (lpitem->item_flags & MF_SEPARATOR)
450 SelectObject( hdc, sysColorObjects.hpenWindowFrame );
451 MoveTo( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
452 LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
458 if (lpitem->item_flags & MF_HILITE)
460 if (lpitem->item_flags & MF_GRAYED)
461 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
463 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
464 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
468 if (lpitem->item_flags & MF_GRAYED)
469 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
471 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
472 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
477 /* Draw the check mark */
479 if (lpitem->item_flags & MF_CHECKED)
481 GRAPH_DrawBitmap(hdc, lpitem->hCheckBit ? lpitem->hCheckBit :
482 hStdCheck, rect.left,
483 (rect.top+rect.bottom-check_bitmap_height) / 2,
484 0, 0, check_bitmap_width, check_bitmap_height );
486 else if (lpitem->hUnCheckBit != 0) /* Not checked */
488 GRAPH_DrawBitmap(hdc, lpitem->hUnCheckBit, rect.left,
489 (rect.top+rect.bottom-check_bitmap_height) / 2,
490 0, 0, check_bitmap_width, check_bitmap_height );
493 /* Draw the popup-menu arrow */
495 if (lpitem->item_flags & MF_POPUP)
497 GRAPH_DrawBitmap( hdc, hStdMnArrow,
498 rect.right-arrow_bitmap_width-1,
499 (rect.top+rect.bottom-arrow_bitmap_height) / 2,
500 0, 0, arrow_bitmap_width, arrow_bitmap_height );
503 rect.left += check_bitmap_width;
504 rect.right -= arrow_bitmap_width;
507 /* Draw the item text or bitmap */
509 if (lpitem->item_flags & MF_BITMAP)
511 GRAPH_DrawBitmap( hdc, (HBITMAP)lpitem->hText, rect.left, rect.top,
512 0, 0, rect.right-rect.left, rect.bottom-rect.top );
515 /* No bitmap - process text if present */
516 else if ((lpitem->item_text) != ((char *) NULL))
522 rect.left += MENU_BAR_ITEMS_SPACE / 2;
523 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
524 i = strlen( lpitem->item_text );
528 for (i = 0; lpitem->item_text[i]; i++)
529 if ((lpitem->item_text[i] == '\t') ||
530 (lpitem->item_text[i] == '\b')) break;
533 DrawText( hdc, lpitem->item_text, i, &rect,
534 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
536 if (lpitem->item_text[i]) /* There's a tab or flush-right char */
538 if (lpitem->item_text[i] == '\t')
540 rect.left = lpitem->xTab;
541 DrawText( hdc, lpitem->item_text + i + 1, -1, &rect,
542 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
544 else DrawText( hdc, lpitem->item_text + i + 1, -1, &rect,
545 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
551 /***********************************************************************
554 * Paint a popup menu.
556 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
563 GetClientRect( hwnd, &rect );
564 FillRect( hdc, &rect, sysColorObjects.hbrushMenu );
565 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
566 if (!menu || !menu->nItems) return;
567 item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
568 for (i = menu->nItems; i > 0; i--, item++)
569 MENU_DrawMenuItem( hdc, item, menu->Height, FALSE );
573 /***********************************************************************
576 * Paint a menu bar. Returns the height of the menu bar.
578 WORD MENU_DrawMenuBar(HDC hDC, LPRECT lprect, HWND hwnd, BOOL suppress_draw)
583 WND *wndPtr = WIN_FindWndPtr( hwnd );
585 lppop = (LPPOPUPMENU) USER_HEAP_ADDR( wndPtr->wIDmenu );
586 if (lppop == NULL || lprect == NULL) return SYSMETRICS_CYMENU;
587 dprintf_menu(stddeb,"MENU_DrawMenuBar(%04X, %p, %p); !\n",
589 if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
590 lprect->bottom = lprect->top + lppop->Height;
591 if (suppress_draw) return lppop->Height;
593 FillRect(hDC, lprect, sysColorObjects.hbrushMenu );
594 SelectObject( hDC, sysColorObjects.hpenWindowFrame );
595 MoveTo( hDC, lprect->left, lprect->bottom );
596 LineTo( hDC, lprect->right, lprect->bottom );
598 if (lppop->nItems == 0) return SYSMETRICS_CYMENU;
599 lpitem = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
600 for (i = 0; i < lppop->nItems; i++, lpitem++)
602 MENU_DrawMenuItem( hDC, lpitem, lppop->Height, TRUE );
604 return lppop->Height;
608 /***********************************************************************
611 * Display a popup menu.
613 static BOOL MENU_ShowPopup(HWND hwndOwner, HMENU hmenu, WORD id, int x, int y)
617 if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return FALSE;
618 if (menu->FocusedItem != NO_SELECTED_ITEM)
620 MENUITEM *item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
621 item[menu->FocusedItem].item_flags &= ~(MF_HILITE | MF_MOUSESELECT);
622 menu->FocusedItem = NO_SELECTED_ITEM;
624 SendMessage( hwndOwner, WM_INITMENUPOPUP, hmenu,
625 MAKELONG( id, (menu->wFlags & MF_POPUP) ? 1 : 0 ));
626 MENU_PopupMenuCalcSize( menu, hwndOwner );
629 WND *wndPtr = WIN_FindWndPtr( hwndOwner );
630 if (!wndPtr) return FALSE;
631 menu->hWnd = CreateWindow( POPUPMENU_CLASS_NAME, "",
632 WS_POPUP | WS_BORDER, x, y,
633 menu->Width + 2*SYSMETRICS_CXBORDER,
634 menu->Height + 2*SYSMETRICS_CYBORDER,
635 0, 0, wndPtr->hInstance,
636 (LPSTR)(DWORD)hmenu );
637 if (!menu->hWnd) return FALSE;
639 else SetWindowPos( menu->hWnd, 0, x, y,
640 menu->Width + 2*SYSMETRICS_CXBORDER,
641 menu->Height + 2*SYSMETRICS_CYBORDER,
642 SWP_NOACTIVATE | SWP_NOZORDER );
644 /* Display the window */
646 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
647 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
648 UpdateWindow( menu->hWnd );
653 /***********************************************************************
656 static void MENU_SelectItem( HMENU hmenu, WORD wIndex )
662 lppop = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
663 if (!lppop->nItems) return;
664 items = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
665 if ((wIndex != NO_SELECTED_ITEM) &&
666 (wIndex != SYSMENU_SELECTED) &&
667 (items[wIndex].item_flags & MF_SEPARATOR))
668 wIndex = NO_SELECTED_ITEM;
669 if (lppop->FocusedItem == wIndex) return;
670 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
671 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
673 /* Clear previous highlighted item */
674 if (lppop->FocusedItem != NO_SELECTED_ITEM)
676 if (lppop->FocusedItem == SYSMENU_SELECTED)
677 NC_DrawSysButton( lppop->hWnd, hdc, FALSE );
680 items[lppop->FocusedItem].item_flags &=~(MF_HILITE|MF_MOUSESELECT);
681 MENU_DrawMenuItem( hdc, &items[lppop->FocusedItem], lppop->Height,
682 !(lppop->wFlags & MF_POPUP) );
686 /* Highlight new item (if any) */
687 lppop->FocusedItem = wIndex;
688 if (lppop->FocusedItem != NO_SELECTED_ITEM)
690 if (lppop->FocusedItem == SYSMENU_SELECTED)
691 NC_DrawSysButton( lppop->hWnd, hdc, TRUE );
694 items[lppop->FocusedItem].item_flags |= MF_HILITE;
695 MENU_DrawMenuItem( hdc, &items[lppop->FocusedItem], lppop->Height,
696 !(lppop->wFlags & MF_POPUP) );
697 SendMessage(lppop->hWnd, WM_MENUSELECT,
698 items[lppop->FocusedItem].item_id,
699 MAKELONG( hmenu, items[lppop->FocusedItem].item_flags));
702 ReleaseDC( lppop->hWnd, hdc );
706 /***********************************************************************
707 * MENU_SelectNextItem
709 static void MENU_SelectNextItem( HMENU hmenu )
715 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
716 if (!menu->nItems) return;
717 items = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
718 if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
719 (menu->FocusedItem != SYSMENU_SELECTED))
721 for (i = menu->FocusedItem+1; i < menu->nItems; i++)
723 if (!(items[i].item_flags & MF_SEPARATOR))
725 MENU_SelectItem( hmenu, i );
729 if (MENU_HasSysMenu( menu ))
731 MENU_SelectItem( hmenu, SYSMENU_SELECTED );
735 for (i = 0; i < menu->nItems; i++)
737 if (!(items[i].item_flags & MF_SEPARATOR))
739 MENU_SelectItem( hmenu, i );
743 if (MENU_HasSysMenu( menu )) MENU_SelectItem( hmenu, SYSMENU_SELECTED );
747 /***********************************************************************
748 * MENU_SelectPrevItem
750 static void MENU_SelectPrevItem( HMENU hmenu )
756 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
757 if (!menu->nItems) return;
758 items = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
759 if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
760 (menu->FocusedItem != SYSMENU_SELECTED))
762 for (i = menu->FocusedItem - 1; i >= 0; i--)
764 if (!(items[i].item_flags & MF_SEPARATOR))
766 MENU_SelectItem( hmenu, i );
770 if (MENU_HasSysMenu( menu ))
772 MENU_SelectItem( hmenu, SYSMENU_SELECTED );
776 for (i = menu->nItems - 1; i > 0; i--)
778 if (!(items[i].item_flags & MF_SEPARATOR))
780 MENU_SelectItem( hmenu, i );
784 if (MENU_HasSysMenu( menu )) MENU_SelectItem( hmenu, SYSMENU_SELECTED );
788 /***********************************************************************
791 * Return the handle of the selected sub-popup menu (if any).
793 static HMENU MENU_GetSubPopup( HMENU hmenu )
798 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
799 if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
800 else if (menu->FocusedItem == SYSMENU_SELECTED)
801 return GetSystemMenu( menu->hWnd, FALSE );
803 item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
804 if (!(item->item_flags & MF_POPUP) || !(item->item_flags & MF_MOUSESELECT))
806 return item->item_id;
810 /***********************************************************************
813 * Hide the sub-popup menus of this menu.
815 static void MENU_HideSubPopups( HMENU hmenu )
818 POPUPMENU *menu, *submenu;
821 if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return;
822 if (menu->FocusedItem == NO_SELECTED_ITEM) return;
823 if (menu->FocusedItem == SYSMENU_SELECTED)
825 hsubmenu = GetSystemMenu( menu->hWnd, FALSE );
829 item = ((MENUITEM *)USER_HEAP_ADDR(menu->hItems)) + menu->FocusedItem;
830 if (!(item->item_flags & MF_POPUP) ||
831 !(item->item_flags & MF_MOUSESELECT)) return;
832 item->item_flags &= ~MF_MOUSESELECT;
833 hsubmenu = item->item_id;
835 submenu = (POPUPMENU *) USER_HEAP_ADDR( hsubmenu );
836 MENU_HideSubPopups( hsubmenu );
837 if (submenu->hWnd) ShowWindow( submenu->hWnd, SW_HIDE );
838 MENU_SelectItem( hsubmenu, NO_SELECTED_ITEM );
842 /***********************************************************************
845 * Display the sub-menu of the selected item of this menu.
846 * Return the handle of the submenu, or hmenu if no submenu to display.
848 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu, BOOL selectFirst )
854 if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return hmenu;
855 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return hmenu;
856 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
857 if (menu->FocusedItem == SYSMENU_SELECTED)
859 MENU_ShowPopup(hwndOwner, wndPtr->hSysMenu, 0, wndPtr->rectClient.left,
860 wndPtr->rectClient.top - menu->Height - 2*SYSMETRICS_CYBORDER);
861 if (selectFirst) MENU_SelectNextItem( wndPtr->hSysMenu );
862 return wndPtr->hSysMenu;
864 item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
865 if (!(item->item_flags & MF_POPUP) ||
866 (item->item_flags & (MF_GRAYED | MF_DISABLED))) return hmenu;
867 item->item_flags |= MF_MOUSESELECT;
868 if (menu->wFlags & MF_POPUP)
870 MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
871 wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width,
872 wndPtr->rectWindow.top + item->rect.top );
876 MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
877 wndPtr->rectWindow.left + item->rect.left,
878 wndPtr->rectWindow.top + item->rect.bottom );
880 if (selectFirst) MENU_SelectNextItem( (HMENU)item->item_id );
881 return (HMENU)item->item_id;
885 /***********************************************************************
886 * MENU_FindMenuByCoords
888 * Find the menu containing a given point (in screen coords).
890 static HMENU MENU_FindMenuByCoords( HMENU hmenu, POINT pt )
895 if (!(hwnd = WindowFromPoint( pt ))) return 0;
898 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
899 if (menu->hWnd == hwnd)
901 if (!(menu->wFlags & MF_POPUP))
903 /* Make sure it's in the menu bar (or in system menu) */
904 WND *wndPtr = WIN_FindWndPtr( menu->hWnd );
905 if ((pt.x < wndPtr->rectClient.left) ||
906 (pt.x >= wndPtr->rectClient.right) ||
907 (pt.y >= wndPtr->rectClient.top)) return 0;
908 if (pt.y < wndPtr->rectClient.top - menu->Height)
910 if (!MENU_IsInSysMenu( menu, pt )) return 0;
912 /* else it's in the menu bar */
916 hmenu = MENU_GetSubPopup( hmenu );
922 /***********************************************************************
923 * MENU_ExecFocusedItem
925 * Execute a menu item (for instance when user pressed Enter).
926 * Return TRUE if we can go on with menu tracking.
928 static BOOL MENU_ExecFocusedItem( HWND hwndOwner, HMENU hmenu,
929 HMENU *hmenuCurrent )
932 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
933 if (!menu || !menu->nItems || (menu->FocusedItem == NO_SELECTED_ITEM) ||
934 (menu->FocusedItem == SYSMENU_SELECTED)) return TRUE;
935 item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
936 if (!(item->item_flags & MF_POPUP))
938 if (!(item->item_flags & (MF_GRAYED | MF_DISABLED)))
940 PostMessage( hwndOwner, (menu->wFlags & MF_SYSMENU) ?
941 WM_SYSCOMMAND : WM_COMMAND, item->item_id, 0 );
948 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
954 /***********************************************************************
957 * Handle a button-down event in a menu. Point is in screen coords.
958 * hmenuCurrent is the top-most visible popup.
959 * Return TRUE if we can go on with menu tracking.
961 static BOOL MENU_ButtonDown( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
968 if (!hmenu) return FALSE; /* Outside all menus */
969 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
970 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
971 if (!item) /* Maybe in system menu */
973 if (!MENU_IsInSysMenu( menu, pt )) return FALSE;
974 id = SYSMENU_SELECTED;
977 if (menu->FocusedItem == id)
979 if (id == SYSMENU_SELECTED) return FALSE;
980 if (item->item_flags & MF_POPUP)
982 if (item->item_flags & MF_MOUSESELECT)
984 if (menu->wFlags & MF_POPUP)
986 MENU_HideSubPopups( hmenu );
987 *hmenuCurrent = hmenu;
991 else *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
996 MENU_HideSubPopups( hmenu );
997 MENU_SelectItem( hmenu, id );
998 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
1004 /***********************************************************************
1007 * Handle a button-up event in a menu. Point is in screen coords.
1008 * hmenuCurrent is the top-most visible popup.
1009 * Return TRUE if we can go on with menu tracking.
1011 static BOOL MENU_ButtonUp( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
1019 if (!hmenu) return FALSE; /* Outside all menus */
1020 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
1021 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
1022 if (!item) /* Maybe in system menu */
1024 if (!MENU_IsInSysMenu( menu, pt )) return FALSE;
1025 id = SYSMENU_SELECTED;
1026 hsubmenu = GetSystemMenu( menu->hWnd, FALSE );
1029 if (menu->FocusedItem != id) return FALSE;
1031 if (id != SYSMENU_SELECTED)
1033 if (!(item->item_flags & MF_POPUP))
1035 return MENU_ExecFocusedItem( hwndOwner, hmenu, hmenuCurrent );
1037 hsubmenu = item->item_id;
1039 /* Select first item of sub-popup */
1040 MENU_SelectItem( hsubmenu, NO_SELECTED_ITEM );
1041 MENU_SelectNextItem( hsubmenu );
1046 /***********************************************************************
1049 * Handle a motion event in a menu. Point is in screen coords.
1050 * hmenuCurrent is the top-most visible popup.
1051 * Return TRUE if we can go on with menu tracking.
1053 static BOOL MENU_MouseMove( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
1057 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
1058 WORD id = NO_SELECTED_ITEM;
1062 item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
1063 if (!item) /* Maybe in system menu */
1065 if (!MENU_IsInSysMenu( menu, pt ))
1066 id = NO_SELECTED_ITEM; /* Outside all items */
1067 else id = SYSMENU_SELECTED;
1070 if (id == NO_SELECTED_ITEM)
1072 MENU_SelectItem( *hmenuCurrent, NO_SELECTED_ITEM );
1074 else if (menu->FocusedItem != id)
1076 MENU_HideSubPopups( hmenu );
1077 MENU_SelectItem( hmenu, id );
1078 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, FALSE );
1084 /***********************************************************************
1087 * Handle a VK_LEFT key event in a menu.
1088 * hmenuCurrent is the top-most visible popup.
1090 static void MENU_KeyLeft( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent )
1093 HMENU hmenutmp, hmenuprev;
1095 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
1096 hmenuprev = hmenutmp = hmenu;
1097 while (hmenutmp != *hmenuCurrent)
1099 hmenutmp = MENU_GetSubPopup( hmenuprev );
1100 if (hmenutmp != *hmenuCurrent) hmenuprev = hmenutmp;
1102 MENU_HideSubPopups( hmenuprev );
1104 if ((hmenuprev == hmenu) && !(menu->wFlags & MF_POPUP))
1106 /* Select previous item on the menu bar */
1107 MENU_SelectPrevItem( hmenu );
1108 if (*hmenuCurrent != hmenu)
1110 /* A popup menu was displayed -> display the next one */
1111 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
1114 else *hmenuCurrent = hmenuprev;
1118 /***********************************************************************
1121 * Handle a VK_RIGHT key event in a menu.
1122 * hmenuCurrent is the top-most visible popup.
1124 static void MENU_KeyRight( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent )
1129 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
1131 if ((menu->wFlags & MF_POPUP) || (*hmenuCurrent != hmenu))
1133 /* If already displaying a popup, try to display sub-popup */
1134 hmenutmp = MENU_ShowSubPopup( hwndOwner, *hmenuCurrent, TRUE );
1135 if (hmenutmp != *hmenuCurrent) /* Sub-popup displayed */
1137 *hmenuCurrent = hmenutmp;
1142 /* If on menu-bar, go to next item */
1143 if (!(menu->wFlags & MF_POPUP))
1145 MENU_HideSubPopups( hmenu );
1146 MENU_SelectNextItem( hmenu );
1147 if (*hmenuCurrent != hmenu)
1149 /* A popup menu was displayed -> display the next one */
1150 *hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
1153 else if (*hmenuCurrent != hmenu) /* Hide last level popup */
1156 hmenuprev = hmenutmp = hmenu;
1157 while (hmenutmp != *hmenuCurrent)
1159 hmenutmp = MENU_GetSubPopup( hmenuprev );
1160 if (hmenutmp != *hmenuCurrent) hmenuprev = hmenutmp;
1162 MENU_HideSubPopups( hmenuprev );
1163 *hmenuCurrent = hmenuprev;
1168 /***********************************************************************
1171 * Menu tracking code.
1172 * If 'x' and 'y' are not 0, we simulate a button-down event at (x,y)
1173 * before beginning tracking. This is to help menu-bar tracking.
1175 static BOOL MENU_TrackMenu( HMENU hmenu, WORD wFlags, int x, int y,
1176 HWND hwnd, LPRECT lprect )
1180 HMENU hmenuCurrent = hmenu;
1181 BOOL fClosed = FALSE;
1184 fEndMenuCalled = FALSE;
1185 if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return FALSE;
1188 POINT pt = { x, y };
1189 MENU_ButtonDown( hwnd, hmenu, &hmenuCurrent, pt );
1195 if (!MSG_InternalGetMessage( &msg, 0, hwnd, MSGF_MENU, 0, TRUE ))
1198 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
1200 /* Find the sub-popup for this mouse event (if any) */
1201 HMENU hsubmenu = MENU_FindMenuByCoords( hmenu, msg.pt );
1205 case WM_RBUTTONDOWN:
1206 case WM_NCRBUTTONDOWN:
1207 if (!(wFlags & TPM_RIGHTBUTTON)) break;
1209 case WM_LBUTTONDOWN:
1210 case WM_NCLBUTTONDOWN:
1211 fClosed = !MENU_ButtonDown( hwnd, hsubmenu,
1212 &hmenuCurrent, msg.pt );
1216 case WM_NCRBUTTONUP:
1217 if (!(wFlags & TPM_RIGHTBUTTON)) break;
1220 case WM_NCLBUTTONUP:
1221 /* If outside all menus but inside lprect, ignore it */
1222 if (!hsubmenu && lprect && PtInRect( lprect, msg.pt )) break;
1223 fClosed = !MENU_ButtonUp( hwnd, hsubmenu,
1224 &hmenuCurrent, msg.pt );
1228 case WM_NCMOUSEMOVE:
1229 if ((msg.wParam & MK_LBUTTON) ||
1230 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON)))
1232 fClosed = !MENU_MouseMove( hwnd, hsubmenu,
1233 &hmenuCurrent, msg.pt );
1238 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
1246 MENU_SelectItem( hmenuCurrent, NO_SELECTED_ITEM );
1247 MENU_SelectNextItem( hmenuCurrent );
1251 MENU_SelectItem( hmenuCurrent, NO_SELECTED_ITEM );
1252 MENU_SelectPrevItem( hmenuCurrent );
1256 MENU_SelectPrevItem( hmenuCurrent );
1260 /* If on menu bar, pull-down the menu */
1261 if (!(menu->wFlags & MF_POPUP) && (hmenuCurrent == hmenu))
1262 hmenuCurrent = MENU_ShowSubPopup( hwnd, hmenu, TRUE );
1264 MENU_SelectNextItem( hmenuCurrent );
1268 MENU_KeyLeft( hwnd, hmenu, &hmenuCurrent );
1272 MENU_KeyRight( hwnd, hmenu, &hmenuCurrent );
1277 fClosed = !MENU_ExecFocusedItem( hwnd, hmenuCurrent,
1288 break; /* WM_KEYDOWN */
1298 break; /* WM_SYSKEYDOWN */
1302 /* Hack to avoid control chars. */
1303 /* We will find a better way real soon... */
1304 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
1305 pos = MENU_FindItemByKey( hwnd, hmenuCurrent, msg.wParam );
1306 if (pos == (WORD)-2) fClosed = TRUE;
1307 else if (pos == (WORD)-1) MessageBeep(0);
1310 MENU_SelectItem( hmenuCurrent, pos );
1311 fClosed = !MENU_ExecFocusedItem( hwnd, hmenuCurrent,
1316 break; /* WM_CHAR */
1317 } /* switch(msg.message) */
1321 DispatchMessage( &msg );
1323 if (fEndMenuCalled) fClosed = TRUE;
1325 if (!fClosed) /* Remove the message from the queue */
1326 PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
1329 MENU_HideSubPopups( hmenu );
1330 if (menu->wFlags & MF_POPUP) ShowWindow( menu->hWnd, SW_HIDE );
1331 MENU_SelectItem( hmenu, NO_SELECTED_ITEM );
1332 fEndMenuCalled = FALSE;
1337 /***********************************************************************
1338 * MENU_TrackMouseMenuBar
1340 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
1342 void MENU_TrackMouseMenuBar( HWND hwnd, POINT pt )
1344 WND *wndPtr = WIN_FindWndPtr( hwnd );
1345 SendMessage( hwnd, WM_ENTERMENULOOP, 0, 0 );
1346 MENU_TrackMenu( (HMENU)wndPtr->wIDmenu, TPM_LEFTALIGN | TPM_LEFTBUTTON,
1347 pt.x, pt.y, hwnd, NULL );
1348 SendMessage( hwnd, WM_EXITMENULOOP, 0, 0 );
1352 /***********************************************************************
1353 * MENU_TrackKbdMenuBar
1355 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
1357 void MENU_TrackKbdMenuBar( HWND hwnd, WORD wParam )
1359 WND *wndPtr = WIN_FindWndPtr( hwnd );
1360 SendMessage( hwnd, WM_ENTERMENULOOP, 0, 0 );
1361 /* Select first selectable item */
1362 MENU_SelectItem( wndPtr->wIDmenu, NO_SELECTED_ITEM );
1363 MENU_SelectNextItem( (HMENU)wndPtr->wIDmenu );
1364 MENU_TrackMenu( (HMENU)wndPtr->wIDmenu, TPM_LEFTALIGN | TPM_LEFTBUTTON,
1366 SendMessage( hwnd, WM_EXITMENULOOP, 0, 0 );
1370 /**********************************************************************
1371 * TrackPopupMenu [USER.416]
1373 BOOL TrackPopupMenu( HMENU hMenu, WORD wFlags, short x, short y,
1374 short nReserved, HWND hWnd, LPRECT lpRect )
1376 if (!MENU_ShowPopup( hWnd, hMenu, 0, x, y )) return FALSE;
1377 return MENU_TrackMenu( hMenu, wFlags, 0, 0, hWnd, lpRect );
1381 /***********************************************************************
1384 LONG PopupMenuWndProc( HWND hwnd, WORD message, WORD wParam, LONG lParam )
1390 CREATESTRUCT *createStruct = (CREATESTRUCT *)lParam;
1391 HMENU hmenu = (HMENU) ((int)createStruct->lpCreateParams & 0xffff);
1392 SetWindowWord( hwnd, 0, hmenu );
1396 case WM_MOUSEACTIVATE: /* We don't want to be activated */
1397 return MA_NOACTIVATE;
1402 BeginPaint( hwnd, &ps );
1403 MENU_DrawPopupMenu( hwnd, ps.hdc,
1404 (HMENU)GetWindowWord( hwnd, 0 ) );
1405 EndPaint( hwnd, &ps );
1410 return DefWindowProc(hwnd, message, wParam, lParam);
1416 /***********************************************************************
1417 * MENU_GetMenuBarHeight
1419 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
1421 WORD MENU_GetMenuBarHeight( HWND hwnd, WORD menubarWidth, int orgX, int orgY )
1428 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1429 if (!(lppop = (LPPOPUPMENU)USER_HEAP_ADDR( wndPtr->wIDmenu ))) return 0;
1430 hdc = GetDC( hwnd );
1431 SetRect( &rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU );
1432 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
1433 ReleaseDC( hwnd, hdc );
1434 return lppop->Height;
1438 /**********************************************************************
1439 * ChangeMenu [USER.153]
1441 BOOL ChangeMenu(HMENU hMenu, WORD nPos, LPSTR lpNewItem,
1442 WORD wItemID, WORD wFlags)
1444 if (wFlags & MF_APPEND)
1445 return AppendMenu(hMenu, wFlags, wItemID, lpNewItem);
1446 if (wFlags & MF_DELETE)
1447 return DeleteMenu(hMenu, wItemID, wFlags);
1448 if (wFlags & MF_INSERT)
1449 return InsertMenu(hMenu, nPos, wFlags, wItemID, lpNewItem);
1450 if (wFlags & MF_CHANGE)
1451 return ModifyMenu(hMenu, nPos, wFlags, wItemID, lpNewItem);
1452 if (wFlags & MF_REMOVE)
1453 return RemoveMenu(hMenu, wItemID, wFlags);
1458 /**********************************************************************
1459 * CheckMenuItem [USER.154]
1461 BOOL CheckMenuItem(HMENU hMenu, WORD wItemID, WORD wFlags)
1464 dprintf_menu(stddeb,"CheckMenuItem (%04X, %04X, %04X) !\n",
1465 hMenu, wItemID, wFlags);
1466 if (!(lpitem = MENU_FindItem(&hMenu, &wItemID, wFlags))) return FALSE;
1467 if (wFlags & MF_CHECKED) lpitem->item_flags |= MF_CHECKED;
1468 else lpitem->item_flags &= ~MF_CHECKED;
1473 /**********************************************************************
1474 * EnableMenuItem [USER.155]
1476 BOOL EnableMenuItem(HMENU hMenu, WORD wItemID, WORD wFlags)
1479 dprintf_menu(stddeb,"EnableMenuItem (%04X, %04X, %04X) !\n",
1480 hMenu, wItemID, wFlags);
1481 if (!(lpitem = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return FALSE;
1483 /* We can't have MF_GRAYED and MF_DISABLED together */
1484 if (wFlags & MF_GRAYED)
1486 lpitem->item_flags = (lpitem->item_flags & ~MF_DISABLED) | MF_GRAYED;
1488 else if (wFlags & MF_DISABLED)
1490 lpitem->item_flags = (lpitem->item_flags & ~MF_GRAYED) | MF_DISABLED;
1492 else /* MF_ENABLED */
1494 lpitem->item_flags &= ~(MF_GRAYED | MF_DISABLED);
1500 /**********************************************************************
1501 * GetMenuString [USER.161]
1503 int GetMenuString(HMENU hMenu, WORD wItemID,
1504 LPSTR str, short nMaxSiz, WORD wFlags)
1508 dprintf_menu(stddeb,"GetMenuString(%04X, %04X, %p, %d, %04X);\n",
1509 hMenu, wItemID, str, nMaxSiz, wFlags);
1510 if (str == NULL) return FALSE;
1511 lpitem = MENU_FindItem( &hMenu, &wItemID, wFlags );
1512 if (lpitem != NULL) {
1513 if (lpitem->item_text != NULL) {
1514 maxsiz = min(nMaxSiz - 1, strlen(lpitem->item_text));
1515 strncpy(str, lpitem->item_text, maxsiz + 1);
1519 dprintf_menu(stddeb,"GetMenuString // Found !\n");
1526 /**********************************************************************
1527 * HiliteMenuItem [USER.162]
1529 BOOL HiliteMenuItem(HWND hWnd, HMENU hMenu, WORD wItemID, WORD wHilite)
1533 dprintf_menu(stddeb,"HiliteMenuItem(%04X, %04X, %04X, %04X);\n",
1534 hWnd, hMenu, wItemID, wHilite);
1535 if (!(lpitem = MENU_FindItem( &hMenu, &wItemID, wHilite ))) return FALSE;
1536 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return FALSE;
1537 if (menu->FocusedItem == wItemID) return TRUE;
1538 MENU_HideSubPopups( hMenu );
1539 MENU_SelectItem( hMenu, wItemID );
1544 /**********************************************************************
1545 * GetMenuState [USER.250]
1547 WORD GetMenuState(HMENU hMenu, WORD wItemID, WORD wFlags)
1550 dprintf_menu(stddeb,"GetMenuState(%04X, %04X, %04X);\n",
1551 hMenu, wItemID, wFlags);
1552 if (!(lpitem = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
1553 if (lpitem->item_flags & MF_POPUP)
1555 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_ADDR( lpitem->item_id );
1556 if (!menu) return -1;
1557 else return (menu->nItems << 8) | (menu->wFlags & 0xff);
1559 else return lpitem->item_flags;
1563 /**********************************************************************
1564 * GetMenuItemCount [USER.263]
1566 WORD GetMenuItemCount(HMENU hMenu)
1569 dprintf_menu(stddeb,"GetMenuItemCount(%04X);\n", hMenu);
1570 menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu);
1571 if (menu == NULL) return (WORD)-1;
1572 dprintf_menu(stddeb,"GetMenuItemCount(%04X) return %d \n",
1573 hMenu, menu->nItems);
1574 return menu->nItems;
1578 /**********************************************************************
1579 * GetMenuItemID [USER.264]
1581 WORD GetMenuItemID(HMENU hMenu, int nPos)
1586 dprintf_menu(stddeb,"GetMenuItemID(%04X, %d);\n", hMenu, nPos);
1587 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return -1;
1588 if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
1589 item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
1590 if (item[nPos].item_flags & MF_POPUP) return -1;
1591 return item[nPos].item_id;
1595 /**********************************************************************
1596 * InsertMenu [USER.410]
1598 BOOL InsertMenu(HMENU hMenu, WORD nPos, WORD wFlags, WORD wItemID, LPSTR lpNewItem)
1601 MENUITEM *lpitem, *newItems;
1604 if (IS_STRING_ITEM(wFlags))
1606 dprintf_menu(stddeb,"InsertMenu (%04X, %04X, %04X, %04X, '%s') !\n",
1607 hMenu, nPos, wFlags, wItemID, lpNewItem);
1610 dprintf_menu(stddeb,"InsertMenu (%04X, %04X, %04X, %04X, %p) !\n",
1611 hMenu, nPos, wFlags, wItemID, lpNewItem);
1613 /* Find where to insert new item */
1615 if ((wFlags & MF_BYPOSITION) && (nPos == (WORD)-1))
1617 /* Special case: append to menu */
1618 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return FALSE;
1619 nPos = menu->nItems;
1623 if (!MENU_FindItem( &hMenu, &nPos, wFlags )) return FALSE;
1624 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return FALSE;
1627 /* Create new items array */
1629 hNewItems = USER_HEAP_ALLOC( GMEM_MOVEABLE,
1630 sizeof(MENUITEM) * (menu->nItems+1) );
1631 if (!hNewItems) return FALSE;
1632 newItems = (MENUITEM *) USER_HEAP_ADDR( hNewItems );
1633 if (menu->nItems > 0)
1635 /* Copy the old array into the new */
1636 MENUITEM *oldItems = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
1637 if (nPos > 0) memcpy( newItems, oldItems, nPos * sizeof(MENUITEM) );
1638 if (nPos < menu->nItems) memcpy( &newItems[nPos+1], &oldItems[nPos],
1639 (menu->nItems-nPos)*sizeof(MENUITEM) );
1641 USER_HEAP_FREE( menu->hItems );
1643 menu->hItems = hNewItems;
1646 /* Store the new item data */
1648 lpitem = &newItems[nPos];
1649 lpitem->item_flags = wFlags & ~(MF_HILITE | MF_MOUSESELECT);
1650 lpitem->item_id = wItemID;
1652 if (IS_STRING_ITEM(wFlags))
1654 /* Item beginning with a backspace is a help item */
1655 if (lpNewItem[0] == '\b')
1657 lpitem->item_flags |= MF_HELP;
1660 lpitem->hText = USER_HEAP_ALLOC( GMEM_MOVEABLE, strlen(lpNewItem)+1 );
1661 lpitem->item_text = (char *)USER_HEAP_ADDR( lpitem->hText );
1662 strcpy( lpitem->item_text, lpNewItem );
1664 else if (wFlags & MF_BITMAP) lpitem->hText = LOWORD((DWORD)lpNewItem);
1665 else lpitem->item_text = lpNewItem;
1667 if (wFlags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
1668 ((POPUPMENU *)USER_HEAP_ADDR(wItemID))->wFlags |= MF_POPUP;
1670 SetRectEmpty( &lpitem->rect );
1671 lpitem->hCheckBit = hStdCheck;
1672 lpitem->hUnCheckBit = 0;
1677 /**********************************************************************
1678 * AppendMenu [USER.411]
1680 BOOL AppendMenu(HMENU hMenu, WORD wFlags, WORD wItemID, LPSTR lpNewItem)
1682 return InsertMenu( hMenu, -1, wFlags | MF_BYPOSITION, wItemID, lpNewItem );
1686 /**********************************************************************
1687 * RemoveMenu [USER.412]
1689 BOOL RemoveMenu(HMENU hMenu, WORD nPos, WORD wFlags)
1693 dprintf_menu(stddeb,"RemoveMenu (%04X, %04X, %04X) !\n",
1694 hMenu, nPos, wFlags);
1695 if (!(lpitem = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
1696 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return FALSE;
1700 if (IS_STRING_ITEM(lpitem->item_flags)) USER_HEAP_FREE( lpitem->hText );
1701 if (--menu->nItems == 0)
1703 USER_HEAP_FREE( menu->hItems );
1708 while(nPos < menu->nItems)
1710 *lpitem = *(lpitem+1);
1714 menu->hItems = USER_HEAP_REALLOC( menu->hItems,
1715 menu->nItems * sizeof(MENUITEM),
1722 /**********************************************************************
1723 * DeleteMenu [USER.413]
1725 BOOL DeleteMenu(HMENU hMenu, WORD nPos, WORD wFlags)
1727 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
1728 if (!item) return FALSE;
1729 if (item->item_flags & MF_POPUP) DestroyMenu( item->item_id );
1730 /* nPos is now the position of the item */
1731 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
1736 /**********************************************************************
1737 * ModifyMenu [USER.414]
1739 BOOL ModifyMenu(HMENU hMenu, WORD nPos, WORD wFlags, WORD wItemID, LPSTR lpNewItem)
1742 if (IS_STRING_ITEM(wFlags))
1743 dprintf_menu(stddeb,"ModifyMenu (%04X, %04X, %04X, %04X, '%s') !\n",
1744 hMenu, nPos, wFlags, wItemID, lpNewItem);
1746 dprintf_menu(stddeb,"ModifyMenu (%04X, %04X, %04X, %04X, %p) !\n",
1747 hMenu, nPos, wFlags, wItemID, lpNewItem);
1748 if (!(lpitem = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
1750 if (IS_STRING_ITEM(lpitem->item_flags)) USER_HEAP_FREE( lpitem->hText );
1751 lpitem->item_flags = wFlags & ~(MF_HILITE | MF_MOUSESELECT);
1752 lpitem->item_id = wItemID;
1754 if (IS_STRING_ITEM(wFlags))
1756 lpitem->hText = USER_HEAP_ALLOC( GMEM_MOVEABLE, strlen(lpNewItem)+1 );
1757 lpitem->item_text = (char *)USER_HEAP_ADDR( lpitem->hText );
1758 strcpy( lpitem->item_text, lpNewItem );
1760 else if (wFlags & MF_BITMAP) lpitem->hText = LOWORD((DWORD)lpNewItem);
1761 else lpitem->item_text = lpNewItem;
1762 SetRectEmpty( &lpitem->rect );
1767 /**********************************************************************
1768 * CreatePopupMenu [USER.415]
1770 HMENU CreatePopupMenu()
1775 if (!(hmenu = CreateMenu())) return 0;
1776 menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
1777 menu->wFlags |= MF_POPUP;
1782 /**********************************************************************
1783 * GetMenuCheckMarkDimensions [USER.417]
1785 DWORD GetMenuCheckMarkDimensions()
1787 return MAKELONG( check_bitmap_width, check_bitmap_height );
1791 /**********************************************************************
1792 * SetMenuItemBitmaps [USER.418]
1794 BOOL SetMenuItemBitmaps(HMENU hMenu, WORD nPos, WORD wFlags,
1795 HBITMAP hNewCheck, HBITMAP hNewUnCheck)
1798 dprintf_menu(stddeb,"SetMenuItemBitmaps (%04X, %04X, %04X, %04X, %08X) !\n",
1799 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
1800 if (!(lpitem = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
1802 if (!hNewCheck && !hNewUnCheck)
1804 /* If both are NULL, restore default bitmaps */
1805 lpitem->hCheckBit = hStdCheck;
1806 lpitem->hUnCheckBit = 0;
1807 lpitem->item_flags &= ~MF_USECHECKBITMAPS;
1809 else /* Install new bitmaps */
1811 lpitem->hCheckBit = hNewCheck;
1812 lpitem->hUnCheckBit = hNewUnCheck;
1813 lpitem->item_flags |= MF_USECHECKBITMAPS;
1819 /**********************************************************************
1820 * CreateMenu [USER.151]
1826 dprintf_menu(stddeb,"CreateMenu !\n");
1827 if (!(hMenu = USER_HEAP_ALLOC( GMEM_MOVEABLE, sizeof(POPUPMENU) )))
1829 menu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu);
1832 menu->wMagic = MENU_MAGIC;
1839 menu->FocusedItem = NO_SELECTED_ITEM;
1840 dprintf_menu(stddeb,"CreateMenu // return %04X\n", hMenu);
1845 /**********************************************************************
1846 * DestroyMenu [USER.152]
1848 BOOL DestroyMenu(HMENU hMenu)
1851 dprintf_menu(stddeb,"DestroyMenu (%04X) !\n", hMenu);
1852 if (hMenu == 0) return FALSE;
1853 lppop = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu);
1854 if (lppop == NULL) return FALSE;
1855 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd)
1856 DestroyWindow( lppop->hWnd );
1861 MENUITEM *item = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
1862 for (i = lppop->nItems; i > 0; i--, item++)
1864 if (item->item_flags & MF_POPUP)
1865 DestroyMenu( item->item_id );
1867 USER_HEAP_FREE( lppop->hItems );
1869 USER_HEAP_FREE( hMenu );
1870 dprintf_menu(stddeb,"DestroyMenu (%04X) // End !\n", hMenu);
1874 /**********************************************************************
1875 * GetSystemMenu [USER.156]
1877 HMENU GetSystemMenu(HWND hWnd, BOOL bRevert)
1880 wndPtr = WIN_FindWndPtr(hWnd);
1882 return wndPtr->hSysMenu;
1885 DestroyMenu(wndPtr->hSysMenu);
1886 wndPtr->hSysMenu = CopySysMenu();
1888 return wndPtr->hSysMenu;
1891 /**********************************************************************
1892 * SetSystemMenu [USER.280]
1894 BOOL SetSystemMenu(HWND hWnd, HMENU newHmenu)
1898 if ((wndPtr = WIN_FindWndPtr(hWnd)) != NULL) wndPtr->hSysMenu = newHmenu;
1903 /**********************************************************************
1904 * GetMenu [USER.157]
1906 HMENU GetMenu(HWND hWnd)
1908 WND * wndPtr = WIN_FindWndPtr(hWnd);
1909 if (wndPtr == NULL) return 0;
1910 return wndPtr->wIDmenu;
1914 /**********************************************************************
1915 * SetMenu [USER.158]
1917 BOOL SetMenu(HWND hWnd, HMENU hMenu)
1920 WND * wndPtr = WIN_FindWndPtr(hWnd);
1921 if (wndPtr == NULL) {
1922 fprintf(stderr,"SetMenu(%04X, %04X) // Bad window handle !\n",
1926 dprintf_menu(stddeb,"SetMenu(%04X, %04X);\n", hWnd, hMenu);
1927 if (GetCapture() == hWnd) ReleaseCapture();
1928 wndPtr->wIDmenu = hMenu;
1931 lpmenu = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu);
1932 if (lpmenu == NULL) {
1933 fprintf(stderr,"SetMenu(%04X, %04X) // Bad menu handle !\n",
1937 lpmenu->hWnd = hWnd;
1938 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
1939 lpmenu->Height = 0; /* Make sure we recalculate the size */
1941 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
1942 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
1948 /**********************************************************************
1949 * GetSubMenu [USER.159]
1951 HMENU GetSubMenu(HMENU hMenu, short nPos)
1955 dprintf_menu(stddeb,"GetSubMenu (%04X, %04X) !\n", hMenu, nPos);
1956 if (!(lppop = (LPPOPUPMENU) USER_HEAP_ADDR(hMenu))) return 0;
1957 if ((WORD)nPos >= lppop->nItems) return 0;
1958 lpitem = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
1959 if (!(lpitem[nPos].item_flags & MF_POPUP)) return 0;
1960 return lpitem[nPos].item_id;
1964 /**********************************************************************
1965 * DrawMenuBar [USER.160]
1967 void DrawMenuBar(HWND hWnd)
1971 dprintf_menu(stddeb,"DrawMenuBar (%04X)\n", hWnd);
1972 wndPtr = WIN_FindWndPtr(hWnd);
1973 if (wndPtr != NULL && (wndPtr->dwStyle & WS_CHILD) == 0 &&
1974 wndPtr->wIDmenu != 0) {
1975 dprintf_menu(stddeb,"DrawMenuBar wIDmenu=%04X \n",
1977 lppop = (LPPOPUPMENU) USER_HEAP_ADDR(wndPtr->wIDmenu);
1978 if (lppop == NULL) return;
1980 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
1981 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
1982 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
1987 /***********************************************************************
1988 * EndMenu (USER.187)
1992 /* Note: this won't work when we have multiple tasks... */
1993 fEndMenuCalled = TRUE;
1997 /***********************************************************************
1998 * LookupMenuHandle (USER.217)
2000 HMENU LookupMenuHandle( HMENU hmenu, INT id )
2002 if (!MENU_FindItem( &hmenu, &id, MF_BYCOMMAND )) return 0;
2007 /**********************************************************************
2008 * LoadMenuIndirect [USER.220]
2010 HMENU LoadMenuIndirect(LPSTR menu_template)
2013 MENU_HEADER *menu_desc;
2014 dprintf_menu(stddeb,"LoadMenuIndirect: menu_template '%p'\n",
2016 hMenu = CreateMenu();
2017 menu_desc = (MENU_HEADER *)menu_template;
2018 ParseMenuResource((WORD *)(menu_desc + 1), 0, hMenu);
2023 /**********************************************************************
2024 * CopySysMenu (Internal)
2030 extern unsigned char sysres_MENU_SYSMENU[];
2032 hMenu=LoadMenuIndirect(sysres_MENU_SYSMENU);
2034 dprintf_menu(stddeb,"No SYSMENU\n");
2037 menu = (POPUPMENU*) USER_HEAP_ADDR(hMenu);
2038 menu->wFlags |= MF_SYSMENU|MF_POPUP;
2039 dprintf_menu(stddeb,"CopySysMenu hMenu=%04X !\n", hMenu);
2044 /**********************************************************************
2045 * ParseMenuResource (from Resource or Template)
2047 WORD * ParseMenuResource(WORD *first_item, int level, HMENU hMenu)
2055 next_item = first_item;
2060 if (*item & MF_POPUP) {
2061 MENU_POPUPITEM *popup_item = (MENU_POPUPITEM *) item;
2062 next_item = (WORD *) (popup_item->item_text +
2063 strlen(popup_item->item_text) + 1);
2064 hSubMenu = CreatePopupMenu();
2065 next_item = ParseMenuResource(next_item, level, hSubMenu);
2066 AppendMenu(hMenu, popup_item->item_flags,
2067 hSubMenu, popup_item->item_text);
2070 MENUITEMTEMPLATE *normal_item = (MENUITEMTEMPLATE *) item;
2071 next_item = (WORD *) (normal_item->item_text +
2072 strlen(normal_item->item_text) + 1);
2073 if (strlen(normal_item->item_text) == 0 && normal_item->item_id == 0)
2074 normal_item->item_flags |= MF_SEPARATOR;
2075 AppendMenu(hMenu, normal_item->item_flags,
2076 normal_item->item_id, normal_item->item_text);
2079 while (!(*item & MF_END));
2084 /**********************************************************************
2087 BOOL IsMenu( HMENU hmenu )
2090 if (!(menu = (LPPOPUPMENU) USER_HEAP_ADDR( hmenu ))) return FALSE;
2091 return (menu->wMagic == MENU_MAGIC);