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
= LoadBitmap16( 0, MAKEINTRESOURCE(OBM_CHECK
) )))
141 GetObject16( hStdCheck
, sizeof(bm
), &bm
);
142 check_bitmap_width
= bm
.bmWidth
;
143 check_bitmap_height
= bm
.bmHeight
;
144 if (!(hStdMnArrow
= LoadBitmap16( 0, MAKEINTRESOURCE(OBM_MNARROW
) )))
146 GetObject16( hStdMnArrow
, sizeof(bm
), &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
;
306 menuchar
= SendMessage32A( hwndOwner
, WM_MENUCHAR
,
307 MAKEWPARAM( key
, menu
->wFlags
), hmenu
);
308 if (HIWORD(menuchar
) == 2) return LOWORD(menuchar
);
309 if (HIWORD(menuchar
) == 1) return -2;
314 /***********************************************************************
317 * Calculate the size of the menu item and store it in lpitem->rect.
319 static void MENU_CalcItemSize( HDC hdc
, MENUITEM
*lpitem
, HWND hwndOwner
,
320 int orgX
, int orgY
, BOOL menuBar
)
325 SetRect16( &lpitem
->rect
, orgX
, orgY
, orgX
, orgY
);
327 if (lpitem
->item_flags
& MF_OWNERDRAW
)
329 MEASUREITEMSTRUCT16
*mis
;
330 if (!(mis
= SEGPTR_NEW(MEASUREITEMSTRUCT16
))) return;
331 mis
->CtlType
= ODT_MENU
;
332 mis
->itemID
= lpitem
->item_id
;
333 mis
->itemData
= (DWORD
)lpitem
->text
;
334 mis
->itemHeight
= 16;
336 SendMessage16( hwndOwner
, WM_MEASUREITEM
, 0, (LPARAM
)SEGPTR_GET(mis
) );
337 lpitem
->rect
.bottom
+= mis
->itemHeight
;
338 lpitem
->rect
.right
+= mis
->itemWidth
;
339 dprintf_menu( stddeb
, "DrawMenuItem: MeasureItem %04x %dx%d!\n",
340 lpitem
->item_id
, mis
->itemWidth
, mis
->itemHeight
);
345 if (lpitem
->item_flags
& MF_SEPARATOR
)
347 lpitem
->rect
.bottom
+= SEPARATOR_HEIGHT
;
353 lpitem
->rect
.right
+= 2 * check_bitmap_width
;
354 if (lpitem
->item_flags
& MF_POPUP
)
355 lpitem
->rect
.right
+= arrow_bitmap_width
;
358 if (lpitem
->item_flags
& MF_BITMAP
)
361 if (GetObject16( (HBITMAP16
)(UINT32
)lpitem
->text
, sizeof(bm
), &bm
))
363 lpitem
->rect
.right
+= bm
.bmWidth
;
364 lpitem
->rect
.bottom
+= bm
.bmHeight
;
369 /* If we get here, then it must be a text item */
371 if (IS_STRING_ITEM( lpitem
->item_flags
))
373 dwSize
= GetTextExtent( hdc
, lpitem
->text
, strlen(lpitem
->text
) );
374 lpitem
->rect
.right
+= LOWORD(dwSize
);
375 lpitem
->rect
.bottom
+= MAX( HIWORD(dwSize
), SYSMETRICS_CYMENU
);
378 if (menuBar
) lpitem
->rect
.right
+= MENU_BAR_ITEMS_SPACE
;
379 else if ((p
= strchr( lpitem
->text
, '\t' )) != NULL
)
381 /* Item contains a tab (only meaningful in popup menus) */
382 lpitem
->xTab
= check_bitmap_width
+ MENU_TAB_SPACE
+
383 LOWORD( GetTextExtent( hdc
, lpitem
->text
,
384 (int)(p
- lpitem
->text
) ));
385 lpitem
->rect
.right
+= MENU_TAB_SPACE
;
389 if (strchr( lpitem
->text
, '\b' ))
390 lpitem
->rect
.right
+= MENU_TAB_SPACE
;
391 lpitem
->xTab
= lpitem
->rect
.right
- check_bitmap_width
392 - arrow_bitmap_width
;
398 /***********************************************************************
399 * MENU_PopupMenuCalcSize
401 * Calculate the size of a popup menu.
403 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop
, HWND hwndOwner
)
408 int orgX
, orgY
, maxX
, maxTab
, maxTabWidth
;
410 lppop
->Width
= lppop
->Height
= 0;
411 if (lppop
->nItems
== 0) return;
414 while (start
< lppop
->nItems
)
416 lpitem
= &lppop
->items
[start
];
419 maxTab
= maxTabWidth
= 0;
421 /* Parse items until column break or end of menu */
422 for (i
= start
; i
< lppop
->nItems
; i
++, lpitem
++)
425 (lpitem
->item_flags
& (MF_MENUBREAK
| MF_MENUBARBREAK
))) break;
426 MENU_CalcItemSize( hdc
, lpitem
, hwndOwner
, orgX
, orgY
, FALSE
);
427 if (lpitem
->item_flags
& MF_MENUBARBREAK
) orgX
++;
428 maxX
= MAX( maxX
, lpitem
->rect
.right
);
429 orgY
= lpitem
->rect
.bottom
;
430 if (IS_STRING_ITEM(lpitem
->item_flags
) && lpitem
->xTab
)
432 maxTab
= MAX( maxTab
, lpitem
->xTab
);
433 maxTabWidth
= MAX(maxTabWidth
,lpitem
->rect
.right
-lpitem
->xTab
);
437 /* Finish the column (set all items to the largest width found) */
438 maxX
= MAX( maxX
, maxTab
+ maxTabWidth
);
439 for (lpitem
= &lppop
->items
[start
]; start
< i
; start
++, lpitem
++)
441 lpitem
->rect
.right
= maxX
;
442 if (IS_STRING_ITEM(lpitem
->item_flags
) && lpitem
->xTab
)
443 lpitem
->xTab
= maxTab
;
445 lppop
->Height
= MAX( lppop
->Height
, orgY
);
453 /***********************************************************************
454 * MENU_MenuBarCalcSize
456 * Calculate the size of the menu bar.
458 static void MENU_MenuBarCalcSize( HDC hdc
, LPRECT16 lprect
, LPPOPUPMENU lppop
,
462 int start
, i
, orgX
, orgY
, maxY
, helpPos
;
464 if ((lprect
== NULL
) || (lppop
== NULL
)) return;
465 if (lppop
->nItems
== 0) return;
466 dprintf_menu(stddeb
,"MENU_MenuBarCalcSize left=%d top=%d right=%d bottom=%d\n",
467 lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
468 lppop
->Width
= lprect
->right
- lprect
->left
;
473 while (start
< lppop
->nItems
)
475 lpitem
= &lppop
->items
[start
];
479 /* Parse items until line break or end of menu */
480 for (i
= start
; i
< lppop
->nItems
; i
++, lpitem
++)
482 if ((helpPos
== -1) && (lpitem
->item_flags
& MF_HELP
)) helpPos
= i
;
484 (lpitem
->item_flags
& (MF_MENUBREAK
| MF_MENUBARBREAK
))) break;
485 MENU_CalcItemSize( hdc
, lpitem
, hwndOwner
, orgX
, orgY
, TRUE
);
486 if (lpitem
->rect
.right
> lprect
->right
)
488 if (i
!= start
) break;
489 else lpitem
->rect
.right
= lprect
->right
;
491 maxY
= MAX( maxY
, lpitem
->rect
.bottom
);
492 orgX
= lpitem
->rect
.right
;
495 /* Finish the line (set all items to the largest height found) */
496 while (start
< i
) lppop
->items
[start
++].rect
.bottom
= maxY
;
499 lprect
->bottom
= maxY
;
500 lppop
->Height
= lprect
->bottom
- lprect
->top
;
502 /* Flush right all items between the MF_HELP and the last item */
503 /* (if several lines, only move the last line) */
506 lpitem
= &lppop
->items
[lppop
->nItems
-1];
507 orgY
= lpitem
->rect
.top
;
508 orgX
= lprect
->right
;
509 for (i
= lppop
->nItems
- 1; i
>= helpPos
; i
--, lpitem
--)
511 if (lpitem
->rect
.top
!= orgY
) break; /* Other line */
512 if (lpitem
->rect
.right
>= orgX
) break; /* Too far right already */
513 lpitem
->rect
.left
+= orgX
- lpitem
->rect
.right
;
514 lpitem
->rect
.right
= orgX
;
515 orgX
= lpitem
->rect
.left
;
521 /***********************************************************************
524 * Draw a single menu item.
526 static void MENU_DrawMenuItem( HWND hwnd
, HDC hdc
, MENUITEM
*lpitem
,
527 UINT height
, BOOL menuBar
)
531 if (lpitem
->item_flags
& MF_OWNERDRAW
)
533 DRAWITEMSTRUCT32 dis
;
535 dprintf_menu( stddeb
, "DrawMenuItem: Ownerdraw!\n" );
536 dis
.CtlType
= ODT_MENU
;
537 dis
.itemID
= lpitem
->item_id
;
538 dis
.itemData
= (DWORD
)lpitem
->text
;
540 if (lpitem
->item_flags
& MF_CHECKED
) dis
.itemState
|= ODS_CHECKED
;
541 if (lpitem
->item_flags
& MF_GRAYED
) dis
.itemState
|= ODS_GRAYED
;
542 if (lpitem
->item_flags
& MF_HILITE
) dis
.itemState
|= ODS_SELECTED
;
543 dis
.itemAction
= ODA_DRAWENTIRE
| ODA_SELECT
| ODA_FOCUS
;
546 CONV_RECT16TO32( &lpitem
->rect
, &dis
.rcItem
);
547 SendMessage32A( hwnd
, WM_DRAWITEM
, 0, (LPARAM
)&dis
);
551 if (menuBar
&& (lpitem
->item_flags
& MF_SEPARATOR
)) return;
554 /* Draw the background */
556 if (lpitem
->item_flags
& MF_HILITE
)
557 FillRect16( hdc
, &rect
, sysColorObjects
.hbrushHighlight
);
558 else FillRect16( hdc
, &rect
, sysColorObjects
.hbrushMenu
);
559 SetBkMode( hdc
, TRANSPARENT
);
561 /* Draw the separator bar (if any) */
563 if (!menuBar
&& (lpitem
->item_flags
& MF_MENUBARBREAK
))
565 SelectObject( hdc
, sysColorObjects
.hpenWindowFrame
);
566 MoveTo( hdc
, rect
.left
, 0 );
567 LineTo( hdc
, rect
.left
, height
);
569 if (lpitem
->item_flags
& MF_SEPARATOR
)
571 SelectObject( hdc
, sysColorObjects
.hpenWindowFrame
);
572 MoveTo( hdc
, rect
.left
, rect
.top
+ SEPARATOR_HEIGHT
/2 );
573 LineTo( hdc
, rect
.right
, rect
.top
+ SEPARATOR_HEIGHT
/2 );
579 if (lpitem
->item_flags
& MF_HILITE
)
581 if (lpitem
->item_flags
& MF_GRAYED
)
582 SetTextColor( hdc
, GetSysColor( COLOR_GRAYTEXT
) );
584 SetTextColor( hdc
, GetSysColor( COLOR_HIGHLIGHTTEXT
) );
585 SetBkColor( hdc
, GetSysColor( COLOR_HIGHLIGHT
) );
589 if (lpitem
->item_flags
& MF_GRAYED
)
590 SetTextColor( hdc
, GetSysColor( COLOR_GRAYTEXT
) );
592 SetTextColor( hdc
, GetSysColor( COLOR_MENUTEXT
) );
593 SetBkColor( hdc
, GetSysColor( COLOR_MENU
) );
598 /* Draw the check mark */
600 if (lpitem
->item_flags
& MF_CHECKED
)
602 GRAPH_DrawBitmap(hdc
, lpitem
->hCheckBit
? lpitem
->hCheckBit
:
603 hStdCheck
, rect
.left
,
604 (rect
.top
+rect
.bottom
-check_bitmap_height
) / 2,
605 0, 0, check_bitmap_width
, check_bitmap_height
);
607 else if (lpitem
->hUnCheckBit
!= 0) /* Not checked */
609 GRAPH_DrawBitmap(hdc
, lpitem
->hUnCheckBit
, rect
.left
,
610 (rect
.top
+rect
.bottom
-check_bitmap_height
) / 2,
611 0, 0, check_bitmap_width
, check_bitmap_height
);
614 /* Draw the popup-menu arrow */
616 if (lpitem
->item_flags
& MF_POPUP
)
618 GRAPH_DrawBitmap( hdc
, hStdMnArrow
,
619 rect
.right
-arrow_bitmap_width
-1,
620 (rect
.top
+rect
.bottom
-arrow_bitmap_height
) / 2,
621 0, 0, arrow_bitmap_width
, arrow_bitmap_height
);
624 rect
.left
+= check_bitmap_width
;
625 rect
.right
-= arrow_bitmap_width
;
628 /* Draw the item text or bitmap */
630 if (lpitem
->item_flags
& MF_BITMAP
)
632 GRAPH_DrawBitmap( hdc
, (HBITMAP16
)(UINT32
)lpitem
->text
,
633 rect
.left
, rect
.top
, 0, 0,
634 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
637 /* No bitmap - process text if present */
638 else if (IS_STRING_ITEM(lpitem
->item_flags
))
644 rect
.left
+= MENU_BAR_ITEMS_SPACE
/ 2;
645 rect
.right
-= MENU_BAR_ITEMS_SPACE
/ 2;
646 i
= strlen( lpitem
->text
);
650 for (i
= 0; lpitem
->text
[i
]; i
++)
651 if ((lpitem
->text
[i
] == '\t') || (lpitem
->text
[i
] == '\b'))
655 DrawText16( hdc
, lpitem
->text
, i
, &rect
,
656 DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
658 if (lpitem
->text
[i
]) /* There's a tab or flush-right char */
660 if (lpitem
->text
[i
] == '\t')
662 rect
.left
= lpitem
->xTab
;
663 DrawText16( hdc
, lpitem
->text
+ i
+ 1, -1, &rect
,
664 DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
666 else DrawText16( hdc
, lpitem
->text
+ i
+ 1, -1, &rect
,
667 DT_RIGHT
| DT_VCENTER
| DT_SINGLELINE
);
673 /***********************************************************************
676 * Paint a popup menu.
678 static void MENU_DrawPopupMenu( HWND hwnd
, HDC hdc
, HMENU hmenu
)
685 GetClientRect16( hwnd
, &rect
);
686 FillRect16( hdc
, &rect
, sysColorObjects
.hbrushMenu
);
687 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
688 if (!menu
|| !menu
->nItems
) return;
689 for (i
= menu
->nItems
, item
= menu
->items
; i
> 0; i
--, item
++)
690 MENU_DrawMenuItem( hwnd
, hdc
, item
, menu
->Height
, FALSE
);
694 /***********************************************************************
697 * Paint a menu bar. Returns the height of the menu bar.
699 UINT
MENU_DrawMenuBar(HDC hDC
, LPRECT16 lprect
, HWND hwnd
, BOOL suppress_draw
)
703 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
705 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR( (HMENU
)wndPtr
->wIDmenu
);
706 if (lppop
== NULL
|| lprect
== NULL
) return SYSMETRICS_CYMENU
;
707 dprintf_menu(stddeb
,"MENU_DrawMenuBar(%04x, %p, %p); !\n",
709 if (lppop
->Height
== 0) MENU_MenuBarCalcSize(hDC
, lprect
, lppop
, hwnd
);
710 lprect
->bottom
= lprect
->top
+ lppop
->Height
;
711 if (suppress_draw
) return lppop
->Height
;
713 FillRect16(hDC
, lprect
, sysColorObjects
.hbrushMenu
);
714 SelectObject( hDC
, sysColorObjects
.hpenWindowFrame
);
715 MoveTo( hDC
, lprect
->left
, lprect
->bottom
);
716 LineTo( hDC
, lprect
->right
, lprect
->bottom
);
718 if (lppop
->nItems
== 0) return SYSMETRICS_CYMENU
;
719 for (i
= 0; i
< lppop
->nItems
; i
++)
721 MENU_DrawMenuItem( hwnd
, hDC
, &lppop
->items
[i
], lppop
->Height
, TRUE
);
723 return lppop
->Height
;
727 /***********************************************************************
730 static BOOL
MENU_SwitchTPWndTo( HTASK hTask
)
732 /* This is supposed to be called when popup is hidden */
734 TDB
* task
= (TDB
*)GlobalLock16(hTask
);
736 if( !task
) return 0;
738 /* if this task got as far as menu tracking it must have a queue */
740 pTopPWnd
->hInstance
= task
->hInstance
;
741 pTopPWnd
->hmemTaskQ
= task
->hQueue
;
745 /***********************************************************************
748 * Display a popup menu.
750 static BOOL
MENU_ShowPopup(HWND hwndOwner
, HMENU hmenu
, UINT id
, int x
, int y
)
756 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
757 if (menu
->FocusedItem
!= NO_SELECTED_ITEM
)
759 menu
->items
[menu
->FocusedItem
].item_flags
&= ~(MF_HILITE
|MF_MOUSESELECT
);
760 menu
->FocusedItem
= NO_SELECTED_ITEM
;
762 SendMessage16( hwndOwner
, WM_INITMENUPOPUP
, (WPARAM
)hmenu
,
763 MAKELONG( id
, (menu
->wFlags
& MF_SYSMENU
) ? 1 : 0 ));
764 MENU_PopupMenuCalcSize( menu
, hwndOwner
);
766 wndPtr
= WIN_FindWndPtr( hwndOwner
);
767 if (!wndPtr
) return FALSE
;
771 pTopPWnd
= WIN_FindWndPtr(CreateWindow16( POPUPMENU_CLASS_ATOM
, NULL
,
772 WS_POPUP
| WS_BORDER
, x
, y
,
773 menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
774 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
775 0, 0, wndPtr
->hInstance
,
776 (LPVOID
)(HMENU32
)hmenu
));
777 if (!pTopPWnd
) return FALSE
;
783 /* create new window for the submenu */
784 HWND hWnd
= CreateWindow16( POPUPMENU_CLASS_ATOM
, NULL
,
785 WS_POPUP
| WS_BORDER
, x
, y
,
786 menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
787 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
788 menu
->hWnd
, 0, wndPtr
->hInstance
,
789 (LPVOID
)(HMENU32
)hmenu
);
790 if( !hWnd
) return FALSE
;
797 MENU_SwitchTPWndTo(GetCurrentTask());
798 SendMessage16( pTopPWnd
->hwndSelf
, WM_USER
, (WPARAM
)hmenu
, 0L);
800 menu
->hWnd
= pTopPWnd
->hwndSelf
;
805 SetWindowPos(menu
->hWnd
, 0, x
, y
, menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
806 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
807 SWP_NOACTIVATE
| SWP_NOZORDER
);
809 /* Display the window */
811 SetWindowPos( menu
->hWnd
, HWND_TOP
, 0, 0, 0, 0,
812 SWP_SHOWWINDOW
| SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
813 UpdateWindow( menu
->hWnd
);
818 /***********************************************************************
821 static void MENU_SelectItem( HWND hwndOwner
, HMENU hmenu
, UINT wIndex
,
822 BOOL sendMenuSelect
)
827 lppop
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
828 if (!lppop
->nItems
) return;
829 if ((wIndex
!= NO_SELECTED_ITEM
) &&
830 (wIndex
!= SYSMENU_SELECTED
) &&
831 (lppop
->items
[wIndex
].item_flags
& MF_SEPARATOR
))
832 wIndex
= NO_SELECTED_ITEM
;
833 if (lppop
->FocusedItem
== wIndex
) return;
834 if (lppop
->wFlags
& MF_POPUP
) hdc
= GetDC( lppop
->hWnd
);
835 else hdc
= GetDCEx( lppop
->hWnd
, 0, DCX_CACHE
| DCX_WINDOW
);
837 /* Clear previous highlighted item */
838 if (lppop
->FocusedItem
!= NO_SELECTED_ITEM
)
840 if (lppop
->FocusedItem
== SYSMENU_SELECTED
)
841 NC_DrawSysButton( lppop
->hWnd
, hdc
, FALSE
);
844 lppop
->items
[lppop
->FocusedItem
].item_flags
&=~(MF_HILITE
|MF_MOUSESELECT
);
845 MENU_DrawMenuItem(lppop
->hWnd
,hdc
,&lppop
->items
[lppop
->FocusedItem
],
846 lppop
->Height
, !(lppop
->wFlags
& MF_POPUP
) );
850 /* Highlight new item (if any) */
851 lppop
->FocusedItem
= wIndex
;
852 if (lppop
->FocusedItem
!= NO_SELECTED_ITEM
)
854 if (lppop
->FocusedItem
== SYSMENU_SELECTED
)
856 NC_DrawSysButton( lppop
->hWnd
, hdc
, TRUE
);
858 SendMessage16( hwndOwner
, WM_MENUSELECT
,
859 WIN_FindWndPtr(lppop
->hWnd
)->hSysMenu
,
860 MAKELONG(lppop
->wFlags
| MF_MOUSESELECT
, hmenu
));
864 lppop
->items
[lppop
->FocusedItem
].item_flags
|= MF_HILITE
;
865 MENU_DrawMenuItem( lppop
->hWnd
, hdc
, &lppop
->items
[lppop
->FocusedItem
],
866 lppop
->Height
, !(lppop
->wFlags
& MF_POPUP
) );
868 SendMessage16( hwndOwner
, WM_MENUSELECT
,
869 lppop
->items
[lppop
->FocusedItem
].item_id
,
870 MAKELONG( lppop
->items
[lppop
->FocusedItem
].item_flags
| MF_MOUSESELECT
, hmenu
));
873 else if (sendMenuSelect
)
874 SendMessage16( hwndOwner
, WM_MENUSELECT
, hmenu
,
875 MAKELONG( lppop
->wFlags
| MF_MOUSESELECT
, hmenu
) );
877 ReleaseDC( lppop
->hWnd
, hdc
);
881 /***********************************************************************
882 * MENU_SelectNextItem
884 static void MENU_SelectNextItem( HWND hwndOwner
, HMENU hmenu
)
889 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
890 if (!menu
->items
) return;
891 if ((menu
->FocusedItem
!= NO_SELECTED_ITEM
) &&
892 (menu
->FocusedItem
!= SYSMENU_SELECTED
))
894 for (i
= menu
->FocusedItem
+1; i
< menu
->nItems
; i
++)
896 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
898 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
902 if (MENU_HasSysMenu( menu
))
904 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
908 for (i
= 0; i
< menu
->nItems
; i
++)
910 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
912 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
916 if (MENU_HasSysMenu( menu
))
917 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
921 /***********************************************************************
922 * MENU_SelectPrevItem
924 static void MENU_SelectPrevItem( HWND hwndOwner
, HMENU hmenu
)
929 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
930 if (!menu
->items
) return;
931 if ((menu
->FocusedItem
!= NO_SELECTED_ITEM
) &&
932 (menu
->FocusedItem
!= SYSMENU_SELECTED
))
934 for (i
= menu
->FocusedItem
- 1; i
>= 0; i
--)
936 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
938 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
942 if (MENU_HasSysMenu( menu
))
944 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
948 for (i
= menu
->nItems
- 1; i
> 0; i
--)
950 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
952 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
956 if (MENU_HasSysMenu( menu
))
957 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
961 /**********************************************************************
964 * Set an item flags, id and text ptr.
966 static BOOL
MENU_SetItemData( MENUITEM
*item
, UINT flags
, UINT id
, LPCSTR str
)
968 LPSTR prevText
= IS_STRING_ITEM(item
->item_flags
) ? item
->text
: NULL
;
970 if (IS_STRING_ITEM(flags
))
974 flags
|= MF_SEPARATOR
;
980 /* Item beginning with a backspace is a help item */
986 if (!(text
= HEAP_strdupA( SystemHeap
, 0, str
))) return FALSE
;
990 else if ((flags
& MF_BITMAP
) || (flags
& MF_OWNERDRAW
))
991 item
->text
= (LPSTR
)str
;
992 else item
->text
= NULL
;
994 item
->item_flags
= flags
& ~(MF_HILITE
| MF_MOUSESELECT
);
996 SetRectEmpty16( &item
->rect
);
997 if (prevText
) HeapFree( SystemHeap
, 0, prevText
);
1002 /**********************************************************************
1005 * Insert a new item into a menu.
1007 static MENUITEM
*MENU_InsertItem( HMENU hMenu
, UINT pos
, UINT flags
)
1012 if (!(menu
= (POPUPMENU
*)USER_HEAP_LIN_ADDR(hMenu
)))
1014 dprintf_menu( stddeb
, "MENU_InsertItem: %04x not a menu handle\n",
1019 /* Find where to insert new item */
1021 if ((flags
& MF_BYPOSITION
) &&
1022 ((pos
== (UINT
)-1) || (pos
== menu
->nItems
)))
1024 /* Special case: append to menu */
1025 /* Some programs specify the menu length to do that */
1030 if (!MENU_FindItem( &hMenu
, &pos
, flags
))
1032 dprintf_menu( stddeb
, "MENU_InsertItem: item %x not found\n",
1036 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
)))
1038 dprintf_menu(stddeb
,"MENU_InsertItem: %04x not a menu handle\n",
1044 /* Create new items array */
1046 newItems
= HeapAlloc( SystemHeap
, 0, sizeof(MENUITEM
) * (menu
->nItems
+1) );
1049 dprintf_menu( stddeb
, "MENU_InsertItem: allocation failed\n" );
1052 if (menu
->nItems
> 0)
1054 /* Copy the old array into the new */
1055 if (pos
> 0) memcpy( newItems
, menu
->items
, pos
* sizeof(MENUITEM
) );
1056 if (pos
< menu
->nItems
) memcpy( &newItems
[pos
+1], &menu
->items
[pos
],
1057 (menu
->nItems
-pos
)*sizeof(MENUITEM
) );
1058 HeapFree( SystemHeap
, 0, menu
->items
);
1060 menu
->items
= newItems
;
1062 memset( &newItems
[pos
], 0, sizeof(*newItems
) );
1063 return &newItems
[pos
];
1067 /**********************************************************************
1068 * MENU_ParseResource
1070 * Parse a standard menu resource and add items to the menu.
1071 * Return a pointer to the end of the resource.
1073 static LPCSTR
MENU_ParseResource( LPCSTR res
, HMENU hMenu
, BOOL unicode
)
1080 flags
= GET_WORD(res
);
1081 res
+= sizeof(WORD
);
1082 if (!(flags
& MF_POPUP
))
1085 res
+= sizeof(WORD
);
1087 if (!IS_STRING_ITEM(flags
))
1088 fprintf( stderr
, "MENU_ParseResource: not a string item %04x\n",
1091 if (!unicode
) res
+= strlen(str
) + 1;
1092 else res
+= (lstrlen32W((LPCWSTR
)str
) + 1) * sizeof(WCHAR
);
1093 if (flags
& MF_POPUP
)
1095 HMENU hSubMenu
= CreatePopupMenu();
1096 if (!hSubMenu
) return NULL
;
1097 if (!(res
= MENU_ParseResource( res
, hSubMenu
, unicode
)))
1099 if (!unicode
) AppendMenu32A( hMenu
, flags
, (UINT
)hSubMenu
, str
);
1100 else AppendMenu32W( hMenu
, flags
, (UINT
)hSubMenu
, (LPCWSTR
)str
);
1102 else /* Not a popup */
1104 if (!unicode
) AppendMenu32A( hMenu
, flags
, id
, *str
? str
: NULL
);
1105 else AppendMenu32W( hMenu
, flags
, id
,
1106 *(LPCWSTR
)str
? (LPCWSTR
)str
: NULL
);
1108 } while (!(flags
& MF_END
));
1113 /***********************************************************************
1116 * Return the handle of the selected sub-popup menu (if any).
1118 static HMENU
MENU_GetSubPopup( HMENU hmenu
)
1123 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1124 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return 0;
1125 else if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1126 return WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1128 item
= &menu
->items
[menu
->FocusedItem
];
1129 if (!(item
->item_flags
& MF_POPUP
) || !(item
->item_flags
& MF_MOUSESELECT
))
1131 return (HMENU
)item
->item_id
;
1135 /***********************************************************************
1136 * MENU_HideSubPopups
1138 * Hide the sub-popup menus of this menu.
1140 static void MENU_HideSubPopups( HWND hwndOwner
, HMENU hmenu
,
1141 BOOL sendMenuSelect
)
1144 POPUPMENU
*menu
, *submenu
;
1147 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return;
1148 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return;
1149 if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1151 hsubmenu
= WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1155 item
= &menu
->items
[menu
->FocusedItem
];
1156 if (!(item
->item_flags
& MF_POPUP
) ||
1157 !(item
->item_flags
& MF_MOUSESELECT
)) return;
1158 item
->item_flags
&= ~MF_MOUSESELECT
;
1159 hsubmenu
= (HMENU
)item
->item_id
;
1161 submenu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hsubmenu
);
1162 MENU_HideSubPopups( hwndOwner
, hsubmenu
, FALSE
);
1163 MENU_SelectItem( hwndOwner
, hsubmenu
, NO_SELECTED_ITEM
, sendMenuSelect
);
1164 if (submenu
->hWnd
== pTopPWnd
->hwndSelf
)
1166 ShowWindow( submenu
->hWnd
, SW_HIDE
);
1171 DestroyWindow( submenu
->hWnd
);
1177 /***********************************************************************
1180 * Display the sub-menu of the selected item of this menu.
1181 * Return the handle of the submenu, or hmenu if no submenu to display.
1183 static HMENU
MENU_ShowSubPopup( HWND hwndOwner
, HMENU hmenu
, BOOL selectFirst
)
1189 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return hmenu
;
1190 if (!(wndPtr
= WIN_FindWndPtr( menu
->hWnd
))) return hmenu
;
1191 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return hmenu
;
1192 if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1194 MENU_ShowPopup(hwndOwner
, wndPtr
->hSysMenu
, 0, wndPtr
->rectClient
.left
,
1195 wndPtr
->rectClient
.top
- menu
->Height
- 2*SYSMETRICS_CYBORDER
);
1196 if (selectFirst
) MENU_SelectNextItem( hwndOwner
, wndPtr
->hSysMenu
);
1197 return wndPtr
->hSysMenu
;
1199 item
= &menu
->items
[menu
->FocusedItem
];
1200 if (!(item
->item_flags
& MF_POPUP
) ||
1201 (item
->item_flags
& (MF_GRAYED
| MF_DISABLED
))) return hmenu
;
1202 item
->item_flags
|= MF_MOUSESELECT
;
1203 if (menu
->wFlags
& MF_POPUP
)
1205 MENU_ShowPopup( hwndOwner
, (HMENU
)item
->item_id
, menu
->FocusedItem
,
1206 wndPtr
->rectWindow
.left
+ item
->rect
.right
-arrow_bitmap_width
,
1207 wndPtr
->rectWindow
.top
+ item
->rect
.top
);
1211 MENU_ShowPopup( hwndOwner
, (HMENU
)item
->item_id
, menu
->FocusedItem
,
1212 wndPtr
->rectWindow
.left
+ item
->rect
.left
,
1213 wndPtr
->rectWindow
.top
+ item
->rect
.bottom
);
1215 if (selectFirst
) MENU_SelectNextItem( hwndOwner
, (HMENU
)item
->item_id
);
1216 return (HMENU
)item
->item_id
;
1220 /***********************************************************************
1221 * MENU_FindMenuByCoords
1223 * Find the menu containing a given point (in screen coords).
1225 static HMENU
MENU_FindMenuByCoords( HMENU hmenu
, POINT16 pt
)
1230 if (!(hwnd
= WindowFromPoint16( pt
))) return 0;
1233 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1234 if (menu
->hWnd
== hwnd
)
1236 if (!(menu
->wFlags
& MF_POPUP
))
1238 /* Make sure it's in the menu bar (or in system menu) */
1239 WND
*wndPtr
= WIN_FindWndPtr( menu
->hWnd
);
1240 if ((pt
.x
< wndPtr
->rectClient
.left
) ||
1241 (pt
.x
>= wndPtr
->rectClient
.right
) ||
1242 (pt
.y
>= wndPtr
->rectClient
.top
)) return 0;
1243 if (pt
.y
< wndPtr
->rectClient
.top
- menu
->Height
)
1245 if (!MENU_IsInSysMenu( menu
, pt
)) return 0;
1247 /* else it's in the menu bar */
1251 hmenu
= MENU_GetSubPopup( hmenu
);
1257 /***********************************************************************
1258 * MENU_ExecFocusedItem
1260 * Execute a menu item (for instance when user pressed Enter).
1261 * Return TRUE if we can go on with menu tracking.
1263 static BOOL
MENU_ExecFocusedItem( HWND hwndOwner
, HMENU hmenu
,
1264 HMENU
*hmenuCurrent
)
1267 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1268 if (!menu
|| !menu
->nItems
|| (menu
->FocusedItem
== NO_SELECTED_ITEM
) ||
1269 (menu
->FocusedItem
== SYSMENU_SELECTED
)) return TRUE
;
1270 item
= &menu
->items
[menu
->FocusedItem
];
1271 if (!(item
->item_flags
& MF_POPUP
))
1273 if (!(item
->item_flags
& (MF_GRAYED
| MF_DISABLED
)))
1275 PostMessage( hwndOwner
, (menu
->wFlags
& MF_SYSMENU
) ?
1276 WM_SYSCOMMAND
: WM_COMMAND
, item
->item_id
, 0 );
1283 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, TRUE
);
1289 /***********************************************************************
1292 * Handle a button-down event in a menu. Point is in screen coords.
1293 * hmenuCurrent is the top-most visible popup.
1294 * Return TRUE if we can go on with menu tracking.
1296 static BOOL
MENU_ButtonDown( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1303 if (!hmenu
) return FALSE
; /* Outside all menus */
1304 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1305 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1306 if (!item
) /* Maybe in system menu */
1308 if (!MENU_IsInSysMenu( menu
, pt
)) return FALSE
;
1309 id
= SYSMENU_SELECTED
;
1312 if (menu
->FocusedItem
== id
)
1314 if (id
== SYSMENU_SELECTED
) return FALSE
;
1315 if (item
->item_flags
& MF_POPUP
)
1317 if (item
->item_flags
& MF_MOUSESELECT
)
1319 if (menu
->wFlags
& MF_POPUP
)
1321 MENU_HideSubPopups( hwndOwner
, hmenu
, TRUE
);
1322 *hmenuCurrent
= hmenu
;
1326 else *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1331 MENU_HideSubPopups( hwndOwner
, hmenu
, FALSE
);
1332 MENU_SelectItem( hwndOwner
, hmenu
, id
, TRUE
);
1333 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1339 /***********************************************************************
1342 * Handle a button-up event in a menu. Point is in screen coords.
1343 * hmenuCurrent is the top-most visible popup.
1344 * Return TRUE if we can go on with menu tracking.
1346 static BOOL
MENU_ButtonUp( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1354 if (!hmenu
) return FALSE
; /* Outside all menus */
1355 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1356 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1357 if (!item
) /* Maybe in system menu */
1359 if (!MENU_IsInSysMenu( menu
, pt
)) return FALSE
;
1360 id
= SYSMENU_SELECTED
;
1361 hsubmenu
= WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1364 if (menu
->FocusedItem
!= id
) return FALSE
;
1366 if (id
!= SYSMENU_SELECTED
)
1368 if (!(item
->item_flags
& MF_POPUP
))
1370 return MENU_ExecFocusedItem( hwndOwner
, hmenu
, hmenuCurrent
);
1372 hsubmenu
= (HMENU
)item
->item_id
;
1374 /* Select first item of sub-popup */
1375 MENU_SelectItem( hwndOwner
, hsubmenu
, NO_SELECTED_ITEM
, FALSE
);
1376 MENU_SelectNextItem( hwndOwner
, hsubmenu
);
1381 /***********************************************************************
1384 * Handle a motion event in a menu. Point is in screen coords.
1385 * hmenuCurrent is the top-most visible popup.
1386 * Return TRUE if we can go on with menu tracking.
1388 static BOOL
MENU_MouseMove( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1392 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1393 UINT id
= NO_SELECTED_ITEM
;
1397 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1398 if (!item
) /* Maybe in system menu */
1400 if (!MENU_IsInSysMenu( menu
, pt
))
1401 id
= NO_SELECTED_ITEM
; /* Outside all items */
1402 else id
= SYSMENU_SELECTED
;
1405 if (id
== NO_SELECTED_ITEM
)
1407 MENU_SelectItem( hwndOwner
, *hmenuCurrent
, NO_SELECTED_ITEM
, TRUE
);
1409 else if (menu
->FocusedItem
!= id
)
1411 MENU_HideSubPopups( hwndOwner
, hmenu
, FALSE
);
1412 MENU_SelectItem( hwndOwner
, hmenu
, id
, TRUE
);
1413 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1419 /***********************************************************************
1422 * Handle a VK_LEFT key event in a menu.
1423 * hmenuCurrent is the top-most visible popup.
1425 static void MENU_KeyLeft( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
)
1428 HMENU hmenutmp
, hmenuprev
;
1430 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1431 hmenuprev
= hmenutmp
= hmenu
;
1432 while (hmenutmp
!= *hmenuCurrent
)
1434 hmenutmp
= MENU_GetSubPopup( hmenuprev
);
1435 if (hmenutmp
!= *hmenuCurrent
) hmenuprev
= hmenutmp
;
1437 MENU_HideSubPopups( hwndOwner
, hmenuprev
, TRUE
);
1439 if ((hmenuprev
== hmenu
) && !(menu
->wFlags
& MF_POPUP
))
1441 /* Select previous item on the menu bar */
1442 MENU_SelectPrevItem( hwndOwner
, hmenu
);
1443 if (*hmenuCurrent
!= hmenu
)
1445 /* A popup menu was displayed -> display the next one */
1446 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, TRUE
);
1449 else *hmenuCurrent
= hmenuprev
;
1453 /***********************************************************************
1456 * Handle a VK_RIGHT key event in a menu.
1457 * hmenuCurrent is the top-most visible popup.
1459 static void MENU_KeyRight( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
)
1464 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1466 if ((menu
->wFlags
& MF_POPUP
) || (*hmenuCurrent
!= hmenu
))
1468 /* If already displaying a popup, try to display sub-popup */
1469 hmenutmp
= MENU_ShowSubPopup( hwndOwner
, *hmenuCurrent
, TRUE
);
1470 if (hmenutmp
!= *hmenuCurrent
) /* Sub-popup displayed */
1472 *hmenuCurrent
= hmenutmp
;
1477 /* If on menu-bar, go to next item */
1478 if (!(menu
->wFlags
& MF_POPUP
))
1480 MENU_HideSubPopups( hwndOwner
, hmenu
, FALSE
);
1481 MENU_SelectNextItem( hwndOwner
, hmenu
);
1482 if (*hmenuCurrent
!= hmenu
)
1484 /* A popup menu was displayed -> display the next one */
1485 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, TRUE
);
1488 else if (*hmenuCurrent
!= hmenu
) /* Hide last level popup */
1491 hmenuprev
= hmenutmp
= hmenu
;
1492 while (hmenutmp
!= *hmenuCurrent
)
1494 hmenutmp
= MENU_GetSubPopup( hmenuprev
);
1495 if (hmenutmp
!= *hmenuCurrent
) hmenuprev
= hmenutmp
;
1497 MENU_HideSubPopups( hwndOwner
, hmenuprev
, TRUE
);
1498 *hmenuCurrent
= hmenuprev
;
1503 /***********************************************************************
1506 * Menu tracking code.
1507 * If 'x' and 'y' are not 0, we simulate a button-down event at (x,y)
1508 * before beginning tracking. This is to help menu-bar tracking.
1510 static BOOL
MENU_TrackMenu( HMENU hmenu
, UINT wFlags
, int x
, int y
,
1511 HWND hwnd
, const RECT16
*lprect
)
1516 HMENU hmenuCurrent
= hmenu
;
1517 BOOL fClosed
= FALSE
, fRemove
;
1520 fEndMenuCalled
= FALSE
;
1521 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
1524 POINT16 pt
= { x
, y
};
1525 MENU_ButtonDown( hwnd
, hmenu
, &hmenuCurrent
, pt
);
1528 hMsg
= USER_HEAP_ALLOC( sizeof(MSG16
) );
1529 msg
= (MSG16
*)USER_HEAP_LIN_ADDR( hMsg
);
1532 if (!MSG_InternalGetMessage( (SEGPTR
)USER_HEAP_SEG_ADDR(hMsg
), 0,
1533 hwnd
, MSGF_MENU
, 0, TRUE
))
1537 if ((msg
->message
>= WM_MOUSEFIRST
) && (msg
->message
<= WM_MOUSELAST
))
1539 /* Find the sub-popup for this mouse event (if any) */
1540 HMENU hsubmenu
= MENU_FindMenuByCoords( hmenu
, msg
->pt
);
1542 switch(msg
->message
)
1544 case WM_RBUTTONDOWN
:
1545 case WM_NCRBUTTONDOWN
:
1546 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1548 case WM_LBUTTONDOWN
:
1549 case WM_NCLBUTTONDOWN
:
1550 fClosed
= !MENU_ButtonDown( hwnd
, hsubmenu
,
1551 &hmenuCurrent
, msg
->pt
);
1555 case WM_NCRBUTTONUP
:
1556 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1559 case WM_NCLBUTTONUP
:
1560 /* If outside all menus but inside lprect, ignore it */
1561 if (!hsubmenu
&& lprect
&& PtInRect16(lprect
, msg
->pt
)) break;
1562 fClosed
= !MENU_ButtonUp( hwnd
, hsubmenu
,
1563 &hmenuCurrent
, msg
->pt
);
1564 fRemove
= TRUE
; /* Remove event even if outside menu */
1568 case WM_NCMOUSEMOVE
:
1569 if ((msg
->wParam
& MK_LBUTTON
) ||
1570 ((wFlags
& TPM_RIGHTBUTTON
) && (msg
->wParam
& MK_RBUTTON
)))
1572 fClosed
= !MENU_MouseMove( hwnd
, hsubmenu
,
1573 &hmenuCurrent
, msg
->pt
);
1578 else if ((msg
->message
>= WM_KEYFIRST
) && (msg
->message
<= WM_KEYLAST
))
1580 fRemove
= TRUE
; /* Keyboard messages are always removed */
1581 switch(msg
->message
)
1587 MENU_SelectItem( hwnd
, hmenuCurrent
, NO_SELECTED_ITEM
, FALSE
);
1588 MENU_SelectNextItem( hwnd
, hmenuCurrent
);
1592 MENU_SelectItem( hwnd
, hmenuCurrent
, NO_SELECTED_ITEM
, FALSE
);
1593 MENU_SelectPrevItem( hwnd
, hmenuCurrent
);
1597 MENU_SelectPrevItem( hwnd
, hmenuCurrent
);
1601 /* If on menu bar, pull-down the menu */
1602 if (!(menu
->wFlags
& MF_POPUP
) && (hmenuCurrent
== hmenu
))
1603 hmenuCurrent
= MENU_ShowSubPopup( hwnd
, hmenu
, TRUE
);
1605 MENU_SelectNextItem( hwnd
, hmenuCurrent
);
1609 MENU_KeyLeft( hwnd
, hmenu
, &hmenuCurrent
);
1613 MENU_KeyRight( hwnd
, hmenu
, &hmenuCurrent
);
1618 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1629 break; /* WM_KEYDOWN */
1639 break; /* WM_SYSKEYDOWN */
1643 /* Hack to avoid control chars. */
1644 /* We will find a better way real soon... */
1645 if ((msg
->wParam
<= 32) || (msg
->wParam
>= 127)) break;
1646 pos
= MENU_FindItemByKey( hwnd
, hmenuCurrent
, msg
->wParam
);
1647 if (pos
== (UINT
)-2) fClosed
= TRUE
;
1648 else if (pos
== (UINT
)-1) MessageBeep(0);
1651 MENU_SelectItem( hwnd
, hmenuCurrent
, pos
, TRUE
);
1652 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1657 break; /* WM_CHAR */
1658 } /* switch(msg->message) */
1662 DispatchMessage( msg
);
1664 if (fEndMenuCalled
) fClosed
= TRUE
;
1665 if (!fClosed
) fRemove
= TRUE
;
1667 if (fRemove
) /* Remove the message from the queue */
1668 PeekMessage16( msg
, 0, msg
->message
, msg
->message
, PM_REMOVE
);
1670 USER_HEAP_FREE( hMsg
);
1672 MENU_HideSubPopups( hwnd
, hmenu
, FALSE
);
1673 if (menu
->wFlags
& MF_POPUP
)
1675 ShowWindow( menu
->hWnd
, SW_HIDE
);
1678 MENU_SelectItem( hwnd
, hmenu
, NO_SELECTED_ITEM
, FALSE
);
1679 SendMessage16( hwnd
, WM_MENUSELECT
, 0, MAKELONG( 0xffff, 0 ) );
1680 fEndMenuCalled
= FALSE
;
1685 /***********************************************************************
1686 * MENU_TrackMouseMenuBar
1688 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
1690 void MENU_TrackMouseMenuBar( HWND hwnd
, POINT16 pt
)
1692 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1694 SendMessage16( hwnd
, WM_ENTERMENULOOP
, 0, 0 );
1695 SendMessage16( hwnd
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1696 MENU_TrackMenu( (HMENU
)wndPtr
->wIDmenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1697 pt
.x
, pt
.y
, hwnd
, NULL
);
1698 SendMessage16( hwnd
, WM_EXITMENULOOP
, 0, 0 );
1703 /***********************************************************************
1704 * MENU_TrackKbdMenuBar
1706 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
1708 void MENU_TrackKbdMenuBar( WND
* wndPtr
, UINT wParam
, INT vkey
)
1710 UINT uItem
= NO_SELECTED_ITEM
;
1713 /* find window that has a menu
1716 if( !(wndPtr
->dwStyle
& WS_CHILD
) )
1718 wndPtr
= WIN_FindWndPtr( GetActiveWindow() );
1719 if( !wndPtr
) return;
1722 while( wndPtr
->dwStyle
& WS_CHILD
&&
1723 !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1724 if( !(wndPtr
= wndPtr
->parent
) ) return;
1726 if( wndPtr
->dwStyle
& WS_CHILD
|| !wndPtr
->wIDmenu
)
1727 if( !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1730 hTrackMenu
= ( IsMenu( wndPtr
->wIDmenu
) )? wndPtr
->wIDmenu
:
1734 SendMessage16( wndPtr
->hwndSelf
, WM_ENTERMENULOOP
, 0, 0 );
1735 SendMessage16( wndPtr
->hwndSelf
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1737 /* find suitable menu entry
1740 if( vkey
== VK_SPACE
)
1741 uItem
= SYSMENU_SELECTED
;
1744 uItem
= MENU_FindItemByKey( wndPtr
->hwndSelf
, wndPtr
->wIDmenu
, vkey
);
1745 if( uItem
>= 0xFFFE )
1747 if( uItem
== 0xFFFF )
1749 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1755 MENU_SelectItem( wndPtr
->hwndSelf
, hTrackMenu
, uItem
, TRUE
);
1756 if( uItem
== NO_SELECTED_ITEM
)
1757 MENU_SelectNextItem( wndPtr
->hwndSelf
, hTrackMenu
);
1759 PostMessage( wndPtr
->hwndSelf
, WM_KEYDOWN
, VK_DOWN
, 0L );
1761 MENU_TrackMenu( hTrackMenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1762 0, 0, wndPtr
->hwndSelf
, NULL
);
1764 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1769 /**********************************************************************
1770 * TrackPopupMenu16 (USER.416)
1772 BOOL16
TrackPopupMenu16( HMENU16 hMenu
, UINT16 wFlags
, INT16 x
, INT16 y
,
1773 INT16 nReserved
, HWND16 hWnd
, const RECT16
*lpRect
)
1778 if (MENU_ShowPopup( hWnd
, hMenu
, 0, x
, y
))
1779 ret
= MENU_TrackMenu( hMenu
, wFlags
, 0, 0, hWnd
, lpRect
);
1785 /**********************************************************************
1786 * TrackPopupMenu32 (USER32.548)
1788 BOOL32
TrackPopupMenu32( HMENU32 hMenu
, UINT32 wFlags
, INT32 x
, INT32 y
,
1789 INT32 nReserved
, HWND32 hWnd
, const RECT32
*lpRect
)
1792 CONV_RECT32TO16( lpRect
, &r
);
1793 return TrackPopupMenu16( hMenu
, wFlags
, x
, y
, nReserved
, hWnd
, &r
);
1797 /***********************************************************************
1800 LRESULT
PopupMenuWndProc(HWND hwnd
,UINT message
,WPARAM wParam
,LPARAM lParam
)
1806 CREATESTRUCT16
*cs
= (CREATESTRUCT16
*)PTR_SEG_TO_LIN(lParam
);
1807 SetWindowLong32A( hwnd
, 0, (LONG
)cs
->lpCreateParams
);
1811 case WM_MOUSEACTIVATE
: /* We don't want to be activated */
1812 return MA_NOACTIVATE
;
1817 BeginPaint16( hwnd
, &ps
);
1818 MENU_DrawPopupMenu( hwnd
, ps
.hdc
,
1819 (HMENU
)GetWindowLong32A( hwnd
, 0 ) );
1820 EndPaint16( hwnd
, &ps
);
1825 /* zero out global pointer in case system popup
1826 * was destroyed by AppExit
1829 if( hwnd
== pTopPWnd
->hwndSelf
)
1836 if (wParam
) SetWindowLong32A( hwnd
, 0, (HMENU
)wParam
);
1839 return DefWindowProc16(hwnd
, message
, wParam
, lParam
);
1845 /***********************************************************************
1846 * MENU_GetMenuBarHeight
1848 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
1850 UINT
MENU_GetMenuBarHeight( HWND hwnd
, UINT menubarWidth
, int orgX
, int orgY
)
1857 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
1858 if (!(lppop
= (LPPOPUPMENU
)USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
)))
1860 hdc
= GetDC( hwnd
);
1861 SetRect16(&rectBar
, orgX
, orgY
, orgX
+menubarWidth
, orgY
+SYSMETRICS_CYMENU
);
1862 MENU_MenuBarCalcSize( hdc
, &rectBar
, lppop
, hwnd
);
1863 ReleaseDC( hwnd
, hdc
);
1864 return lppop
->Height
;
1868 /*******************************************************************
1869 * ChangeMenu16 (USER.153)
1871 BOOL16
ChangeMenu16( HMENU16 hMenu
, UINT16 pos
, SEGPTR data
,
1872 UINT16 id
, UINT16 flags
)
1874 dprintf_menu( stddeb
,"ChangeMenu16: menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
1875 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1876 if (flags
& MF_APPEND
) return AppendMenu16( hMenu
, flags
& ~MF_APPEND
,
1878 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
1879 /* for MF_DELETE. We should check the parameters for all others */
1880 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
1881 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1882 if (flags
& MF_CHANGE
) return ModifyMenu16( hMenu
, pos
, flags
& ~MF_CHANGE
,
1884 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1885 flags
& MF_BYPOSITION
? pos
: id
,
1886 flags
& ~MF_REMOVE
);
1887 /* Default: MF_INSERT */
1888 return InsertMenu16( hMenu
, pos
, flags
, id
, data
);
1892 /*******************************************************************
1893 * ChangeMenu32A (USER32.22)
1895 BOOL32
ChangeMenu32A( HMENU32 hMenu
, UINT32 pos
, LPCSTR data
,
1896 UINT32 id
, UINT32 flags
)
1898 dprintf_menu( stddeb
,"ChangeMenu32A: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1899 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1900 if (flags
& MF_APPEND
) return AppendMenu32A( hMenu
, flags
& ~MF_APPEND
,
1902 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1903 if (flags
& MF_CHANGE
) return ModifyMenu32A(hMenu
, pos
, flags
& ~MF_CHANGE
,
1905 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1906 flags
& MF_BYPOSITION
? pos
: id
,
1907 flags
& ~MF_REMOVE
);
1908 /* Default: MF_INSERT */
1909 return InsertMenu32A( hMenu
, pos
, flags
, id
, data
);
1913 /*******************************************************************
1914 * ChangeMenu32W (USER32.23)
1916 BOOL32
ChangeMenu32W( HMENU32 hMenu
, UINT32 pos
, LPCWSTR data
,
1917 UINT32 id
, UINT32 flags
)
1919 dprintf_menu( stddeb
,"ChangeMenu32W: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1920 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1921 if (flags
& MF_APPEND
) return AppendMenu32W( hMenu
, flags
& ~MF_APPEND
,
1923 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1924 if (flags
& MF_CHANGE
) return ModifyMenu32W(hMenu
, pos
, flags
& ~MF_CHANGE
,
1926 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1927 flags
& MF_BYPOSITION
? pos
: id
,
1928 flags
& ~MF_REMOVE
);
1929 /* Default: MF_INSERT */
1930 return InsertMenu32W( hMenu
, pos
, flags
, id
, data
);
1934 /*******************************************************************
1935 * CheckMenuItem (USER.154)
1937 INT
CheckMenuItem( HMENU hMenu
, UINT id
, UINT flags
)
1942 dprintf_menu( stddeb
,"CheckMenuItem: %04x %04x %04x\n", hMenu
, id
, flags
);
1943 if (!(item
= MENU_FindItem( &hMenu
, &id
, flags
))) return -1;
1944 ret
= item
->item_flags
& MF_CHECKED
;
1945 if (flags
& MF_CHECKED
) item
->item_flags
|= MF_CHECKED
;
1946 else item
->item_flags
&= ~MF_CHECKED
;
1951 /**********************************************************************
1952 * EnableMenuItem [USER.155]
1954 BOOL
EnableMenuItem(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
1957 dprintf_menu(stddeb
,"EnableMenuItem (%04x, %04X, %04X) !\n",
1958 hMenu
, wItemID
, wFlags
);
1959 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return FALSE
;
1961 /* We can't have MF_GRAYED and MF_DISABLED together */
1962 if (wFlags
& MF_GRAYED
)
1964 item
->item_flags
= (item
->item_flags
& ~MF_DISABLED
) | MF_GRAYED
;
1966 else if (wFlags
& MF_DISABLED
)
1968 item
->item_flags
= (item
->item_flags
& ~MF_GRAYED
) | MF_DISABLED
;
1970 else /* MF_ENABLED */
1972 item
->item_flags
&= ~(MF_GRAYED
| MF_DISABLED
);
1978 /*******************************************************************
1979 * GetMenuString (USER.161)
1981 int GetMenuString( HMENU hMenu
, UINT wItemID
,
1982 LPSTR str
, short nMaxSiz
, UINT wFlags
)
1986 dprintf_menu( stddeb
, "GetMenuString: menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
1987 hMenu
, wItemID
, str
, nMaxSiz
, wFlags
);
1988 if (!str
|| !nMaxSiz
) return 0;
1990 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return 0;
1991 if (!IS_STRING_ITEM(item
->item_flags
)) return 0;
1992 lstrcpyn32A( str
, item
->text
, nMaxSiz
);
1993 dprintf_menu( stddeb
, "GetMenuString: returning '%s'\n", str
);
1998 /**********************************************************************
1999 * HiliteMenuItem [USER.162]
2001 BOOL
HiliteMenuItem(HWND hWnd
, HMENU hMenu
, UINT wItemID
, UINT wHilite
)
2004 dprintf_menu(stddeb
,"HiliteMenuItem(%04x, %04x, %04x, %04x);\n",
2005 hWnd
, hMenu
, wItemID
, wHilite
);
2006 if (!MENU_FindItem( &hMenu
, &wItemID
, wHilite
)) return FALSE
;
2007 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2008 if (menu
->FocusedItem
== wItemID
) return TRUE
;
2009 MENU_HideSubPopups( hWnd
, hMenu
, FALSE
);
2010 MENU_SelectItem( hWnd
, hMenu
, wItemID
, TRUE
);
2015 /**********************************************************************
2016 * GetMenuState [USER.250]
2018 UINT
GetMenuState(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
2021 dprintf_menu(stddeb
,"GetMenuState(%04x, %04x, %04x);\n",
2022 hMenu
, wItemID
, wFlags
);
2023 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return -1;
2024 if (item
->item_flags
& MF_POPUP
)
2026 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( (HMENU
)item
->item_id
);
2027 if (!menu
) return -1;
2028 else return (menu
->nItems
<< 8) | (menu
->wFlags
& 0xff);
2030 else return item
->item_flags
;
2034 /**********************************************************************
2035 * GetMenuItemCount [USER.263]
2037 INT
GetMenuItemCount(HMENU hMenu
)
2040 dprintf_menu(stddeb
,"GetMenuItemCount(%04x);\n", hMenu
);
2041 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2042 if (menu
== NULL
) return (UINT
)-1;
2043 dprintf_menu(stddeb
,"GetMenuItemCount(%04x) return %d \n",
2044 hMenu
, menu
->nItems
);
2045 return menu
->nItems
;
2049 /**********************************************************************
2050 * GetMenuItemID [USER.264]
2052 UINT
GetMenuItemID(HMENU hMenu
, int nPos
)
2056 dprintf_menu(stddeb
,"GetMenuItemID(%04x, %d);\n", hMenu
, nPos
);
2057 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return -1;
2058 if ((nPos
< 0) || (nPos
>= menu
->nItems
)) return -1;
2059 if (menu
->items
[nPos
].item_flags
& MF_POPUP
) return -1;
2060 return menu
->items
[nPos
].item_id
;
2064 /*******************************************************************
2065 * InsertMenu16 (USER.410)
2067 BOOL16
InsertMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2068 UINT16 id
, SEGPTR data
)
2070 if (IS_STRING_ITEM(flags
) && data
)
2071 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2072 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2073 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2077 /*******************************************************************
2078 * InsertMenu32A (USER32.321)
2080 BOOL32
InsertMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2081 UINT32 id
, LPCSTR str
)
2085 if (IS_STRING_ITEM(flags
) && str
)
2086 dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x '%s'\n",
2087 hMenu
, pos
, flags
, id
, str
);
2088 else dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x %08lx\n",
2089 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2091 if (!(item
= MENU_InsertItem( hMenu
, pos
, flags
))) return FALSE
;
2093 if (!(MENU_SetItemData( item
, flags
, id
, str
)))
2095 RemoveMenu( hMenu
, pos
, flags
);
2099 if (flags
& MF_POPUP
) /* Set the MF_POPUP flag on the popup-menu */
2100 ((POPUPMENU
*)USER_HEAP_LIN_ADDR((HMENU
)id
))->wFlags
|= MF_POPUP
;
2102 item
->hCheckBit
= hStdCheck
;
2103 item
->hUnCheckBit
= 0;
2108 /*******************************************************************
2109 * InsertMenu32W (USER32.324)
2111 BOOL32
InsertMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2112 UINT32 id
, LPCWSTR str
)
2116 if (IS_STRING_ITEM(flags
) && str
)
2118 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2119 ret
= InsertMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2123 else return InsertMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2127 /*******************************************************************
2128 * AppendMenu16 (USER.411)
2130 BOOL16
AppendMenu16( HMENU16 hMenu
, UINT16 flags
, UINT16 id
, SEGPTR data
)
2132 return InsertMenu16( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2136 /*******************************************************************
2137 * AppendMenu32A (USER32.4)
2139 BOOL32
AppendMenu32A( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCSTR data
)
2141 return InsertMenu32A( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2145 /*******************************************************************
2146 * AppendMenu32W (USER32.5)
2148 BOOL32
AppendMenu32W( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCWSTR data
)
2150 return InsertMenu32W( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2154 /**********************************************************************
2155 * RemoveMenu [USER.412]
2157 BOOL
RemoveMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2162 dprintf_menu(stddeb
,"RemoveMenu (%04x, %04x, %04x)\n",hMenu
, nPos
, wFlags
);
2163 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2164 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2168 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2169 HeapFree( SystemHeap
, 0, item
->text
);
2170 if (--menu
->nItems
== 0)
2172 HeapFree( SystemHeap
, 0, menu
->items
);
2177 while(nPos
< menu
->nItems
)
2183 menu
->items
= HeapReAlloc( SystemHeap
, 0, menu
->items
,
2184 menu
->nItems
* sizeof(MENUITEM
) );
2190 /**********************************************************************
2191 * DeleteMenu [USER.413]
2193 BOOL
DeleteMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2195 MENUITEM
*item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
);
2196 if (!item
) return FALSE
;
2197 if (item
->item_flags
& MF_POPUP
) DestroyMenu( (HMENU
)item
->item_id
);
2198 /* nPos is now the position of the item */
2199 RemoveMenu( hMenu
, nPos
, wFlags
| MF_BYPOSITION
);
2204 /*******************************************************************
2205 * ModifyMenu16 (USER.414)
2207 BOOL16
ModifyMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2208 UINT16 id
, SEGPTR data
)
2210 if (IS_STRING_ITEM(flags
))
2211 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2212 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2213 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2217 /*******************************************************************
2218 * ModifyMenu32A (USER32.396)
2220 BOOL32
ModifyMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2221 UINT32 id
, LPCSTR str
)
2224 HMENU hMenu16
= hMenu
;
2227 if (IS_STRING_ITEM(flags
))
2229 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x '%s'\n",
2230 hMenu
, pos
, flags
, id
, str
? str
: "#NULL#" );
2231 if (!str
) return FALSE
;
2235 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x %08lx\n",
2236 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2239 if (!(item
= MENU_FindItem( &hMenu16
, &pos16
, flags
))) return FALSE
;
2240 return MENU_SetItemData( item
, flags
, id
, str
);
2244 /*******************************************************************
2245 * ModifyMenu32W (USER32.397)
2247 BOOL32
ModifyMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2248 UINT32 id
, LPCWSTR str
)
2252 if (IS_STRING_ITEM(flags
) && str
)
2254 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2255 ret
= ModifyMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2259 else return ModifyMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2263 /**********************************************************************
2264 * CreatePopupMenu [USER.415]
2266 HMENU
CreatePopupMenu()
2271 if (!(hmenu
= CreateMenu())) return 0;
2272 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
2273 menu
->wFlags
|= MF_POPUP
;
2278 /**********************************************************************
2279 * GetMenuCheckMarkDimensions [USER.417]
2281 DWORD
GetMenuCheckMarkDimensions()
2283 return MAKELONG( check_bitmap_width
, check_bitmap_height
);
2287 /**********************************************************************
2288 * SetMenuItemBitmaps [USER.418]
2290 BOOL
SetMenuItemBitmaps(HMENU hMenu
, UINT nPos
, UINT wFlags
,
2291 HBITMAP hNewUnCheck
, HBITMAP hNewCheck
)
2294 dprintf_menu(stddeb
,"SetMenuItemBitmaps(%04x, %04x, %04x, %04x, %04x)\n",
2295 hMenu
, nPos
, wFlags
, hNewCheck
, hNewUnCheck
);
2296 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2298 if (!hNewCheck
&& !hNewUnCheck
)
2300 /* If both are NULL, restore default bitmaps */
2301 item
->hCheckBit
= hStdCheck
;
2302 item
->hUnCheckBit
= 0;
2303 item
->item_flags
&= ~MF_USECHECKBITMAPS
;
2305 else /* Install new bitmaps */
2307 item
->hCheckBit
= hNewCheck
;
2308 item
->hUnCheckBit
= hNewUnCheck
;
2309 item
->item_flags
|= MF_USECHECKBITMAPS
;
2315 /**********************************************************************
2316 * CreateMenu [USER.151]
2322 dprintf_menu(stddeb
,"CreateMenu !\n");
2323 if (!(hMenu
= USER_HEAP_ALLOC( sizeof(POPUPMENU
) )))
2325 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2327 menu
->wMagic
= MENU_MAGIC
;
2334 menu
->FocusedItem
= NO_SELECTED_ITEM
;
2335 dprintf_menu(stddeb
,"CreateMenu // return %04x\n", hMenu
);
2340 /**********************************************************************
2341 * DestroyMenu [USER.152]
2343 BOOL
DestroyMenu(HMENU hMenu
)
2346 dprintf_menu(stddeb
,"DestroyMenu (%04x) !\n", hMenu
);
2348 if (hMenu
== 0) return FALSE
;
2349 /* Silently ignore attempts to destroy default system menu */
2350 if (hMenu
== MENU_DefSysMenu
) return TRUE
;
2351 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2352 if (!lppop
|| (lppop
->wMagic
!= MENU_MAGIC
)) return FALSE
;
2353 lppop
->wMagic
= 0; /* Mark it as destroyed */
2354 if ((lppop
->wFlags
& MF_POPUP
) && lppop
->hWnd
&& lppop
->hWnd
!= pTopPWnd
->hwndSelf
)
2355 DestroyWindow( lppop
->hWnd
);
2360 MENUITEM
*item
= lppop
->items
;
2361 for (i
= lppop
->nItems
; i
> 0; i
--, item
++)
2363 if (item
->item_flags
& MF_POPUP
)
2364 DestroyMenu( (HMENU
)item
->item_id
);
2365 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2366 HeapFree( SystemHeap
, 0, item
->text
);
2368 HeapFree( SystemHeap
, 0, lppop
->items
);
2370 USER_HEAP_FREE( hMenu
);
2371 dprintf_menu(stddeb
,"DestroyMenu (%04x) // End !\n", hMenu
);
2375 /**********************************************************************
2376 * GetSystemMenu [USER.156]
2378 HMENU
GetSystemMenu(HWND hWnd
, BOOL bRevert
)
2380 WND
*wndPtr
= WIN_FindWndPtr( hWnd
);
2381 if (!wndPtr
) return 0;
2383 if (!wndPtr
->hSysMenu
|| (wndPtr
->hSysMenu
== MENU_DefSysMenu
))
2385 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2386 return wndPtr
->hSysMenu
;
2388 if (!bRevert
) return wndPtr
->hSysMenu
;
2389 if (wndPtr
->hSysMenu
) DestroyMenu(wndPtr
->hSysMenu
);
2390 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2391 return wndPtr
->hSysMenu
;
2395 /*******************************************************************
2396 * SetSystemMenu (USER.280)
2398 BOOL
SetSystemMenu( HWND hwnd
, HMENU hMenu
)
2402 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
2403 if (wndPtr
->hSysMenu
&& (wndPtr
->hSysMenu
!= MENU_DefSysMenu
))
2404 DestroyMenu( wndPtr
->hSysMenu
);
2405 wndPtr
->hSysMenu
= hMenu
;
2410 /**********************************************************************
2411 * GetMenu [USER.157]
2413 HMENU
GetMenu(HWND hWnd
)
2415 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2416 if (wndPtr
== NULL
) return 0;
2417 return (HMENU
)wndPtr
->wIDmenu
;
2421 /**********************************************************************
2422 * SetMenu [USER.158]
2424 BOOL
SetMenu(HWND hWnd
, HMENU hMenu
)
2427 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2428 if (wndPtr
== NULL
) {
2429 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad window handle !\n",
2433 dprintf_menu(stddeb
,"SetMenu(%04x, %04x);\n", hWnd
, hMenu
);
2434 if (GetCapture() == hWnd
) ReleaseCapture();
2435 wndPtr
->wIDmenu
= (UINT
)hMenu
;
2438 lpmenu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2439 if (lpmenu
== NULL
) {
2440 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad menu handle !\n",
2444 lpmenu
->hWnd
= hWnd
;
2445 lpmenu
->wFlags
&= ~MF_POPUP
; /* Can't be a popup */
2446 lpmenu
->Height
= 0; /* Make sure we recalculate the size */
2448 if (IsWindowVisible(hWnd
))
2449 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2450 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2456 /**********************************************************************
2457 * GetSubMenu [USER.159]
2459 HMENU
GetSubMenu(HMENU hMenu
, short nPos
)
2463 dprintf_menu(stddeb
,"GetSubMenu (%04x, %04X) !\n", hMenu
, nPos
);
2464 if (!(lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return 0;
2465 if ((UINT
)nPos
>= lppop
->nItems
) return 0;
2466 if (!(lppop
->items
[nPos
].item_flags
& MF_POPUP
)) return 0;
2467 return (HMENU
)lppop
->items
[nPos
].item_id
;
2471 /**********************************************************************
2472 * DrawMenuBar [USER.160]
2474 void DrawMenuBar(HWND hWnd
)
2478 dprintf_menu(stddeb
,"DrawMenuBar (%04x)\n", hWnd
);
2479 wndPtr
= WIN_FindWndPtr(hWnd
);
2480 if (wndPtr
!= NULL
&& (wndPtr
->dwStyle
& WS_CHILD
) == 0 &&
2481 wndPtr
->wIDmenu
!= 0) {
2482 dprintf_menu(stddeb
,"DrawMenuBar wIDmenu=%04X \n",
2484 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
);
2485 if (lppop
== NULL
) return;
2487 lppop
->Height
= 0; /* Make sure we call MENU_MenuBarCalcSize */
2488 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2489 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2494 /***********************************************************************
2495 * EndMenu (USER.187)
2499 /* FIXME: this won't work when we have multiple tasks... */
2500 fEndMenuCalled
= TRUE
;
2504 /***********************************************************************
2505 * LookupMenuHandle (USER.217)
2507 HMENU
LookupMenuHandle( HMENU hmenu
, INT id
)
2509 if (!MENU_FindItem( &hmenu
, &id
, MF_BYCOMMAND
)) return 0;
2514 /**********************************************************************
2515 * LoadMenu (USER.150)
2517 HMENU16
LoadMenu16( HINSTANCE16 instance
, SEGPTR name
)
2525 char *str
= (char *)PTR_SEG_TO_LIN( name
);
2526 dprintf_menu( stddeb
, "LoadMenu(%04x,'%s')\n", instance
, str
);
2527 if (str
[0] == '#') name
= (SEGPTR
)atoi( str
+ 1 );
2530 dprintf_resource(stddeb
,"LoadMenu(%04x,%04x)\n",instance
,LOWORD(name
));
2532 if (!name
) return 0;
2534 /* check for Win32 module */
2535 instance
= GetExePtr( instance
);
2536 if (MODULE_GetPtr(instance
)->flags
& NE_FFLAGS_WIN32
)
2537 return LoadMenu32A(instance
,PTR_SEG_TO_LIN(name
));
2539 if (!(hRsrc
= FindResource16( instance
, name
, RT_MENU
))) return 0;
2540 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
2541 hMenu
= LoadMenuIndirect16(LockResource16(handle
));
2542 FreeResource16( handle
);
2547 /*****************************************************************
2548 * LoadMenu32A (USER32.370)
2550 HMENU32
LoadMenu32A( HINSTANCE32 instance
, LPCSTR name
)
2552 HRSRC32 hrsrc
= FindResource32A( instance
, name
, (LPSTR
)RT_MENU
);
2553 if (!hrsrc
) return 0;
2554 return LoadMenuIndirect32A( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2558 /*****************************************************************
2559 * LoadMenu32W (USER32.372)
2561 HMENU32
LoadMenu32W( HINSTANCE32 instance
, LPCWSTR name
)
2563 HRSRC32 hrsrc
= FindResource32W( instance
, name
, (LPWSTR
)RT_MENU
);
2564 if (!hrsrc
) return 0;
2565 return LoadMenuIndirect32W( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2569 /**********************************************************************
2570 * LoadMenuIndirect16 (USER.220)
2572 HMENU16
LoadMenuIndirect16( LPCVOID
template )
2575 WORD version
, offset
;
2576 LPCSTR p
= (LPCSTR
)template;
2578 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2579 version
= GET_WORD(p
);
2583 fprintf( stderr
, "LoadMenuIndirect16: version must be 0 for Win16\n" );
2586 offset
= GET_WORD(p
);
2587 p
+= sizeof(WORD
) + offset
;
2588 if (!(hMenu
= CreateMenu())) return 0;
2589 if (!MENU_ParseResource( p
, hMenu
, FALSE
))
2591 DestroyMenu( hMenu
);
2598 /**********************************************************************
2599 * LoadMenuIndirect32A (USER32.370)
2601 HMENU32
LoadMenuIndirect32A( LPCVOID
template )
2604 WORD version
, offset
;
2605 LPCSTR p
= (LPCSTR
)template;
2607 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2608 version
= GET_WORD(p
);
2612 fprintf( stderr
, "LoadMenuIndirect32A: version %d not supported.\n",
2616 offset
= GET_WORD(p
);
2617 p
+= sizeof(WORD
) + offset
;
2618 if (!(hMenu
= CreateMenu())) return 0;
2619 if (!MENU_ParseResource( p
, hMenu
, TRUE
))
2621 DestroyMenu( hMenu
);
2628 /**********************************************************************
2629 * LoadMenuIndirect32W (USER32.371)
2631 HMENU32
LoadMenuIndirect32W( LPCVOID
template )
2633 /* FIXME: is there anything different between A and W? */
2634 return LoadMenuIndirect32A( template );
2638 /**********************************************************************
2641 BOOL
IsMenu( HMENU hmenu
)
2644 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
2645 return (menu
->wMagic
== MENU_MAGIC
);