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
))
1536 TranslateMessage( msg
);
1539 if ((msg
->message
>= WM_MOUSEFIRST
) && (msg
->message
<= WM_MOUSELAST
))
1541 /* Find the sub-popup for this mouse event (if any) */
1542 HMENU hsubmenu
= MENU_FindMenuByCoords( hmenu
, msg
->pt
);
1544 switch(msg
->message
)
1546 case WM_RBUTTONDOWN
:
1547 case WM_NCRBUTTONDOWN
:
1548 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1550 case WM_LBUTTONDOWN
:
1551 case WM_NCLBUTTONDOWN
:
1552 fClosed
= !MENU_ButtonDown( hwnd
, hsubmenu
,
1553 &hmenuCurrent
, msg
->pt
);
1557 case WM_NCRBUTTONUP
:
1558 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1561 case WM_NCLBUTTONUP
:
1562 /* If outside all menus but inside lprect, ignore it */
1563 if (!hsubmenu
&& lprect
&& PtInRect16(lprect
, msg
->pt
)) break;
1564 fClosed
= !MENU_ButtonUp( hwnd
, hsubmenu
,
1565 &hmenuCurrent
, msg
->pt
);
1566 fRemove
= TRUE
; /* Remove event even if outside menu */
1570 case WM_NCMOUSEMOVE
:
1571 if ((msg
->wParam
& MK_LBUTTON
) ||
1572 ((wFlags
& TPM_RIGHTBUTTON
) && (msg
->wParam
& MK_RBUTTON
)))
1574 fClosed
= !MENU_MouseMove( hwnd
, hsubmenu
,
1575 &hmenuCurrent
, msg
->pt
);
1580 else if ((msg
->message
>= WM_KEYFIRST
) && (msg
->message
<= WM_KEYLAST
))
1582 fRemove
= TRUE
; /* Keyboard messages are always removed */
1583 switch(msg
->message
)
1589 MENU_SelectItem( hwnd
, hmenuCurrent
, NO_SELECTED_ITEM
, FALSE
);
1590 MENU_SelectNextItem( hwnd
, hmenuCurrent
);
1594 MENU_SelectItem( hwnd
, hmenuCurrent
, NO_SELECTED_ITEM
, FALSE
);
1595 MENU_SelectPrevItem( hwnd
, hmenuCurrent
);
1599 MENU_SelectPrevItem( hwnd
, hmenuCurrent
);
1603 /* If on menu bar, pull-down the menu */
1604 if (!(menu
->wFlags
& MF_POPUP
) && (hmenuCurrent
== hmenu
))
1605 hmenuCurrent
= MENU_ShowSubPopup( hwnd
, hmenu
, TRUE
);
1607 MENU_SelectNextItem( hwnd
, hmenuCurrent
);
1611 MENU_KeyLeft( hwnd
, hmenu
, &hmenuCurrent
);
1615 MENU_KeyRight( hwnd
, hmenu
, &hmenuCurrent
);
1620 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1631 break; /* WM_KEYDOWN */
1641 break; /* WM_SYSKEYDOWN */
1645 /* Hack to avoid control chars. */
1646 /* We will find a better way real soon... */
1647 if ((msg
->wParam
<= 32) || (msg
->wParam
>= 127)) break;
1648 pos
= MENU_FindItemByKey( hwnd
, hmenuCurrent
, msg
->wParam
);
1649 if (pos
== (UINT
)-2) fClosed
= TRUE
;
1650 else if (pos
== (UINT
)-1) MessageBeep(0);
1653 MENU_SelectItem( hwnd
, hmenuCurrent
, pos
, TRUE
);
1654 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1659 break; /* WM_CHAR */
1660 } /* switch(msg->message) */
1664 DispatchMessage( msg
);
1666 if (fEndMenuCalled
) fClosed
= TRUE
;
1667 if (!fClosed
) fRemove
= TRUE
;
1669 if (fRemove
) /* Remove the message from the queue */
1670 PeekMessage16( msg
, 0, msg
->message
, msg
->message
, PM_REMOVE
);
1672 USER_HEAP_FREE( hMsg
);
1674 MENU_HideSubPopups( hwnd
, hmenu
, FALSE
);
1675 if (menu
->wFlags
& MF_POPUP
)
1677 ShowWindow( menu
->hWnd
, SW_HIDE
);
1680 MENU_SelectItem( hwnd
, hmenu
, NO_SELECTED_ITEM
, FALSE
);
1681 SendMessage16( hwnd
, WM_MENUSELECT
, 0, MAKELONG( 0xffff, 0 ) );
1682 fEndMenuCalled
= FALSE
;
1687 /***********************************************************************
1688 * MENU_TrackMouseMenuBar
1690 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
1692 void MENU_TrackMouseMenuBar( HWND hwnd
, POINT16 pt
)
1694 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1696 SendMessage16( hwnd
, WM_ENTERMENULOOP
, 0, 0 );
1697 SendMessage16( hwnd
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1698 MENU_TrackMenu( (HMENU
)wndPtr
->wIDmenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1699 pt
.x
, pt
.y
, hwnd
, NULL
);
1700 SendMessage16( hwnd
, WM_EXITMENULOOP
, 0, 0 );
1705 /***********************************************************************
1706 * MENU_TrackKbdMenuBar
1708 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
1710 void MENU_TrackKbdMenuBar( WND
* wndPtr
, UINT wParam
, INT vkey
)
1712 UINT uItem
= NO_SELECTED_ITEM
;
1715 /* find window that has a menu
1718 if( !(wndPtr
->dwStyle
& WS_CHILD
) )
1720 wndPtr
= WIN_FindWndPtr( GetActiveWindow() );
1721 if( !wndPtr
) return;
1724 while( wndPtr
->dwStyle
& WS_CHILD
&&
1725 !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1726 if( !(wndPtr
= wndPtr
->parent
) ) return;
1728 if( wndPtr
->dwStyle
& WS_CHILD
|| !wndPtr
->wIDmenu
)
1729 if( !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1732 hTrackMenu
= ( IsMenu( wndPtr
->wIDmenu
) )? wndPtr
->wIDmenu
:
1736 SendMessage16( wndPtr
->hwndSelf
, WM_ENTERMENULOOP
, 0, 0 );
1737 SendMessage16( wndPtr
->hwndSelf
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1739 /* find suitable menu entry
1742 if( vkey
== VK_SPACE
)
1743 uItem
= SYSMENU_SELECTED
;
1746 uItem
= MENU_FindItemByKey( wndPtr
->hwndSelf
, wndPtr
->wIDmenu
, vkey
);
1747 if( uItem
>= 0xFFFE )
1749 if( uItem
== 0xFFFF )
1751 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1757 MENU_SelectItem( wndPtr
->hwndSelf
, hTrackMenu
, uItem
, TRUE
);
1758 if( uItem
== NO_SELECTED_ITEM
)
1759 MENU_SelectNextItem( wndPtr
->hwndSelf
, hTrackMenu
);
1761 PostMessage( wndPtr
->hwndSelf
, WM_KEYDOWN
, VK_DOWN
, 0L );
1763 MENU_TrackMenu( hTrackMenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1764 0, 0, wndPtr
->hwndSelf
, NULL
);
1766 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1771 /**********************************************************************
1772 * TrackPopupMenu16 (USER.416)
1774 BOOL16
TrackPopupMenu16( HMENU16 hMenu
, UINT16 wFlags
, INT16 x
, INT16 y
,
1775 INT16 nReserved
, HWND16 hWnd
, const RECT16
*lpRect
)
1780 if (MENU_ShowPopup( hWnd
, hMenu
, 0, x
, y
))
1781 ret
= MENU_TrackMenu( hMenu
, wFlags
, 0, 0, hWnd
, lpRect
);
1787 /**********************************************************************
1788 * TrackPopupMenu32 (USER32.548)
1790 BOOL32
TrackPopupMenu32( HMENU32 hMenu
, UINT32 wFlags
, INT32 x
, INT32 y
,
1791 INT32 nReserved
, HWND32 hWnd
, const RECT32
*lpRect
)
1794 CONV_RECT32TO16( lpRect
, &r
);
1795 return TrackPopupMenu16( hMenu
, wFlags
, x
, y
, nReserved
, hWnd
, &r
);
1799 /***********************************************************************
1802 LRESULT
PopupMenuWndProc(HWND hwnd
,UINT message
,WPARAM wParam
,LPARAM lParam
)
1808 CREATESTRUCT16
*cs
= (CREATESTRUCT16
*)PTR_SEG_TO_LIN(lParam
);
1809 SetWindowLong32A( hwnd
, 0, (LONG
)cs
->lpCreateParams
);
1813 case WM_MOUSEACTIVATE
: /* We don't want to be activated */
1814 return MA_NOACTIVATE
;
1819 BeginPaint16( hwnd
, &ps
);
1820 MENU_DrawPopupMenu( hwnd
, ps
.hdc
,
1821 (HMENU
)GetWindowLong32A( hwnd
, 0 ) );
1822 EndPaint16( hwnd
, &ps
);
1827 /* zero out global pointer in case system popup
1828 * was destroyed by AppExit
1831 if( hwnd
== pTopPWnd
->hwndSelf
)
1838 if (wParam
) SetWindowLong32A( hwnd
, 0, (HMENU
)wParam
);
1841 return DefWindowProc16(hwnd
, message
, wParam
, lParam
);
1847 /***********************************************************************
1848 * MENU_GetMenuBarHeight
1850 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
1852 UINT
MENU_GetMenuBarHeight( HWND hwnd
, UINT menubarWidth
, int orgX
, int orgY
)
1859 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
1860 if (!(lppop
= (LPPOPUPMENU
)USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
)))
1862 hdc
= GetDC( hwnd
);
1863 SetRect16(&rectBar
, orgX
, orgY
, orgX
+menubarWidth
, orgY
+SYSMETRICS_CYMENU
);
1864 MENU_MenuBarCalcSize( hdc
, &rectBar
, lppop
, hwnd
);
1865 ReleaseDC( hwnd
, hdc
);
1866 return lppop
->Height
;
1870 /*******************************************************************
1871 * ChangeMenu16 (USER.153)
1873 BOOL16
ChangeMenu16( HMENU16 hMenu
, UINT16 pos
, SEGPTR data
,
1874 UINT16 id
, UINT16 flags
)
1876 dprintf_menu( stddeb
,"ChangeMenu16: menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
1877 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1878 if (flags
& MF_APPEND
) return AppendMenu16( hMenu
, flags
& ~MF_APPEND
,
1880 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
1881 /* for MF_DELETE. We should check the parameters for all others */
1882 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
1883 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1884 if (flags
& MF_CHANGE
) return ModifyMenu16( hMenu
, pos
, flags
& ~MF_CHANGE
,
1886 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1887 flags
& MF_BYPOSITION
? pos
: id
,
1888 flags
& ~MF_REMOVE
);
1889 /* Default: MF_INSERT */
1890 return InsertMenu16( hMenu
, pos
, flags
, id
, data
);
1894 /*******************************************************************
1895 * ChangeMenu32A (USER32.22)
1897 BOOL32
ChangeMenu32A( HMENU32 hMenu
, UINT32 pos
, LPCSTR data
,
1898 UINT32 id
, UINT32 flags
)
1900 dprintf_menu( stddeb
,"ChangeMenu32A: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1901 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1902 if (flags
& MF_APPEND
) return AppendMenu32A( hMenu
, flags
& ~MF_APPEND
,
1904 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1905 if (flags
& MF_CHANGE
) return ModifyMenu32A(hMenu
, pos
, flags
& ~MF_CHANGE
,
1907 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1908 flags
& MF_BYPOSITION
? pos
: id
,
1909 flags
& ~MF_REMOVE
);
1910 /* Default: MF_INSERT */
1911 return InsertMenu32A( hMenu
, pos
, flags
, id
, data
);
1915 /*******************************************************************
1916 * ChangeMenu32W (USER32.23)
1918 BOOL32
ChangeMenu32W( HMENU32 hMenu
, UINT32 pos
, LPCWSTR data
,
1919 UINT32 id
, UINT32 flags
)
1921 dprintf_menu( stddeb
,"ChangeMenu32W: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1922 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1923 if (flags
& MF_APPEND
) return AppendMenu32W( hMenu
, flags
& ~MF_APPEND
,
1925 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1926 if (flags
& MF_CHANGE
) return ModifyMenu32W(hMenu
, pos
, flags
& ~MF_CHANGE
,
1928 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1929 flags
& MF_BYPOSITION
? pos
: id
,
1930 flags
& ~MF_REMOVE
);
1931 /* Default: MF_INSERT */
1932 return InsertMenu32W( hMenu
, pos
, flags
, id
, data
);
1936 /*******************************************************************
1937 * CheckMenuItem (USER.154)
1939 INT
CheckMenuItem( HMENU hMenu
, UINT id
, UINT flags
)
1944 dprintf_menu( stddeb
,"CheckMenuItem: %04x %04x %04x\n", hMenu
, id
, flags
);
1945 if (!(item
= MENU_FindItem( &hMenu
, &id
, flags
))) return -1;
1946 ret
= item
->item_flags
& MF_CHECKED
;
1947 if (flags
& MF_CHECKED
) item
->item_flags
|= MF_CHECKED
;
1948 else item
->item_flags
&= ~MF_CHECKED
;
1953 /**********************************************************************
1954 * EnableMenuItem [USER.155]
1956 BOOL
EnableMenuItem(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
1959 dprintf_menu(stddeb
,"EnableMenuItem (%04x, %04X, %04X) !\n",
1960 hMenu
, wItemID
, wFlags
);
1961 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return FALSE
;
1963 /* We can't have MF_GRAYED and MF_DISABLED together */
1964 if (wFlags
& MF_GRAYED
)
1966 item
->item_flags
= (item
->item_flags
& ~MF_DISABLED
) | MF_GRAYED
;
1968 else if (wFlags
& MF_DISABLED
)
1970 item
->item_flags
= (item
->item_flags
& ~MF_GRAYED
) | MF_DISABLED
;
1972 else /* MF_ENABLED */
1974 item
->item_flags
&= ~(MF_GRAYED
| MF_DISABLED
);
1980 /*******************************************************************
1981 * GetMenuString (USER.161)
1983 int GetMenuString( HMENU hMenu
, UINT wItemID
,
1984 LPSTR str
, short nMaxSiz
, UINT wFlags
)
1988 dprintf_menu( stddeb
, "GetMenuString: menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
1989 hMenu
, wItemID
, str
, nMaxSiz
, wFlags
);
1990 if (!str
|| !nMaxSiz
) return 0;
1992 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return 0;
1993 if (!IS_STRING_ITEM(item
->item_flags
)) return 0;
1994 lstrcpyn32A( str
, item
->text
, nMaxSiz
);
1995 dprintf_menu( stddeb
, "GetMenuString: returning '%s'\n", str
);
2000 /**********************************************************************
2001 * HiliteMenuItem [USER.162]
2003 BOOL
HiliteMenuItem(HWND hWnd
, HMENU hMenu
, UINT wItemID
, UINT wHilite
)
2006 dprintf_menu(stddeb
,"HiliteMenuItem(%04x, %04x, %04x, %04x);\n",
2007 hWnd
, hMenu
, wItemID
, wHilite
);
2008 if (!MENU_FindItem( &hMenu
, &wItemID
, wHilite
)) return FALSE
;
2009 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2010 if (menu
->FocusedItem
== wItemID
) return TRUE
;
2011 MENU_HideSubPopups( hWnd
, hMenu
, FALSE
);
2012 MENU_SelectItem( hWnd
, hMenu
, wItemID
, TRUE
);
2017 /**********************************************************************
2018 * GetMenuState [USER.250]
2020 UINT
GetMenuState(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
2023 dprintf_menu(stddeb
,"GetMenuState(%04x, %04x, %04x);\n",
2024 hMenu
, wItemID
, wFlags
);
2025 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return -1;
2026 if (item
->item_flags
& MF_POPUP
)
2028 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( (HMENU
)item
->item_id
);
2029 if (!menu
) return -1;
2030 else return (menu
->nItems
<< 8) | (menu
->wFlags
& 0xff);
2032 else return item
->item_flags
;
2036 /**********************************************************************
2037 * GetMenuItemCount [USER.263]
2039 INT
GetMenuItemCount(HMENU hMenu
)
2042 dprintf_menu(stddeb
,"GetMenuItemCount(%04x);\n", hMenu
);
2043 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2044 if (menu
== NULL
) return (UINT
)-1;
2045 dprintf_menu(stddeb
,"GetMenuItemCount(%04x) return %d \n",
2046 hMenu
, menu
->nItems
);
2047 return menu
->nItems
;
2051 /**********************************************************************
2052 * GetMenuItemID [USER.264]
2054 UINT
GetMenuItemID(HMENU hMenu
, int nPos
)
2058 dprintf_menu(stddeb
,"GetMenuItemID(%04x, %d);\n", hMenu
, nPos
);
2059 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return -1;
2060 if ((nPos
< 0) || (nPos
>= menu
->nItems
)) return -1;
2061 if (menu
->items
[nPos
].item_flags
& MF_POPUP
) return -1;
2062 return menu
->items
[nPos
].item_id
;
2066 /*******************************************************************
2067 * InsertMenu16 (USER.410)
2069 BOOL16
InsertMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2070 UINT16 id
, SEGPTR data
)
2072 if (IS_STRING_ITEM(flags
) && data
)
2073 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2074 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2075 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2079 /*******************************************************************
2080 * InsertMenu32A (USER32.321)
2082 BOOL32
InsertMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2083 UINT32 id
, LPCSTR str
)
2087 if (IS_STRING_ITEM(flags
) && str
)
2088 dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x '%s'\n",
2089 hMenu
, pos
, flags
, id
, str
);
2090 else dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x %08lx\n",
2091 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2093 if (!(item
= MENU_InsertItem( hMenu
, pos
, flags
))) return FALSE
;
2095 if (!(MENU_SetItemData( item
, flags
, id
, str
)))
2097 RemoveMenu( hMenu
, pos
, flags
);
2101 if (flags
& MF_POPUP
) /* Set the MF_POPUP flag on the popup-menu */
2102 ((POPUPMENU
*)USER_HEAP_LIN_ADDR((HMENU
)id
))->wFlags
|= MF_POPUP
;
2104 item
->hCheckBit
= hStdCheck
;
2105 item
->hUnCheckBit
= 0;
2110 /*******************************************************************
2111 * InsertMenu32W (USER32.324)
2113 BOOL32
InsertMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2114 UINT32 id
, LPCWSTR str
)
2118 if (IS_STRING_ITEM(flags
) && str
)
2120 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2121 ret
= InsertMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2125 else return InsertMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2129 /*******************************************************************
2130 * AppendMenu16 (USER.411)
2132 BOOL16
AppendMenu16( HMENU16 hMenu
, UINT16 flags
, UINT16 id
, SEGPTR data
)
2134 return InsertMenu16( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2138 /*******************************************************************
2139 * AppendMenu32A (USER32.4)
2141 BOOL32
AppendMenu32A( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCSTR data
)
2143 return InsertMenu32A( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2147 /*******************************************************************
2148 * AppendMenu32W (USER32.5)
2150 BOOL32
AppendMenu32W( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCWSTR data
)
2152 return InsertMenu32W( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2156 /**********************************************************************
2157 * RemoveMenu [USER.412]
2159 BOOL
RemoveMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2164 dprintf_menu(stddeb
,"RemoveMenu (%04x, %04x, %04x)\n",hMenu
, nPos
, wFlags
);
2165 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2166 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2170 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2171 HeapFree( SystemHeap
, 0, item
->text
);
2172 if (--menu
->nItems
== 0)
2174 HeapFree( SystemHeap
, 0, menu
->items
);
2179 while(nPos
< menu
->nItems
)
2185 menu
->items
= HeapReAlloc( SystemHeap
, 0, menu
->items
,
2186 menu
->nItems
* sizeof(MENUITEM
) );
2192 /**********************************************************************
2193 * DeleteMenu [USER.413]
2195 BOOL
DeleteMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2197 MENUITEM
*item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
);
2198 if (!item
) return FALSE
;
2199 if (item
->item_flags
& MF_POPUP
) DestroyMenu( (HMENU
)item
->item_id
);
2200 /* nPos is now the position of the item */
2201 RemoveMenu( hMenu
, nPos
, wFlags
| MF_BYPOSITION
);
2206 /*******************************************************************
2207 * ModifyMenu16 (USER.414)
2209 BOOL16
ModifyMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2210 UINT16 id
, SEGPTR data
)
2212 if (IS_STRING_ITEM(flags
))
2213 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2214 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2215 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2219 /*******************************************************************
2220 * ModifyMenu32A (USER32.396)
2222 BOOL32
ModifyMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2223 UINT32 id
, LPCSTR str
)
2226 HMENU hMenu16
= hMenu
;
2229 if (IS_STRING_ITEM(flags
))
2231 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x '%s'\n",
2232 hMenu
, pos
, flags
, id
, str
? str
: "#NULL#" );
2233 if (!str
) return FALSE
;
2237 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x %08lx\n",
2238 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2241 if (!(item
= MENU_FindItem( &hMenu16
, &pos16
, flags
))) return FALSE
;
2242 return MENU_SetItemData( item
, flags
, id
, str
);
2246 /*******************************************************************
2247 * ModifyMenu32W (USER32.397)
2249 BOOL32
ModifyMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2250 UINT32 id
, LPCWSTR str
)
2254 if (IS_STRING_ITEM(flags
) && str
)
2256 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2257 ret
= ModifyMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2261 else return ModifyMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2265 /**********************************************************************
2266 * CreatePopupMenu [USER.415]
2268 HMENU
CreatePopupMenu()
2273 if (!(hmenu
= CreateMenu())) return 0;
2274 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
2275 menu
->wFlags
|= MF_POPUP
;
2280 /**********************************************************************
2281 * GetMenuCheckMarkDimensions [USER.417]
2283 DWORD
GetMenuCheckMarkDimensions()
2285 return MAKELONG( check_bitmap_width
, check_bitmap_height
);
2289 /**********************************************************************
2290 * SetMenuItemBitmaps [USER.418]
2292 BOOL
SetMenuItemBitmaps(HMENU hMenu
, UINT nPos
, UINT wFlags
,
2293 HBITMAP hNewUnCheck
, HBITMAP hNewCheck
)
2296 dprintf_menu(stddeb
,"SetMenuItemBitmaps(%04x, %04x, %04x, %04x, %04x)\n",
2297 hMenu
, nPos
, wFlags
, hNewCheck
, hNewUnCheck
);
2298 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2300 if (!hNewCheck
&& !hNewUnCheck
)
2302 /* If both are NULL, restore default bitmaps */
2303 item
->hCheckBit
= hStdCheck
;
2304 item
->hUnCheckBit
= 0;
2305 item
->item_flags
&= ~MF_USECHECKBITMAPS
;
2307 else /* Install new bitmaps */
2309 item
->hCheckBit
= hNewCheck
;
2310 item
->hUnCheckBit
= hNewUnCheck
;
2311 item
->item_flags
|= MF_USECHECKBITMAPS
;
2317 /**********************************************************************
2318 * CreateMenu [USER.151]
2324 dprintf_menu(stddeb
,"CreateMenu !\n");
2325 if (!(hMenu
= USER_HEAP_ALLOC( sizeof(POPUPMENU
) )))
2327 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2329 menu
->wMagic
= MENU_MAGIC
;
2336 menu
->FocusedItem
= NO_SELECTED_ITEM
;
2337 dprintf_menu(stddeb
,"CreateMenu // return %04x\n", hMenu
);
2342 /**********************************************************************
2343 * DestroyMenu [USER.152]
2345 BOOL
DestroyMenu(HMENU hMenu
)
2348 dprintf_menu(stddeb
,"DestroyMenu (%04x) !\n", hMenu
);
2350 if (hMenu
== 0) return FALSE
;
2351 /* Silently ignore attempts to destroy default system menu */
2352 if (hMenu
== MENU_DefSysMenu
) return TRUE
;
2353 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2354 if (!lppop
|| (lppop
->wMagic
!= MENU_MAGIC
)) return FALSE
;
2355 lppop
->wMagic
= 0; /* Mark it as destroyed */
2356 if ((lppop
->wFlags
& MF_POPUP
) && lppop
->hWnd
&& lppop
->hWnd
!= pTopPWnd
->hwndSelf
)
2357 DestroyWindow( lppop
->hWnd
);
2362 MENUITEM
*item
= lppop
->items
;
2363 for (i
= lppop
->nItems
; i
> 0; i
--, item
++)
2365 if (item
->item_flags
& MF_POPUP
)
2366 DestroyMenu( (HMENU
)item
->item_id
);
2367 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2368 HeapFree( SystemHeap
, 0, item
->text
);
2370 HeapFree( SystemHeap
, 0, lppop
->items
);
2372 USER_HEAP_FREE( hMenu
);
2373 dprintf_menu(stddeb
,"DestroyMenu (%04x) // End !\n", hMenu
);
2377 /**********************************************************************
2378 * GetSystemMenu [USER.156]
2380 HMENU
GetSystemMenu(HWND hWnd
, BOOL bRevert
)
2382 WND
*wndPtr
= WIN_FindWndPtr( hWnd
);
2383 if (!wndPtr
) return 0;
2385 if (!wndPtr
->hSysMenu
|| (wndPtr
->hSysMenu
== MENU_DefSysMenu
))
2387 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2388 return wndPtr
->hSysMenu
;
2390 if (!bRevert
) return wndPtr
->hSysMenu
;
2391 if (wndPtr
->hSysMenu
) DestroyMenu(wndPtr
->hSysMenu
);
2392 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2393 return wndPtr
->hSysMenu
;
2397 /*******************************************************************
2398 * SetSystemMenu (USER.280)
2400 BOOL
SetSystemMenu( HWND hwnd
, HMENU hMenu
)
2404 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
2405 if (wndPtr
->hSysMenu
&& (wndPtr
->hSysMenu
!= MENU_DefSysMenu
))
2406 DestroyMenu( wndPtr
->hSysMenu
);
2407 wndPtr
->hSysMenu
= hMenu
;
2412 /**********************************************************************
2413 * GetMenu [USER.157]
2415 HMENU
GetMenu(HWND hWnd
)
2417 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2418 if (wndPtr
== NULL
) return 0;
2419 return (HMENU
)wndPtr
->wIDmenu
;
2423 /**********************************************************************
2424 * SetMenu [USER.158]
2426 BOOL
SetMenu(HWND hWnd
, HMENU hMenu
)
2429 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2430 if (wndPtr
== NULL
) {
2431 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad window handle !\n",
2435 dprintf_menu(stddeb
,"SetMenu(%04x, %04x);\n", hWnd
, hMenu
);
2436 if (GetCapture() == hWnd
) ReleaseCapture();
2437 wndPtr
->wIDmenu
= (UINT
)hMenu
;
2440 lpmenu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2441 if (lpmenu
== NULL
) {
2442 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad menu handle !\n",
2446 lpmenu
->hWnd
= hWnd
;
2447 lpmenu
->wFlags
&= ~MF_POPUP
; /* Can't be a popup */
2448 lpmenu
->Height
= 0; /* Make sure we recalculate the size */
2450 if (IsWindowVisible(hWnd
))
2451 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2452 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2458 /**********************************************************************
2459 * GetSubMenu [USER.159]
2461 HMENU
GetSubMenu(HMENU hMenu
, short nPos
)
2465 dprintf_menu(stddeb
,"GetSubMenu (%04x, %04X) !\n", hMenu
, nPos
);
2466 if (!(lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return 0;
2467 if ((UINT
)nPos
>= lppop
->nItems
) return 0;
2468 if (!(lppop
->items
[nPos
].item_flags
& MF_POPUP
)) return 0;
2469 return (HMENU
)lppop
->items
[nPos
].item_id
;
2473 /**********************************************************************
2474 * DrawMenuBar [USER.160]
2476 void DrawMenuBar(HWND hWnd
)
2480 dprintf_menu(stddeb
,"DrawMenuBar (%04x)\n", hWnd
);
2481 wndPtr
= WIN_FindWndPtr(hWnd
);
2482 if (wndPtr
!= NULL
&& (wndPtr
->dwStyle
& WS_CHILD
) == 0 &&
2483 wndPtr
->wIDmenu
!= 0) {
2484 dprintf_menu(stddeb
,"DrawMenuBar wIDmenu=%04X \n",
2486 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
);
2487 if (lppop
== NULL
) return;
2489 lppop
->Height
= 0; /* Make sure we call MENU_MenuBarCalcSize */
2490 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2491 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2496 /***********************************************************************
2497 * EndMenu (USER.187)
2501 /* FIXME: this won't work when we have multiple tasks... */
2502 fEndMenuCalled
= TRUE
;
2506 /***********************************************************************
2507 * LookupMenuHandle (USER.217)
2509 HMENU
LookupMenuHandle( HMENU hmenu
, INT id
)
2511 if (!MENU_FindItem( &hmenu
, &id
, MF_BYCOMMAND
)) return 0;
2516 /**********************************************************************
2517 * LoadMenu (USER.150)
2519 HMENU16
LoadMenu16( HINSTANCE16 instance
, SEGPTR name
)
2527 char *str
= (char *)PTR_SEG_TO_LIN( name
);
2528 dprintf_menu( stddeb
, "LoadMenu(%04x,'%s')\n", instance
, str
);
2529 if (str
[0] == '#') name
= (SEGPTR
)atoi( str
+ 1 );
2532 dprintf_resource(stddeb
,"LoadMenu(%04x,%04x)\n",instance
,LOWORD(name
));
2534 if (!name
) return 0;
2536 /* check for Win32 module */
2537 instance
= GetExePtr( instance
);
2538 if (MODULE_GetPtr(instance
)->flags
& NE_FFLAGS_WIN32
)
2539 return LoadMenu32A(instance
,PTR_SEG_TO_LIN(name
));
2541 if (!(hRsrc
= FindResource16( instance
, name
, RT_MENU
))) return 0;
2542 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
2543 hMenu
= LoadMenuIndirect16(LockResource16(handle
));
2544 FreeResource16( handle
);
2549 /*****************************************************************
2550 * LoadMenu32A (USER32.370)
2552 HMENU32
LoadMenu32A( HINSTANCE32 instance
, LPCSTR name
)
2554 HRSRC32 hrsrc
= FindResource32A( instance
, name
, (LPSTR
)RT_MENU
);
2555 if (!hrsrc
) return 0;
2556 return LoadMenuIndirect32A( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2560 /*****************************************************************
2561 * LoadMenu32W (USER32.372)
2563 HMENU32
LoadMenu32W( HINSTANCE32 instance
, LPCWSTR name
)
2565 HRSRC32 hrsrc
= FindResource32W( instance
, name
, (LPWSTR
)RT_MENU
);
2566 if (!hrsrc
) return 0;
2567 return LoadMenuIndirect32W( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2571 /**********************************************************************
2572 * LoadMenuIndirect16 (USER.220)
2574 HMENU16
LoadMenuIndirect16( LPCVOID
template )
2577 WORD version
, offset
;
2578 LPCSTR p
= (LPCSTR
)template;
2580 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2581 version
= GET_WORD(p
);
2585 fprintf( stderr
, "LoadMenuIndirect16: version must be 0 for Win16\n" );
2588 offset
= GET_WORD(p
);
2589 p
+= sizeof(WORD
) + offset
;
2590 if (!(hMenu
= CreateMenu())) return 0;
2591 if (!MENU_ParseResource( p
, hMenu
, FALSE
))
2593 DestroyMenu( hMenu
);
2600 /**********************************************************************
2601 * LoadMenuIndirect32A (USER32.370)
2603 HMENU32
LoadMenuIndirect32A( LPCVOID
template )
2606 WORD version
, offset
;
2607 LPCSTR p
= (LPCSTR
)template;
2609 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2610 version
= GET_WORD(p
);
2614 fprintf( stderr
, "LoadMenuIndirect32A: version %d not supported.\n",
2618 offset
= GET_WORD(p
);
2619 p
+= sizeof(WORD
) + offset
;
2620 if (!(hMenu
= CreateMenu())) return 0;
2621 if (!MENU_ParseResource( p
, hMenu
, TRUE
))
2623 DestroyMenu( hMenu
);
2630 /**********************************************************************
2631 * LoadMenuIndirect32W (USER32.371)
2633 HMENU32
LoadMenuIndirect32W( LPCVOID
template )
2635 /* FIXME: is there anything different between A and W? */
2636 return LoadMenuIndirect32A( template );
2640 /**********************************************************************
2643 BOOL
IsMenu( HMENU hmenu
)
2646 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
2647 return (menu
->wMagic
== MENU_MAGIC
);