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' */
66 /* Dimension of the menu bitmaps */
67 static WORD check_bitmap_width
= 0, check_bitmap_height
= 0;
68 static WORD arrow_bitmap_width
= 0, arrow_bitmap_height
= 0;
70 /* Flag set by EndMenu() to force an exit from menu tracking */
71 static BOOL fEndMenuCalled
= FALSE
;
73 /* Space between 2 menu bar items */
74 #define MENU_BAR_ITEMS_SPACE 16
76 /* Minimum width of a tab character */
77 #define MENU_TAB_SPACE 8
79 /* Height of a separator item */
80 #define SEPARATOR_HEIGHT 5
82 /* Values for menu->FocusedItem */
83 /* (other values give the position of the focused item) */
84 #define NO_SELECTED_ITEM 0xffff
85 #define SYSMENU_SELECTED 0xfffe /* Only valid on menu-bars */
87 #define IS_STRING_ITEM(flags) (!((flags) & (MF_BITMAP | MF_OWNERDRAW | \
88 MF_MENUBARBREAK | MF_MENUBREAK | MF_SEPARATOR)))
90 extern void NC_DrawSysButton(HWND hwnd
, HDC hdc
, BOOL down
); /* nonclient.c */
91 extern BOOL
NC_GetSysPopupPos(WND
* wndPtr
, RECT16
* rect
);
93 static HBITMAP hStdCheck
= 0;
94 static HBITMAP hStdMnArrow
= 0;
95 static HMENU MENU_DefSysMenu
= 0; /* Default system menu */
98 /* we _can_ use global popup window because there's no way 2 menues can
99 * be tracked at the same time.
102 static WND
* pTopPWnd
= 0;
103 static UINT uSubPWndLevel
= 0;
106 /**********************************************************************
109 * Load a copy of the system menu.
111 static HMENU
MENU_CopySysMenu(void)
117 if (!(handle
= SYSRES_LoadResource( SYSRES_MENU_SYSMENU
))) return 0;
118 hMenu
= LoadMenuIndirect16( GlobalLock16( handle
) );
119 SYSRES_FreeResource( handle
);
122 dprintf_menu(stddeb
,"No SYSMENU\n");
125 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR(hMenu
);
126 menu
->wFlags
|= MF_SYSMENU
| MF_POPUP
;
127 dprintf_menu(stddeb
,"CopySysMenu hMenu=%04x !\n", hMenu
);
132 /***********************************************************************
135 * Menus initialisation.
143 if (!(hStdCheck
= LoadBitmap16( 0, MAKEINTRESOURCE(OBM_CHECK
) )))
145 GetObject16( hStdCheck
, sizeof(bm
), &bm
);
146 check_bitmap_width
= bm
.bmWidth
;
147 check_bitmap_height
= bm
.bmHeight
;
148 if (!(hStdMnArrow
= LoadBitmap16( 0, MAKEINTRESOURCE(OBM_MNARROW
) )))
150 GetObject16( hStdMnArrow
, sizeof(bm
), &bm
);
151 arrow_bitmap_width
= bm
.bmWidth
;
152 arrow_bitmap_height
= bm
.bmHeight
;
154 if (!(MENU_DefSysMenu
= MENU_CopySysMenu()))
156 fprintf( stderr
, "Unable to create default system menu\n" );
163 /***********************************************************************
166 * Return the default system menu.
168 HMENU
MENU_GetDefSysMenu(void)
170 return MENU_DefSysMenu
;
174 /***********************************************************************
177 * Check whether the window owning the menu bar has a system menu.
179 static BOOL
MENU_HasSysMenu( POPUPMENU
*menu
)
183 if (menu
->wFlags
& MF_POPUP
) return FALSE
;
184 if (!(wndPtr
= WIN_FindWndPtr( menu
->hWnd
))) return FALSE
;
185 return (wndPtr
->dwStyle
& WS_SYSMENU
) != 0;
189 /***********************************************************************
192 * Check whether the point (in screen coords) is in the system menu
193 * of the window owning the given menu.
195 static BOOL
MENU_IsInSysMenu( POPUPMENU
*menu
, POINT16 pt
)
199 if (menu
->wFlags
& MF_POPUP
) return FALSE
;
200 if (!(wndPtr
= WIN_FindWndPtr( menu
->hWnd
))) return FALSE
;
201 if (!(wndPtr
->dwStyle
& WS_SYSMENU
)) return FALSE
;
202 if ((pt
.x
< wndPtr
->rectClient
.left
) ||
203 (pt
.x
>= wndPtr
->rectClient
.left
+SYSMETRICS_CXSIZE
+SYSMETRICS_CXBORDER
))
205 if ((pt
.y
>= wndPtr
->rectClient
.top
- menu
->Height
) ||
206 (pt
.y
< wndPtr
->rectClient
.top
- menu
->Height
-
207 SYSMETRICS_CYSIZE
- SYSMETRICS_CYBORDER
)) return FALSE
;
212 /***********************************************************************
215 * Find a menu item. Return a pointer on the item, and modifies *hmenu
216 * in case the item was in a sub-menu.
218 static MENUITEM
*MENU_FindItem( HMENU
*hmenu
, UINT
*nPos
, UINT wFlags
)
223 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR(*hmenu
))) return NULL
;
224 if (wFlags
& MF_BYPOSITION
)
226 if (*nPos
>= menu
->nItems
) return NULL
;
227 return &menu
->items
[*nPos
];
231 MENUITEM
*item
= menu
->items
;
232 for (i
= 0; i
< menu
->nItems
; i
++, item
++)
234 if (item
->item_id
== *nPos
)
239 else if (item
->item_flags
& MF_POPUP
)
241 HMENU hsubmenu
= (HMENU
)item
->item_id
;
242 MENUITEM
*subitem
= MENU_FindItem( &hsubmenu
, nPos
, wFlags
);
255 /***********************************************************************
256 * MENU_FindItemByCoords
258 * Find the item at the specified coordinates (screen coords).
260 static MENUITEM
*MENU_FindItemByCoords( POPUPMENU
*menu
, int x
, int y
, UINT
*pos
)
266 if (!(wndPtr
= WIN_FindWndPtr( menu
->hWnd
))) return NULL
;
267 x
-= wndPtr
->rectWindow
.left
;
268 y
-= wndPtr
->rectWindow
.top
;
270 for (i
= 0; i
< menu
->nItems
; i
++, item
++)
272 if ((x
>= item
->rect
.left
) && (x
< item
->rect
.right
) &&
273 (y
>= item
->rect
.top
) && (y
< item
->rect
.bottom
))
283 /***********************************************************************
286 * Find the menu item selected by a key press.
287 * Return item id, -1 if none, -2 if we should close the menu.
289 static UINT
MENU_FindItemByKey( HWND hwndOwner
, HMENU hmenu
, UINT key
)
296 if (!IsMenu( hmenu
)) hmenu
= WIN_FindWndPtr(hwndOwner
)->hSysMenu
;
297 if (!hmenu
) return -1;
299 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
302 for (i
= 0; i
< menu
->nItems
; i
++, item
++)
304 if (IS_STRING_ITEM(item
->item_flags
))
306 char *p
= strchr( item
->text
, '&' );
307 if (p
&& (p
[1] != '&') && (toupper(p
[1]) == key
)) return i
;
310 menuchar
= SendMessage32A( hwndOwner
, WM_MENUCHAR
,
311 MAKEWPARAM( key
, menu
->wFlags
), hmenu
);
312 if (HIWORD(menuchar
) == 2) return LOWORD(menuchar
);
313 if (HIWORD(menuchar
) == 1) return -2;
318 /***********************************************************************
321 * Calculate the size of the menu item and store it in lpitem->rect.
323 static void MENU_CalcItemSize( HDC hdc
, MENUITEM
*lpitem
, HWND hwndOwner
,
324 int orgX
, int orgY
, BOOL menuBar
)
329 SetRect16( &lpitem
->rect
, orgX
, orgY
, orgX
, orgY
);
331 if (lpitem
->item_flags
& MF_OWNERDRAW
)
333 MEASUREITEMSTRUCT16
*mis
;
334 if (!(mis
= SEGPTR_NEW(MEASUREITEMSTRUCT16
))) return;
335 mis
->CtlType
= ODT_MENU
;
336 mis
->itemID
= lpitem
->item_id
;
337 mis
->itemData
= (DWORD
)lpitem
->text
;
338 mis
->itemHeight
= 16;
340 SendMessage16( hwndOwner
, WM_MEASUREITEM
, 0, (LPARAM
)SEGPTR_GET(mis
) );
341 lpitem
->rect
.bottom
+= mis
->itemHeight
;
342 lpitem
->rect
.right
+= mis
->itemWidth
;
343 dprintf_menu( stddeb
, "DrawMenuItem: MeasureItem %04x %dx%d!\n",
344 lpitem
->item_id
, mis
->itemWidth
, mis
->itemHeight
);
349 if (lpitem
->item_flags
& MF_SEPARATOR
)
351 lpitem
->rect
.bottom
+= SEPARATOR_HEIGHT
;
357 lpitem
->rect
.right
+= 2 * check_bitmap_width
;
358 if (lpitem
->item_flags
& MF_POPUP
)
359 lpitem
->rect
.right
+= arrow_bitmap_width
;
362 if (lpitem
->item_flags
& MF_BITMAP
)
365 if (GetObject16( (HBITMAP16
)(UINT32
)lpitem
->text
, sizeof(bm
), &bm
))
367 lpitem
->rect
.right
+= bm
.bmWidth
;
368 lpitem
->rect
.bottom
+= bm
.bmHeight
;
373 /* If we get here, then it must be a text item */
375 if (IS_STRING_ITEM( lpitem
->item_flags
))
377 dwSize
= GetTextExtent( hdc
, lpitem
->text
, strlen(lpitem
->text
) );
378 lpitem
->rect
.right
+= LOWORD(dwSize
);
379 lpitem
->rect
.bottom
+= MAX( HIWORD(dwSize
), SYSMETRICS_CYMENU
);
382 if (menuBar
) lpitem
->rect
.right
+= MENU_BAR_ITEMS_SPACE
;
383 else if ((p
= strchr( lpitem
->text
, '\t' )) != NULL
)
385 /* Item contains a tab (only meaningful in popup menus) */
386 lpitem
->xTab
= check_bitmap_width
+ MENU_TAB_SPACE
+
387 LOWORD( GetTextExtent( hdc
, lpitem
->text
,
388 (int)(p
- lpitem
->text
) ));
389 lpitem
->rect
.right
+= MENU_TAB_SPACE
;
393 if (strchr( lpitem
->text
, '\b' ))
394 lpitem
->rect
.right
+= MENU_TAB_SPACE
;
395 lpitem
->xTab
= lpitem
->rect
.right
- check_bitmap_width
396 - arrow_bitmap_width
;
402 /***********************************************************************
403 * MENU_PopupMenuCalcSize
405 * Calculate the size of a popup menu.
407 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop
, HWND hwndOwner
)
412 int orgX
, orgY
, maxX
, maxTab
, maxTabWidth
;
414 lppop
->Width
= lppop
->Height
= 0;
415 if (lppop
->nItems
== 0) return;
418 while (start
< lppop
->nItems
)
420 lpitem
= &lppop
->items
[start
];
423 maxTab
= maxTabWidth
= 0;
425 /* Parse items until column break or end of menu */
426 for (i
= start
; i
< lppop
->nItems
; i
++, lpitem
++)
429 (lpitem
->item_flags
& (MF_MENUBREAK
| MF_MENUBARBREAK
))) break;
430 MENU_CalcItemSize( hdc
, lpitem
, hwndOwner
, orgX
, orgY
, FALSE
);
431 if (lpitem
->item_flags
& MF_MENUBARBREAK
) orgX
++;
432 maxX
= MAX( maxX
, lpitem
->rect
.right
);
433 orgY
= lpitem
->rect
.bottom
;
434 if (IS_STRING_ITEM(lpitem
->item_flags
) && lpitem
->xTab
)
436 maxTab
= MAX( maxTab
, lpitem
->xTab
);
437 maxTabWidth
= MAX(maxTabWidth
,lpitem
->rect
.right
-lpitem
->xTab
);
441 /* Finish the column (set all items to the largest width found) */
442 maxX
= MAX( maxX
, maxTab
+ maxTabWidth
);
443 for (lpitem
= &lppop
->items
[start
]; start
< i
; start
++, lpitem
++)
445 lpitem
->rect
.right
= maxX
;
446 if (IS_STRING_ITEM(lpitem
->item_flags
) && lpitem
->xTab
)
447 lpitem
->xTab
= maxTab
;
449 lppop
->Height
= MAX( lppop
->Height
, orgY
);
457 /***********************************************************************
458 * MENU_MenuBarCalcSize
460 * Calculate the size of the menu bar.
462 static void MENU_MenuBarCalcSize( HDC hdc
, LPRECT16 lprect
, LPPOPUPMENU lppop
,
466 int start
, i
, orgX
, orgY
, maxY
, helpPos
;
468 if ((lprect
== NULL
) || (lppop
== NULL
)) return;
469 if (lppop
->nItems
== 0) return;
470 dprintf_menu(stddeb
,"MENU_MenuBarCalcSize left=%d top=%d right=%d bottom=%d\n",
471 lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
472 lppop
->Width
= lprect
->right
- lprect
->left
;
477 while (start
< lppop
->nItems
)
479 lpitem
= &lppop
->items
[start
];
483 /* Parse items until line break or end of menu */
484 for (i
= start
; i
< lppop
->nItems
; i
++, lpitem
++)
486 if ((helpPos
== -1) && (lpitem
->item_flags
& MF_HELP
)) helpPos
= i
;
488 (lpitem
->item_flags
& (MF_MENUBREAK
| MF_MENUBARBREAK
))) break;
489 MENU_CalcItemSize( hdc
, lpitem
, hwndOwner
, orgX
, orgY
, TRUE
);
490 if (lpitem
->rect
.right
> lprect
->right
)
492 if (i
!= start
) break;
493 else lpitem
->rect
.right
= lprect
->right
;
495 maxY
= MAX( maxY
, lpitem
->rect
.bottom
);
496 orgX
= lpitem
->rect
.right
;
499 /* Finish the line (set all items to the largest height found) */
500 while (start
< i
) lppop
->items
[start
++].rect
.bottom
= maxY
;
503 lprect
->bottom
= maxY
;
504 lppop
->Height
= lprect
->bottom
- lprect
->top
;
506 /* Flush right all items between the MF_HELP and the last item */
507 /* (if several lines, only move the last line) */
510 lpitem
= &lppop
->items
[lppop
->nItems
-1];
511 orgY
= lpitem
->rect
.top
;
512 orgX
= lprect
->right
;
513 for (i
= lppop
->nItems
- 1; i
>= helpPos
; i
--, lpitem
--)
515 if (lpitem
->rect
.top
!= orgY
) break; /* Other line */
516 if (lpitem
->rect
.right
>= orgX
) break; /* Too far right already */
517 lpitem
->rect
.left
+= orgX
- lpitem
->rect
.right
;
518 lpitem
->rect
.right
= orgX
;
519 orgX
= lpitem
->rect
.left
;
525 /***********************************************************************
528 * Draw a single menu item.
530 static void MENU_DrawMenuItem( HWND hwnd
, HDC hdc
, MENUITEM
*lpitem
,
531 UINT height
, BOOL menuBar
)
535 if (lpitem
->item_flags
& MF_OWNERDRAW
)
537 DRAWITEMSTRUCT32 dis
;
539 dprintf_menu( stddeb
, "DrawMenuItem: Ownerdraw!\n" );
540 dis
.CtlType
= ODT_MENU
;
541 dis
.itemID
= lpitem
->item_id
;
542 dis
.itemData
= (DWORD
)lpitem
->text
;
544 if (lpitem
->item_flags
& MF_CHECKED
) dis
.itemState
|= ODS_CHECKED
;
545 if (lpitem
->item_flags
& MF_GRAYED
) dis
.itemState
|= ODS_GRAYED
;
546 if (lpitem
->item_flags
& MF_HILITE
) dis
.itemState
|= ODS_SELECTED
;
547 dis
.itemAction
= ODA_DRAWENTIRE
| ODA_SELECT
| ODA_FOCUS
;
550 CONV_RECT16TO32( &lpitem
->rect
, &dis
.rcItem
);
551 SendMessage32A( hwnd
, WM_DRAWITEM
, 0, (LPARAM
)&dis
);
555 if (menuBar
&& (lpitem
->item_flags
& MF_SEPARATOR
)) return;
558 /* Draw the background */
560 if (lpitem
->item_flags
& MF_HILITE
)
561 FillRect16( hdc
, &rect
, sysColorObjects
.hbrushHighlight
);
562 else FillRect16( hdc
, &rect
, sysColorObjects
.hbrushMenu
);
563 SetBkMode( hdc
, TRANSPARENT
);
565 /* Draw the separator bar (if any) */
567 if (!menuBar
&& (lpitem
->item_flags
& MF_MENUBARBREAK
))
569 SelectObject( hdc
, sysColorObjects
.hpenWindowFrame
);
570 MoveTo( hdc
, rect
.left
, 0 );
571 LineTo( hdc
, rect
.left
, height
);
573 if (lpitem
->item_flags
& MF_SEPARATOR
)
575 SelectObject( hdc
, sysColorObjects
.hpenWindowFrame
);
576 MoveTo( hdc
, rect
.left
, rect
.top
+ SEPARATOR_HEIGHT
/2 );
577 LineTo( hdc
, rect
.right
, rect
.top
+ SEPARATOR_HEIGHT
/2 );
583 if (lpitem
->item_flags
& MF_HILITE
)
585 if (lpitem
->item_flags
& MF_GRAYED
)
586 SetTextColor( hdc
, GetSysColor( COLOR_GRAYTEXT
) );
588 SetTextColor( hdc
, GetSysColor( COLOR_HIGHLIGHTTEXT
) );
589 SetBkColor( hdc
, GetSysColor( COLOR_HIGHLIGHT
) );
593 if (lpitem
->item_flags
& MF_GRAYED
)
594 SetTextColor( hdc
, GetSysColor( COLOR_GRAYTEXT
) );
596 SetTextColor( hdc
, GetSysColor( COLOR_MENUTEXT
) );
597 SetBkColor( hdc
, GetSysColor( COLOR_MENU
) );
602 /* Draw the check mark */
604 if (lpitem
->item_flags
& MF_CHECKED
)
606 GRAPH_DrawBitmap(hdc
, lpitem
->hCheckBit
? lpitem
->hCheckBit
:
607 hStdCheck
, rect
.left
,
608 (rect
.top
+rect
.bottom
-check_bitmap_height
) / 2,
609 0, 0, check_bitmap_width
, check_bitmap_height
);
611 else if (lpitem
->hUnCheckBit
!= 0) /* Not checked */
613 GRAPH_DrawBitmap(hdc
, lpitem
->hUnCheckBit
, rect
.left
,
614 (rect
.top
+rect
.bottom
-check_bitmap_height
) / 2,
615 0, 0, check_bitmap_width
, check_bitmap_height
);
618 /* Draw the popup-menu arrow */
620 if (lpitem
->item_flags
& MF_POPUP
)
622 GRAPH_DrawBitmap( hdc
, hStdMnArrow
,
623 rect
.right
-arrow_bitmap_width
-1,
624 (rect
.top
+rect
.bottom
-arrow_bitmap_height
) / 2,
625 0, 0, arrow_bitmap_width
, arrow_bitmap_height
);
628 rect
.left
+= check_bitmap_width
;
629 rect
.right
-= arrow_bitmap_width
;
632 /* Draw the item text or bitmap */
634 if (lpitem
->item_flags
& MF_BITMAP
)
636 GRAPH_DrawBitmap( hdc
, (HBITMAP16
)(UINT32
)lpitem
->text
,
637 rect
.left
, rect
.top
, 0, 0,
638 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
641 /* No bitmap - process text if present */
642 else if (IS_STRING_ITEM(lpitem
->item_flags
))
648 rect
.left
+= MENU_BAR_ITEMS_SPACE
/ 2;
649 rect
.right
-= MENU_BAR_ITEMS_SPACE
/ 2;
650 i
= strlen( lpitem
->text
);
654 for (i
= 0; lpitem
->text
[i
]; i
++)
655 if ((lpitem
->text
[i
] == '\t') || (lpitem
->text
[i
] == '\b'))
659 DrawText16( hdc
, lpitem
->text
, i
, &rect
,
660 DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
662 if (lpitem
->text
[i
]) /* There's a tab or flush-right char */
664 if (lpitem
->text
[i
] == '\t')
666 rect
.left
= lpitem
->xTab
;
667 DrawText16( hdc
, lpitem
->text
+ i
+ 1, -1, &rect
,
668 DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
670 else DrawText16( hdc
, lpitem
->text
+ i
+ 1, -1, &rect
,
671 DT_RIGHT
| DT_VCENTER
| DT_SINGLELINE
);
677 /***********************************************************************
680 * Paint a popup menu.
682 static void MENU_DrawPopupMenu( HWND hwnd
, HDC hdc
, HMENU hmenu
)
689 GetClientRect16( hwnd
, &rect
);
690 FillRect16( hdc
, &rect
, sysColorObjects
.hbrushMenu
);
691 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
692 if (!menu
|| !menu
->nItems
) return;
693 for (i
= menu
->nItems
, item
= menu
->items
; i
> 0; i
--, item
++)
694 MENU_DrawMenuItem( hwnd
, hdc
, item
, menu
->Height
, FALSE
);
698 /***********************************************************************
701 * Paint a menu bar. Returns the height of the menu bar.
703 UINT
MENU_DrawMenuBar(HDC hDC
, LPRECT16 lprect
, HWND hwnd
, BOOL suppress_draw
)
707 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
709 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR( (HMENU
)wndPtr
->wIDmenu
);
710 if (lppop
== NULL
|| lprect
== NULL
) return SYSMETRICS_CYMENU
;
711 dprintf_menu(stddeb
,"MENU_DrawMenuBar(%04x, %p, %p); !\n",
713 if (lppop
->Height
== 0) MENU_MenuBarCalcSize(hDC
, lprect
, lppop
, hwnd
);
714 lprect
->bottom
= lprect
->top
+ lppop
->Height
;
715 if (suppress_draw
) return lppop
->Height
;
717 FillRect16(hDC
, lprect
, sysColorObjects
.hbrushMenu
);
718 SelectObject( hDC
, sysColorObjects
.hpenWindowFrame
);
719 MoveTo( hDC
, lprect
->left
, lprect
->bottom
);
720 LineTo( hDC
, lprect
->right
, lprect
->bottom
);
722 if (lppop
->nItems
== 0) return SYSMETRICS_CYMENU
;
723 for (i
= 0; i
< lppop
->nItems
; i
++)
725 MENU_DrawMenuItem( hwnd
, hDC
, &lppop
->items
[i
], lppop
->Height
, TRUE
);
727 return lppop
->Height
;
731 /***********************************************************************
734 static BOOL
MENU_SwitchTPWndTo( HTASK hTask
)
736 /* This is supposed to be called when popup is hidden */
738 TDB
* task
= (TDB
*)GlobalLock16(hTask
);
740 if( !task
) return 0;
742 /* if this task got as far as menu tracking it must have a queue */
744 pTopPWnd
->hInstance
= task
->hInstance
;
745 pTopPWnd
->hmemTaskQ
= task
->hQueue
;
749 /***********************************************************************
752 * Display a popup menu.
754 static BOOL
MENU_ShowPopup(HWND hwndOwner
, HMENU hmenu
, UINT id
, int x
, int y
)
760 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
761 if (menu
->FocusedItem
!= NO_SELECTED_ITEM
)
763 menu
->items
[menu
->FocusedItem
].item_flags
&= ~(MF_HILITE
|MF_MOUSESELECT
);
764 menu
->FocusedItem
= NO_SELECTED_ITEM
;
766 SendMessage16( hwndOwner
, WM_INITMENUPOPUP
, (WPARAM
)hmenu
,
767 MAKELONG( id
, (menu
->wFlags
& MF_SYSMENU
) ? 1 : 0 ));
768 MENU_PopupMenuCalcSize( menu
, hwndOwner
);
770 wndPtr
= WIN_FindWndPtr( hwndOwner
);
771 if (!wndPtr
) return FALSE
;
775 pTopPWnd
= WIN_FindWndPtr(CreateWindow16( POPUPMENU_CLASS_ATOM
, NULL
,
776 WS_POPUP
| WS_BORDER
, x
, y
,
777 menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
778 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
779 0, 0, wndPtr
->hInstance
,
780 (LPVOID
)(HMENU32
)hmenu
));
781 if (!pTopPWnd
) return FALSE
;
787 /* create new window for the submenu */
788 HWND hWnd
= CreateWindow16( POPUPMENU_CLASS_ATOM
, NULL
,
789 WS_POPUP
| WS_BORDER
, x
, y
,
790 menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
791 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
792 menu
->hWnd
, 0, wndPtr
->hInstance
,
793 (LPVOID
)(HMENU32
)hmenu
);
794 if( !hWnd
) return FALSE
;
801 MENU_SwitchTPWndTo(GetCurrentTask());
802 SendMessage16( pTopPWnd
->hwndSelf
, WM_USER
, (WPARAM
)hmenu
, 0L);
804 menu
->hWnd
= pTopPWnd
->hwndSelf
;
809 wndPtr
= WIN_FindWndPtr( menu
->hWnd
);
811 SetWindowPos(menu
->hWnd
, 0, x
, y
, menu
->Width
+ 2*SYSMETRICS_CXBORDER
,
812 menu
->Height
+ 2*SYSMETRICS_CYBORDER
,
813 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_NOREDRAW
);
814 /* Display the window */
816 SetWindowPos( menu
->hWnd
, HWND_TOP
, 0, 0, 0, 0,
817 SWP_SHOWWINDOW
| SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
818 UpdateWindow( menu
->hWnd
);
823 /***********************************************************************
826 static void MENU_SelectItem( HWND hwndOwner
, HMENU hmenu
, UINT wIndex
,
827 BOOL sendMenuSelect
)
832 lppop
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
833 if (!lppop
->nItems
) return;
834 if ((wIndex
!= NO_SELECTED_ITEM
) &&
835 (wIndex
!= SYSMENU_SELECTED
) &&
836 (lppop
->items
[wIndex
].item_flags
& MF_SEPARATOR
))
837 wIndex
= NO_SELECTED_ITEM
;
838 if (lppop
->FocusedItem
== wIndex
) return;
839 if (lppop
->wFlags
& MF_POPUP
) hdc
= GetDC( lppop
->hWnd
);
840 else hdc
= GetDCEx( lppop
->hWnd
, 0, DCX_CACHE
| DCX_WINDOW
);
842 /* Clear previous highlighted item */
843 if (lppop
->FocusedItem
!= NO_SELECTED_ITEM
)
845 if (lppop
->FocusedItem
== SYSMENU_SELECTED
)
846 NC_DrawSysButton( lppop
->hWnd
, hdc
, FALSE
);
849 lppop
->items
[lppop
->FocusedItem
].item_flags
&=~(MF_HILITE
|MF_MOUSESELECT
);
850 MENU_DrawMenuItem(lppop
->hWnd
,hdc
,&lppop
->items
[lppop
->FocusedItem
],
851 lppop
->Height
, !(lppop
->wFlags
& MF_POPUP
) );
855 /* Highlight new item (if any) */
856 lppop
->FocusedItem
= wIndex
;
857 if (lppop
->FocusedItem
!= NO_SELECTED_ITEM
)
859 if (lppop
->FocusedItem
== SYSMENU_SELECTED
)
861 NC_DrawSysButton( lppop
->hWnd
, hdc
, TRUE
);
863 SendMessage16( hwndOwner
, WM_MENUSELECT
,
864 WIN_FindWndPtr(lppop
->hWnd
)->hSysMenu
,
865 MAKELONG(lppop
->wFlags
| MF_MOUSESELECT
, hmenu
));
869 lppop
->items
[lppop
->FocusedItem
].item_flags
|= MF_HILITE
;
870 MENU_DrawMenuItem( lppop
->hWnd
, hdc
, &lppop
->items
[lppop
->FocusedItem
],
871 lppop
->Height
, !(lppop
->wFlags
& MF_POPUP
) );
873 SendMessage16( hwndOwner
, WM_MENUSELECT
,
874 lppop
->items
[lppop
->FocusedItem
].item_id
,
875 MAKELONG( lppop
->items
[lppop
->FocusedItem
].item_flags
| MF_MOUSESELECT
, hmenu
));
878 else if (sendMenuSelect
)
879 SendMessage16( hwndOwner
, WM_MENUSELECT
, hmenu
,
880 MAKELONG( lppop
->wFlags
| MF_MOUSESELECT
, hmenu
) );
882 ReleaseDC( lppop
->hWnd
, hdc
);
886 /***********************************************************************
890 static void MENU_SelectItemRel( HWND hwndOwner
, HMENU hmenu
, int offset
)
895 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
896 if (!menu
->items
) return;
897 if ((menu
->FocusedItem
!= NO_SELECTED_ITEM
) &&
898 (menu
->FocusedItem
!= SYSMENU_SELECTED
))
900 for (i
= menu
->FocusedItem
+ offset
; i
>= 0 && i
< menu
->nItems
903 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
905 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
910 if (MENU_HasSysMenu( menu
))
912 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
917 if( offset
> 0 ) { i
= 0; min
= -1; }
918 else i
= menu
->nItems
- 1;
920 for ( ; i
> min
&& i
< menu
->nItems
; i
+= offset
)
922 if (!(menu
->items
[i
].item_flags
& MF_SEPARATOR
))
924 MENU_SelectItem( hwndOwner
, hmenu
, i
, TRUE
);
928 if (MENU_HasSysMenu( menu
))
929 MENU_SelectItem( hwndOwner
, hmenu
, SYSMENU_SELECTED
, TRUE
);
933 /**********************************************************************
936 * Set an item flags, id and text ptr.
938 static BOOL
MENU_SetItemData( MENUITEM
*item
, UINT flags
, UINT id
, LPCSTR str
)
940 LPSTR prevText
= IS_STRING_ITEM(item
->item_flags
) ? item
->text
: NULL
;
942 if (IS_STRING_ITEM(flags
))
946 flags
|= MF_SEPARATOR
;
952 /* Item beginning with a backspace is a help item */
958 if (!(text
= HEAP_strdupA( SystemHeap
, 0, str
))) return FALSE
;
962 else if ((flags
& MF_BITMAP
) || (flags
& MF_OWNERDRAW
))
963 item
->text
= (LPSTR
)str
;
964 else item
->text
= NULL
;
966 item
->item_flags
= flags
& ~(MF_HILITE
| MF_MOUSESELECT
);
968 SetRectEmpty16( &item
->rect
);
969 if (prevText
) HeapFree( SystemHeap
, 0, prevText
);
974 /**********************************************************************
977 * Insert a new item into a menu.
979 static MENUITEM
*MENU_InsertItem( HMENU hMenu
, UINT pos
, UINT flags
)
984 if (!(menu
= (POPUPMENU
*)USER_HEAP_LIN_ADDR(hMenu
)))
986 dprintf_menu( stddeb
, "MENU_InsertItem: %04x not a menu handle\n",
991 /* Find where to insert new item */
993 if ((flags
& MF_BYPOSITION
) &&
994 ((pos
== (UINT
)-1) || (pos
== menu
->nItems
)))
996 /* Special case: append to menu */
997 /* Some programs specify the menu length to do that */
1002 if (!MENU_FindItem( &hMenu
, &pos
, flags
))
1004 dprintf_menu( stddeb
, "MENU_InsertItem: item %x not found\n",
1008 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
)))
1010 dprintf_menu(stddeb
,"MENU_InsertItem: %04x not a menu handle\n",
1016 /* Create new items array */
1018 newItems
= HeapAlloc( SystemHeap
, 0, sizeof(MENUITEM
) * (menu
->nItems
+1) );
1021 dprintf_menu( stddeb
, "MENU_InsertItem: allocation failed\n" );
1024 if (menu
->nItems
> 0)
1026 /* Copy the old array into the new */
1027 if (pos
> 0) memcpy( newItems
, menu
->items
, pos
* sizeof(MENUITEM
) );
1028 if (pos
< menu
->nItems
) memcpy( &newItems
[pos
+1], &menu
->items
[pos
],
1029 (menu
->nItems
-pos
)*sizeof(MENUITEM
) );
1030 HeapFree( SystemHeap
, 0, menu
->items
);
1032 menu
->items
= newItems
;
1034 memset( &newItems
[pos
], 0, sizeof(*newItems
) );
1035 return &newItems
[pos
];
1039 /**********************************************************************
1040 * MENU_ParseResource
1042 * Parse a standard menu resource and add items to the menu.
1043 * Return a pointer to the end of the resource.
1045 static LPCSTR
MENU_ParseResource( LPCSTR res
, HMENU hMenu
, BOOL unicode
)
1052 flags
= GET_WORD(res
);
1053 res
+= sizeof(WORD
);
1054 if (!(flags
& MF_POPUP
))
1057 res
+= sizeof(WORD
);
1059 if (!IS_STRING_ITEM(flags
))
1060 fprintf( stderr
, "MENU_ParseResource: not a string item %04x\n",
1063 if (!unicode
) res
+= strlen(str
) + 1;
1064 else res
+= (lstrlen32W((LPCWSTR
)str
) + 1) * sizeof(WCHAR
);
1065 if (flags
& MF_POPUP
)
1067 HMENU hSubMenu
= CreatePopupMenu();
1068 if (!hSubMenu
) return NULL
;
1069 if (!(res
= MENU_ParseResource( res
, hSubMenu
, unicode
)))
1071 if (!unicode
) AppendMenu32A( hMenu
, flags
, (UINT
)hSubMenu
, str
);
1072 else AppendMenu32W( hMenu
, flags
, (UINT
)hSubMenu
, (LPCWSTR
)str
);
1074 else /* Not a popup */
1076 if (!unicode
) AppendMenu32A( hMenu
, flags
, id
, *str
? str
: NULL
);
1077 else AppendMenu32W( hMenu
, flags
, id
,
1078 *(LPCWSTR
)str
? (LPCWSTR
)str
: NULL
);
1080 } while (!(flags
& MF_END
));
1085 /***********************************************************************
1088 * Return the handle of the selected sub-popup menu (if any).
1090 static HMENU
MENU_GetSubPopup( HMENU hmenu
)
1095 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1096 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return 0;
1097 else if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1098 return WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1100 item
= &menu
->items
[menu
->FocusedItem
];
1101 if (!(item
->item_flags
& MF_POPUP
) || !(item
->item_flags
& MF_MOUSESELECT
))
1103 return (HMENU
)item
->item_id
;
1107 /***********************************************************************
1108 * MENU_HideSubPopups
1110 * Hide the sub-popup menus of this menu.
1112 static void MENU_HideSubPopups( HWND hwndOwner
, HMENU hmenu
,
1113 BOOL sendMenuSelect
)
1116 POPUPMENU
*menu
, *submenu
;
1119 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return;
1120 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return;
1121 if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1123 hsubmenu
= WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1127 item
= &menu
->items
[menu
->FocusedItem
];
1128 if (!(item
->item_flags
& MF_POPUP
) ||
1129 !(item
->item_flags
& MF_MOUSESELECT
)) return;
1130 item
->item_flags
&= ~MF_MOUSESELECT
;
1131 hsubmenu
= (HMENU
)item
->item_id
;
1133 submenu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hsubmenu
);
1134 MENU_HideSubPopups( hwndOwner
, hsubmenu
, FALSE
);
1135 MENU_SelectItem( hwndOwner
, hsubmenu
, NO_SELECTED_ITEM
, sendMenuSelect
);
1136 if (submenu
->hWnd
== pTopPWnd
->hwndSelf
)
1138 ShowWindow( submenu
->hWnd
, SW_HIDE
);
1143 DestroyWindow( submenu
->hWnd
);
1149 /***********************************************************************
1152 * Display the sub-menu of the selected item of this menu.
1153 * Return the handle of the submenu, or hmenu if no submenu to display.
1155 static HMENU
MENU_ShowSubPopup( HWND hwndOwner
, HMENU hmenu
, BOOL selectFirst
)
1161 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return hmenu
;
1162 if (!(wndPtr
= WIN_FindWndPtr( menu
->hWnd
))) return hmenu
;
1163 if (menu
->FocusedItem
== NO_SELECTED_ITEM
) return hmenu
;
1164 if (menu
->FocusedItem
== SYSMENU_SELECTED
)
1166 MENU_ShowPopup(hwndOwner
, wndPtr
->hSysMenu
, 0, wndPtr
->rectClient
.left
,
1167 wndPtr
->rectClient
.top
- menu
->Height
- 2*SYSMETRICS_CYBORDER
);
1168 if (selectFirst
) MENU_SelectItemRel( hwndOwner
, wndPtr
->hSysMenu
, ITEM_NEXT
);
1169 return wndPtr
->hSysMenu
;
1171 item
= &menu
->items
[menu
->FocusedItem
];
1172 if (!(item
->item_flags
& MF_POPUP
) ||
1173 (item
->item_flags
& (MF_GRAYED
| MF_DISABLED
))) return hmenu
;
1174 item
->item_flags
|= MF_MOUSESELECT
;
1175 if (menu
->wFlags
& MF_POPUP
)
1177 MENU_ShowPopup( hwndOwner
, (HMENU
)item
->item_id
, menu
->FocusedItem
,
1178 wndPtr
->rectWindow
.left
+ item
->rect
.right
-arrow_bitmap_width
,
1179 wndPtr
->rectWindow
.top
+ item
->rect
.top
);
1183 MENU_ShowPopup( hwndOwner
, (HMENU
)item
->item_id
, menu
->FocusedItem
,
1184 wndPtr
->rectWindow
.left
+ item
->rect
.left
,
1185 wndPtr
->rectWindow
.top
+ item
->rect
.bottom
);
1187 if (selectFirst
) MENU_SelectItemRel( hwndOwner
, (HMENU
)item
->item_id
, ITEM_NEXT
);
1188 return (HMENU
)item
->item_id
;
1192 /***********************************************************************
1193 * MENU_FindMenuByCoords
1195 * Find the menu containing a given point (in screen coords).
1197 static HMENU
MENU_FindMenuByCoords( HMENU hmenu
, POINT16 pt
)
1202 if (!(hwnd
= WindowFromPoint16( pt
))) return 0;
1205 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1206 if (menu
->hWnd
== hwnd
)
1208 if (!(menu
->wFlags
& MF_POPUP
))
1210 /* Make sure it's in the menu bar (or in system menu) */
1211 WND
*wndPtr
= WIN_FindWndPtr( menu
->hWnd
);
1212 if ((pt
.x
< wndPtr
->rectClient
.left
) ||
1213 (pt
.x
>= wndPtr
->rectClient
.right
) ||
1214 (pt
.y
>= wndPtr
->rectClient
.top
)) return 0;
1215 if (pt
.y
< wndPtr
->rectClient
.top
- menu
->Height
)
1217 if (!MENU_IsInSysMenu( menu
, pt
)) return 0;
1219 /* else it's in the menu bar */
1223 hmenu
= MENU_GetSubPopup( hmenu
);
1229 /***********************************************************************
1230 * MENU_ExecFocusedItem
1232 * Execute a menu item (for instance when user pressed Enter).
1233 * Return TRUE if we can go on with menu tracking.
1235 static BOOL
MENU_ExecFocusedItem( HWND hwndOwner
, HMENU hmenu
,
1236 HMENU
*hmenuCurrent
)
1239 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1240 if (!menu
|| !menu
->nItems
|| (menu
->FocusedItem
== NO_SELECTED_ITEM
) ||
1241 (menu
->FocusedItem
== SYSMENU_SELECTED
)) return TRUE
;
1242 item
= &menu
->items
[menu
->FocusedItem
];
1243 if (!(item
->item_flags
& MF_POPUP
))
1245 if (!(item
->item_flags
& (MF_GRAYED
| MF_DISABLED
)))
1247 PostMessage( hwndOwner
, (menu
->wFlags
& MF_SYSMENU
) ?
1248 WM_SYSCOMMAND
: WM_COMMAND
, item
->item_id
, 0 );
1255 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, TRUE
);
1261 /***********************************************************************
1264 * Handle a button-down event in a menu. Point is in screen coords.
1265 * hmenuCurrent is the top-most visible popup.
1266 * Return TRUE if we can go on with menu tracking.
1268 static BOOL
MENU_ButtonDown( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1275 if (!hmenu
) return FALSE
; /* Outside all menus */
1276 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1277 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1278 if (!item
) /* Maybe in system menu */
1280 if (!MENU_IsInSysMenu( menu
, pt
)) return FALSE
;
1281 id
= SYSMENU_SELECTED
;
1284 if (menu
->FocusedItem
== id
)
1286 if (id
== SYSMENU_SELECTED
) return FALSE
;
1287 if (item
->item_flags
& MF_POPUP
)
1289 if (item
->item_flags
& MF_MOUSESELECT
)
1291 if (menu
->wFlags
& MF_POPUP
)
1293 MENU_HideSubPopups( hwndOwner
, hmenu
, TRUE
);
1294 *hmenuCurrent
= hmenu
;
1298 else *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1303 MENU_HideSubPopups( hwndOwner
, hmenu
, FALSE
);
1304 MENU_SelectItem( hwndOwner
, hmenu
, id
, TRUE
);
1305 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1311 /***********************************************************************
1314 * Handle a button-up event in a menu. Point is in screen coords.
1315 * hmenuCurrent is the top-most visible popup.
1316 * Return TRUE if we can go on with menu tracking.
1318 static BOOL
MENU_ButtonUp( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1326 if (!hmenu
) return FALSE
; /* Outside all menus */
1327 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1328 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1329 if (!item
) /* Maybe in system menu */
1331 if (!MENU_IsInSysMenu( menu
, pt
)) return FALSE
;
1332 id
= SYSMENU_SELECTED
;
1333 hsubmenu
= WIN_FindWndPtr(menu
->hWnd
)->hSysMenu
;
1336 if (menu
->FocusedItem
!= id
) return FALSE
;
1338 if (id
!= SYSMENU_SELECTED
)
1340 if (!(item
->item_flags
& MF_POPUP
))
1342 return MENU_ExecFocusedItem( hwndOwner
, hmenu
, hmenuCurrent
);
1344 hsubmenu
= (HMENU
)item
->item_id
;
1346 /* Select first item of sub-popup */
1347 MENU_SelectItem( hwndOwner
, hsubmenu
, NO_SELECTED_ITEM
, FALSE
);
1348 MENU_SelectItemRel( hwndOwner
, hsubmenu
, ITEM_NEXT
);
1353 /***********************************************************************
1356 * Handle a motion event in a menu. Point is in screen coords.
1357 * hmenuCurrent is the top-most visible popup.
1358 * Return TRUE if we can go on with menu tracking.
1360 static BOOL
MENU_MouseMove( HWND hwndOwner
, HMENU hmenu
, HMENU
*hmenuCurrent
,
1364 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1365 UINT id
= NO_SELECTED_ITEM
;
1369 item
= MENU_FindItemByCoords( menu
, pt
.x
, pt
.y
, &id
);
1370 if (!item
) /* Maybe in system menu */
1372 if (!MENU_IsInSysMenu( menu
, pt
))
1373 id
= NO_SELECTED_ITEM
; /* Outside all items */
1374 else id
= SYSMENU_SELECTED
;
1377 if (id
== NO_SELECTED_ITEM
)
1379 MENU_SelectItem( hwndOwner
, *hmenuCurrent
, NO_SELECTED_ITEM
, TRUE
);
1381 else if (menu
->FocusedItem
!= id
)
1383 MENU_HideSubPopups( hwndOwner
, hmenu
, FALSE
);
1384 MENU_SelectItem( hwndOwner
, hmenu
, id
, TRUE
);
1385 *hmenuCurrent
= MENU_ShowSubPopup( hwndOwner
, hmenu
, FALSE
);
1390 /***********************************************************************
1393 static LRESULT
MENU_DoNextMenu( HWND
* hwndOwner
, HMENU
* hmenu
, HMENU
*hmenuCurrent
, UINT vk
)
1395 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( *hmenu
);
1398 if( (vk
== VK_LEFT
&& !menu
->FocusedItem
)
1399 || (vk
== VK_RIGHT
&& menu
->FocusedItem
== menu
->nItems
- 1)
1400 || menu
->FocusedItem
== SYSMENU_SELECTED
1401 || ((menu
->wFlags
& (MF_POPUP
| MF_SYSMENU
)) == (MF_POPUP
| MF_SYSMENU
)) )
1403 LRESULT l
= SendMessage16( *hwndOwner
, WM_NEXTMENU
, (WPARAM
)vk
,
1404 (LPARAM
)((menu
->FocusedItem
== SYSMENU_SELECTED
)
1405 ? GetSystemMenu( *hwndOwner
, 0)
1408 if( l
== 0 || !IsMenu(LOWORD(l
)) || !IsWindow(HIWORD(l
)) ) return 0;
1410 /* shutdown current menu -
1411 * all these checks for system popup window are needed
1412 * only because Wine system menu tracking is unsuitable
1413 * for a lot of things (esp. when we do not have wIDmenu to fall back on).
1416 MENU_SelectItem( *hwndOwner
, *hmenu
, NO_SELECTED_ITEM
, FALSE
);
1418 if( (menu
->wFlags
& (MF_POPUP
| MF_SYSMENU
)) == (MF_POPUP
| MF_SYSMENU
) )
1422 ShowWindow( menu
->hWnd
, SW_HIDE
);
1425 if( !IsIconic( *hwndOwner
) )
1427 hdc
= GetDCEx( *hwndOwner
, 0, DCX_CACHE
| DCX_WINDOW
);
1428 NC_DrawSysButton( *hwndOwner
, hdc
, FALSE
);
1429 ReleaseDC( *hwndOwner
, hdc
);
1434 *hwndOwner
= HIWORD(l
);
1436 SetCapture( *hwndOwner
);
1438 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( *hmenu
);
1440 /* init next menu */
1442 if( (menu
->wFlags
& (MF_POPUP
| MF_SYSMENU
)) == (MF_POPUP
| MF_SYSMENU
) )
1445 WND
* wndPtr
= WIN_FindWndPtr( *hwndOwner
);
1447 /* stupid kludge, see above */
1449 if( wndPtr
->wIDmenu
&& !(wndPtr
->dwStyle
& WS_CHILD
) )
1450 { *hmenu
= wndPtr
->wIDmenu
; id
= SYSMENU_SELECTED
; }
1453 if( NC_GetSysPopupPos( wndPtr
, &rect
) )
1454 MENU_ShowPopup( *hwndOwner
, *hmenu
, 0, rect
.left
, rect
.bottom
);
1456 if( !IsIconic( *hwndOwner
) )
1458 HDC hdc
= GetDCEx( *hwndOwner
, 0, DCX_CACHE
| DCX_WINDOW
);
1459 NC_DrawSysButton( *hwndOwner
, hdc
, TRUE
);
1460 ReleaseDC( *hwndOwner
, hdc
);
1465 MENU_SelectItem( *hwndOwner
, *hmenu
, id
, TRUE
);
1471 /***********************************************************************
1474 * Handle a VK_LEFT key event in a menu.
1475 * hmenuCurrent is the top-most visible popup.
1477 static void MENU_KeyLeft( HWND
* hwndOwner
, HMENU
* hmenu
, HMENU
*hmenuCurrent
)
1480 HMENU hmenutmp
, hmenuprev
;
1482 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( *hmenu
);
1483 hmenuprev
= hmenutmp
= *hmenu
;
1484 while (hmenutmp
!= *hmenuCurrent
)
1486 hmenutmp
= MENU_GetSubPopup( hmenuprev
);
1487 if (hmenutmp
!= *hmenuCurrent
) hmenuprev
= hmenutmp
;
1489 MENU_HideSubPopups( *hwndOwner
, hmenuprev
, TRUE
);
1492 if ( (hmenuprev
== *hmenu
) &&
1493 ((menu
->wFlags
& MF_SYSMENU
) || !(menu
->wFlags
& MF_POPUP
)) )
1495 /* send WM_NEXTMENU */
1497 if( !MENU_DoNextMenu( hwndOwner
, hmenu
, hmenuCurrent
, VK_LEFT
) )
1498 MENU_SelectItemRel( *hwndOwner
, *hmenu
, ITEM_PREV
);
1499 else *hmenuCurrent
= *hmenu
;
1501 if (*hmenuCurrent
!= hmenutmp
)
1503 /* A sublevel menu was displayed -> display the next one */
1504 *hmenuCurrent
= MENU_ShowSubPopup( *hwndOwner
, *hmenu
, TRUE
);
1507 else *hmenuCurrent
= hmenuprev
;
1511 /***********************************************************************
1514 * Handle a VK_RIGHT key event in a menu.
1515 * hmenuCurrent is the top-most visible popup.
1517 static void MENU_KeyRight( HWND
* hwndOwner
, HMENU
* hmenu
, HMENU
*hmenuCurrent
)
1522 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( *hmenu
);
1524 if ((menu
->wFlags
& MF_POPUP
) || (*hmenuCurrent
!= *hmenu
))
1526 /* If already displaying a popup, try to display sub-popup */
1527 hmenutmp
= MENU_ShowSubPopup( *hwndOwner
, *hmenuCurrent
, TRUE
);
1528 if (hmenutmp
!= *hmenuCurrent
) /* Sub-popup displayed */
1530 *hmenuCurrent
= hmenutmp
;
1535 /* If menu-bar tracking, go to next item */
1537 if (!(menu
->wFlags
& MF_POPUP
) || (menu
->wFlags
& MF_SYSMENU
))
1539 MENU_HideSubPopups( *hwndOwner
, *hmenu
, FALSE
);
1542 /* Send WM_NEXTMENU */
1544 if( !MENU_DoNextMenu( hwndOwner
, hmenu
, hmenuCurrent
, VK_RIGHT
) )
1545 MENU_SelectItemRel( *hwndOwner
, *hmenu
, ITEM_NEXT
);
1546 else *hmenuCurrent
= *hmenu
;
1548 if (*hmenuCurrent
!= hmenutmp
)
1550 /* A sublevel menu was displayed -> display the next one */
1551 *hmenuCurrent
= MENU_ShowSubPopup( *hwndOwner
, *hmenu
, TRUE
);
1554 else if (*hmenuCurrent
!= *hmenu
) /* Hide last level popup */
1557 hmenuprev
= hmenutmp
= *hmenu
;
1558 while (hmenutmp
!= *hmenuCurrent
)
1560 hmenutmp
= MENU_GetSubPopup( hmenuprev
);
1561 if (hmenutmp
!= *hmenuCurrent
) hmenuprev
= hmenutmp
;
1563 MENU_HideSubPopups( *hwndOwner
, hmenuprev
, TRUE
);
1564 *hmenuCurrent
= hmenuprev
;
1569 /***********************************************************************
1572 * Menu tracking code.
1573 * If 'x' and 'y' are not 0, we simulate a button-down event at (x,y)
1574 * before beginning tracking. This is to help menu-bar tracking.
1576 static BOOL
MENU_TrackMenu( HMENU hmenu
, UINT wFlags
, int x
, int y
,
1577 HWND hwnd
, const RECT16
*lprect
)
1581 HMENU hmenuCurrent
= hmenu
;
1582 BOOL fClosed
= FALSE
, fRemove
;
1585 fEndMenuCalled
= FALSE
;
1586 if (!(menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
1589 POINT16 pt
= { x
, y
};
1590 MENU_ButtonDown( hwnd
, hmenu
, &hmenuCurrent
, pt
);
1595 if (!MSG_InternalGetMessage( &msg
, 0, hwnd
, MSGF_MENU
, 0, TRUE
))
1598 TranslateMessage( &msg
);
1601 if ((msg
.message
>= WM_MOUSEFIRST
) && (msg
.message
<= WM_MOUSELAST
))
1603 /* Find the sub-popup for this mouse event (if any) */
1604 HMENU hsubmenu
= MENU_FindMenuByCoords( hmenu
, msg
.pt
);
1608 case WM_RBUTTONDOWN
:
1609 case WM_NCRBUTTONDOWN
:
1610 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1612 case WM_LBUTTONDOWN
:
1613 case WM_NCLBUTTONDOWN
:
1614 fClosed
= !MENU_ButtonDown( hwnd
, hsubmenu
,
1615 &hmenuCurrent
, msg
.pt
);
1619 case WM_NCRBUTTONUP
:
1620 if (!(wFlags
& TPM_RIGHTBUTTON
)) break;
1623 case WM_NCLBUTTONUP
:
1624 /* If outside all menus but inside lprect, ignore it */
1625 if (!hsubmenu
&& lprect
&& PtInRect16(lprect
, msg
.pt
)) break;
1626 fClosed
= !MENU_ButtonUp( hwnd
, hsubmenu
,
1627 &hmenuCurrent
, msg
.pt
);
1628 fRemove
= TRUE
; /* Remove event even if outside menu */
1632 case WM_NCMOUSEMOVE
:
1633 if ((msg
.wParam
& MK_LBUTTON
) ||
1634 ((wFlags
& TPM_RIGHTBUTTON
) && (msg
.wParam
& MK_RBUTTON
)))
1636 fClosed
= !MENU_MouseMove( hwnd
, hsubmenu
,
1637 &hmenuCurrent
, msg
.pt
);
1642 else if ((msg
.message
>= WM_KEYFIRST
) && (msg
.message
<= WM_KEYLAST
))
1644 fRemove
= TRUE
; /* Keyboard messages are always removed */
1652 MENU_SelectItem( hwnd
, hmenuCurrent
, NO_SELECTED_ITEM
, FALSE
);
1656 MENU_SelectItemRel( hwnd
, hmenuCurrent
,
1657 (msg
.wParam
== VK_HOME
)? ITEM_NEXT
: ITEM_PREV
);
1660 case VK_DOWN
: /* If on menu bar, pull-down the menu */
1662 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1663 if (!(menu
->wFlags
& MF_POPUP
) && (hmenuCurrent
== hmenu
))
1664 hmenuCurrent
= MENU_ShowSubPopup( hwnd
, hmenu
, TRUE
);
1666 MENU_SelectItemRel( hwnd
, hmenuCurrent
, ITEM_NEXT
);
1670 MENU_KeyLeft( &hwnd
, &hmenu
, &hmenuCurrent
);
1674 MENU_KeyRight( &hwnd
, &hmenu
, &hmenuCurrent
);
1679 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1690 break; /* WM_KEYDOWN */
1700 break; /* WM_SYSKEYDOWN */
1704 /* Hack to avoid control chars. */
1705 /* We will find a better way real soon... */
1706 if ((msg
.wParam
<= 32) || (msg
.wParam
>= 127)) break;
1707 pos
= MENU_FindItemByKey( hwnd
, hmenuCurrent
, msg
.wParam
);
1708 if (pos
== (UINT
)-2) fClosed
= TRUE
;
1709 else if (pos
== (UINT
)-1) MessageBeep(0);
1712 MENU_SelectItem( hwnd
, hmenuCurrent
, pos
, TRUE
);
1713 fClosed
= !MENU_ExecFocusedItem( hwnd
, hmenuCurrent
,
1718 break; /* WM_CHAR */
1719 } /* switch(msg.message) */
1723 DispatchMessage( &msg
);
1725 if (fEndMenuCalled
) fClosed
= TRUE
;
1726 if (!fClosed
) fRemove
= TRUE
;
1728 if (fRemove
) /* Remove the message from the queue */
1729 PeekMessage16( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
1733 MENU_HideSubPopups( hwnd
, hmenu
, FALSE
);
1734 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
1735 if (menu
&& menu
->wFlags
& MF_POPUP
)
1737 ShowWindow( menu
->hWnd
, SW_HIDE
);
1740 MENU_SelectItem( hwnd
, hmenu
, NO_SELECTED_ITEM
, FALSE
);
1741 SendMessage16( hwnd
, WM_MENUSELECT
, 0, MAKELONG( 0xffff, 0 ) );
1742 fEndMenuCalled
= FALSE
;
1747 /***********************************************************************
1748 * MENU_TrackMouseMenuBar
1750 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
1752 void MENU_TrackMouseMenuBar( HWND hwnd
, POINT16 pt
)
1754 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1756 SendMessage16( hwnd
, WM_ENTERMENULOOP
, 0, 0 );
1757 SendMessage16( hwnd
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1758 MENU_TrackMenu( (HMENU
)wndPtr
->wIDmenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1759 pt
.x
, pt
.y
, hwnd
, NULL
);
1760 SendMessage16( hwnd
, WM_EXITMENULOOP
, 0, 0 );
1765 /***********************************************************************
1766 * MENU_TrackKbdMenuBar
1768 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
1770 void MENU_TrackKbdMenuBar( WND
* wndPtr
, UINT wParam
, INT vkey
)
1772 UINT uItem
= NO_SELECTED_ITEM
;
1775 /* find window that has a menu
1778 if( !(wndPtr
->dwStyle
& WS_CHILD
) )
1780 wndPtr
= WIN_FindWndPtr( GetActiveWindow() );
1781 if( !wndPtr
) return;
1784 while( wndPtr
->dwStyle
& WS_CHILD
&&
1785 !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1786 if( !(wndPtr
= wndPtr
->parent
) ) return;
1788 if( wndPtr
->dwStyle
& WS_CHILD
|| !wndPtr
->wIDmenu
)
1789 if( !(wndPtr
->dwStyle
& WS_SYSMENU
) )
1792 hTrackMenu
= ( IsMenu( wndPtr
->wIDmenu
) )? wndPtr
->wIDmenu
:
1796 SendMessage16( wndPtr
->hwndSelf
, WM_ENTERMENULOOP
, 0, 0 );
1797 SendMessage16( wndPtr
->hwndSelf
, WM_INITMENU
, wndPtr
->wIDmenu
, 0 );
1799 /* find suitable menu entry
1802 if( vkey
== VK_SPACE
)
1803 uItem
= SYSMENU_SELECTED
;
1806 uItem
= MENU_FindItemByKey( wndPtr
->hwndSelf
, wndPtr
->wIDmenu
, vkey
);
1807 if( uItem
>= 0xFFFE )
1809 if( uItem
== 0xFFFF )
1811 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1817 MENU_SelectItem( wndPtr
->hwndSelf
, hTrackMenu
, uItem
, TRUE
);
1818 if( uItem
== NO_SELECTED_ITEM
)
1819 MENU_SelectItemRel( wndPtr
->hwndSelf
, hTrackMenu
, ITEM_NEXT
);
1821 PostMessage( wndPtr
->hwndSelf
, WM_KEYDOWN
, VK_DOWN
, 0L );
1823 MENU_TrackMenu( hTrackMenu
, TPM_LEFTALIGN
| TPM_LEFTBUTTON
,
1824 0, 0, wndPtr
->hwndSelf
, NULL
);
1826 SendMessage16( wndPtr
->hwndSelf
, WM_EXITMENULOOP
, 0, 0 );
1831 /**********************************************************************
1832 * TrackPopupMenu16 (USER.416)
1834 BOOL16
TrackPopupMenu16( HMENU16 hMenu
, UINT16 wFlags
, INT16 x
, INT16 y
,
1835 INT16 nReserved
, HWND16 hWnd
, const RECT16
*lpRect
)
1840 if (MENU_ShowPopup( hWnd
, hMenu
, 0, x
, y
))
1841 ret
= MENU_TrackMenu( hMenu
, wFlags
, 0, 0, hWnd
, lpRect
);
1847 /**********************************************************************
1848 * TrackPopupMenu32 (USER32.548)
1850 BOOL32
TrackPopupMenu32( HMENU32 hMenu
, UINT32 wFlags
, INT32 x
, INT32 y
,
1851 INT32 nReserved
, HWND32 hWnd
, const RECT32
*lpRect
)
1854 CONV_RECT32TO16( lpRect
, &r
);
1855 return TrackPopupMenu16( hMenu
, wFlags
, x
, y
, nReserved
, hWnd
, &r
);
1859 /***********************************************************************
1862 LRESULT
PopupMenuWndProc(HWND hwnd
,UINT message
,WPARAM wParam
,LPARAM lParam
)
1868 CREATESTRUCT16
*cs
= (CREATESTRUCT16
*)PTR_SEG_TO_LIN(lParam
);
1869 SetWindowLong32A( hwnd
, 0, (LONG
)cs
->lpCreateParams
);
1873 case WM_MOUSEACTIVATE
: /* We don't want to be activated */
1874 return MA_NOACTIVATE
;
1879 BeginPaint16( hwnd
, &ps
);
1880 MENU_DrawPopupMenu( hwnd
, ps
.hdc
,
1881 (HMENU
)GetWindowLong32A( hwnd
, 0 ) );
1882 EndPaint16( hwnd
, &ps
);
1887 /* zero out global pointer in case system popup
1888 * was destroyed by AppExit
1891 if( hwnd
== pTopPWnd
->hwndSelf
)
1898 if (wParam
) SetWindowLong32A( hwnd
, 0, (HMENU
)wParam
);
1901 return DefWindowProc16(hwnd
, message
, wParam
, lParam
);
1907 /***********************************************************************
1908 * MENU_GetMenuBarHeight
1910 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
1912 UINT
MENU_GetMenuBarHeight( HWND hwnd
, UINT menubarWidth
, int orgX
, int orgY
)
1919 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
1920 if (!(lppop
= (LPPOPUPMENU
)USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
)))
1922 hdc
= GetDCEx( hwnd
, 0, DCX_CACHE
| DCX_WINDOW
);
1923 SetRect16(&rectBar
, orgX
, orgY
, orgX
+menubarWidth
, orgY
+SYSMETRICS_CYMENU
);
1924 MENU_MenuBarCalcSize( hdc
, &rectBar
, lppop
, hwnd
);
1925 ReleaseDC( hwnd
, hdc
);
1926 return lppop
->Height
;
1930 /*******************************************************************
1931 * ChangeMenu16 (USER.153)
1933 BOOL16
ChangeMenu16( HMENU16 hMenu
, UINT16 pos
, SEGPTR data
,
1934 UINT16 id
, UINT16 flags
)
1936 dprintf_menu( stddeb
,"ChangeMenu16: menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
1937 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1938 if (flags
& MF_APPEND
) return AppendMenu16( hMenu
, flags
& ~MF_APPEND
,
1940 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
1941 /* for MF_DELETE. We should check the parameters for all others */
1942 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
1943 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1944 if (flags
& MF_CHANGE
) return ModifyMenu16( hMenu
, pos
, flags
& ~MF_CHANGE
,
1946 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1947 flags
& MF_BYPOSITION
? pos
: id
,
1948 flags
& ~MF_REMOVE
);
1949 /* Default: MF_INSERT */
1950 return InsertMenu16( hMenu
, pos
, flags
, id
, data
);
1954 /*******************************************************************
1955 * ChangeMenu32A (USER32.22)
1957 BOOL32
ChangeMenu32A( HMENU32 hMenu
, UINT32 pos
, LPCSTR data
,
1958 UINT32 id
, UINT32 flags
)
1960 dprintf_menu( stddeb
,"ChangeMenu32A: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1961 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1962 if (flags
& MF_APPEND
) return AppendMenu32A( hMenu
, flags
& ~MF_APPEND
,
1964 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1965 if (flags
& MF_CHANGE
) return ModifyMenu32A(hMenu
, pos
, flags
& ~MF_CHANGE
,
1967 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1968 flags
& MF_BYPOSITION
? pos
: id
,
1969 flags
& ~MF_REMOVE
);
1970 /* Default: MF_INSERT */
1971 return InsertMenu32A( hMenu
, pos
, flags
, id
, data
);
1975 /*******************************************************************
1976 * ChangeMenu32W (USER32.23)
1978 BOOL32
ChangeMenu32W( HMENU32 hMenu
, UINT32 pos
, LPCWSTR data
,
1979 UINT32 id
, UINT32 flags
)
1981 dprintf_menu( stddeb
,"ChangeMenu32W: menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
1982 hMenu
, pos
, (DWORD
)data
, id
, flags
);
1983 if (flags
& MF_APPEND
) return AppendMenu32W( hMenu
, flags
& ~MF_APPEND
,
1985 if (flags
& MF_DELETE
) return DeleteMenu( hMenu
, pos
, flags
& ~MF_DELETE
);
1986 if (flags
& MF_CHANGE
) return ModifyMenu32W(hMenu
, pos
, flags
& ~MF_CHANGE
,
1988 if (flags
& MF_REMOVE
) return RemoveMenu( hMenu
,
1989 flags
& MF_BYPOSITION
? pos
: id
,
1990 flags
& ~MF_REMOVE
);
1991 /* Default: MF_INSERT */
1992 return InsertMenu32W( hMenu
, pos
, flags
, id
, data
);
1996 /*******************************************************************
1997 * CheckMenuItem (USER.154)
1999 INT
CheckMenuItem( HMENU hMenu
, UINT id
, UINT flags
)
2004 dprintf_menu( stddeb
,"CheckMenuItem: %04x %04x %04x\n", hMenu
, id
, flags
);
2005 if (!(item
= MENU_FindItem( &hMenu
, &id
, flags
))) return -1;
2006 ret
= item
->item_flags
& MF_CHECKED
;
2007 if (flags
& MF_CHECKED
) item
->item_flags
|= MF_CHECKED
;
2008 else item
->item_flags
&= ~MF_CHECKED
;
2013 /**********************************************************************
2014 * EnableMenuItem [USER.155]
2016 BOOL
EnableMenuItem(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
2019 dprintf_menu(stddeb
,"EnableMenuItem (%04x, %04X, %04X) !\n",
2020 hMenu
, wItemID
, wFlags
);
2021 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return FALSE
;
2023 /* We can't have MF_GRAYED and MF_DISABLED together */
2024 if (wFlags
& MF_GRAYED
)
2026 item
->item_flags
= (item
->item_flags
& ~MF_DISABLED
) | MF_GRAYED
;
2028 else if (wFlags
& MF_DISABLED
)
2030 item
->item_flags
= (item
->item_flags
& ~MF_GRAYED
) | MF_DISABLED
;
2032 else /* MF_ENABLED */
2034 item
->item_flags
&= ~(MF_GRAYED
| MF_DISABLED
);
2040 /*******************************************************************
2041 * GetMenuString (USER.161)
2043 int GetMenuString( HMENU hMenu
, UINT wItemID
,
2044 LPSTR str
, short nMaxSiz
, UINT wFlags
)
2048 dprintf_menu( stddeb
, "GetMenuString: menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2049 hMenu
, wItemID
, str
, nMaxSiz
, wFlags
);
2050 if (!str
|| !nMaxSiz
) return 0;
2052 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return 0;
2053 if (!IS_STRING_ITEM(item
->item_flags
)) return 0;
2054 lstrcpyn32A( str
, item
->text
, nMaxSiz
);
2055 dprintf_menu( stddeb
, "GetMenuString: returning '%s'\n", str
);
2060 /**********************************************************************
2061 * HiliteMenuItem [USER.162]
2063 BOOL
HiliteMenuItem(HWND hWnd
, HMENU hMenu
, UINT wItemID
, UINT wHilite
)
2066 dprintf_menu(stddeb
,"HiliteMenuItem(%04x, %04x, %04x, %04x);\n",
2067 hWnd
, hMenu
, wItemID
, wHilite
);
2068 if (!MENU_FindItem( &hMenu
, &wItemID
, wHilite
)) return FALSE
;
2069 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2070 if (menu
->FocusedItem
== wItemID
) return TRUE
;
2071 MENU_HideSubPopups( hWnd
, hMenu
, FALSE
);
2072 MENU_SelectItem( hWnd
, hMenu
, wItemID
, TRUE
);
2077 /**********************************************************************
2078 * GetMenuState [USER.250]
2080 UINT
GetMenuState(HMENU hMenu
, UINT wItemID
, UINT wFlags
)
2083 dprintf_menu(stddeb
,"GetMenuState(%04x, %04x, %04x);\n",
2084 hMenu
, wItemID
, wFlags
);
2085 if (!(item
= MENU_FindItem( &hMenu
, &wItemID
, wFlags
))) return -1;
2086 if (item
->item_flags
& MF_POPUP
)
2088 POPUPMENU
*menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( (HMENU
)item
->item_id
);
2089 if (!menu
) return -1;
2090 else return (menu
->nItems
<< 8) | (menu
->wFlags
& 0xff);
2092 else return item
->item_flags
;
2096 /**********************************************************************
2097 * GetMenuItemCount [USER.263]
2099 INT
GetMenuItemCount(HMENU hMenu
)
2102 dprintf_menu(stddeb
,"GetMenuItemCount(%04x);\n", hMenu
);
2103 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2104 if (menu
== NULL
) return (UINT
)-1;
2105 dprintf_menu(stddeb
,"GetMenuItemCount(%04x) return %d \n",
2106 hMenu
, menu
->nItems
);
2107 return menu
->nItems
;
2111 /**********************************************************************
2112 * GetMenuItemID [USER.264]
2114 UINT
GetMenuItemID(HMENU hMenu
, int nPos
)
2118 dprintf_menu(stddeb
,"GetMenuItemID(%04x, %d);\n", hMenu
, nPos
);
2119 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return -1;
2120 if ((nPos
< 0) || (nPos
>= menu
->nItems
)) return -1;
2121 if (menu
->items
[nPos
].item_flags
& MF_POPUP
) return -1;
2122 return menu
->items
[nPos
].item_id
;
2126 /*******************************************************************
2127 * InsertMenu16 (USER.410)
2129 BOOL16
InsertMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2130 UINT16 id
, SEGPTR data
)
2132 if (IS_STRING_ITEM(flags
) && data
)
2133 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2134 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2135 return InsertMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2139 /*******************************************************************
2140 * InsertMenu32A (USER32.321)
2142 BOOL32
InsertMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2143 UINT32 id
, LPCSTR str
)
2147 if (IS_STRING_ITEM(flags
) && str
)
2148 dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x '%s'\n",
2149 hMenu
, pos
, flags
, id
, str
);
2150 else dprintf_menu( stddeb
, "InsertMenu: %04x %d %04x %04x %08lx\n",
2151 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2153 if (!(item
= MENU_InsertItem( hMenu
, pos
, flags
))) return FALSE
;
2155 if (!(MENU_SetItemData( item
, flags
, id
, str
)))
2157 RemoveMenu( hMenu
, pos
, flags
);
2161 if (flags
& MF_POPUP
) /* Set the MF_POPUP flag on the popup-menu */
2162 ((POPUPMENU
*)USER_HEAP_LIN_ADDR((HMENU
)id
))->wFlags
|= MF_POPUP
;
2164 item
->hCheckBit
= hStdCheck
;
2165 item
->hUnCheckBit
= 0;
2170 /*******************************************************************
2171 * InsertMenu32W (USER32.324)
2173 BOOL32
InsertMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2174 UINT32 id
, LPCWSTR str
)
2178 if (IS_STRING_ITEM(flags
) && str
)
2180 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2181 ret
= InsertMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2185 else return InsertMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2189 /*******************************************************************
2190 * AppendMenu16 (USER.411)
2192 BOOL16
AppendMenu16( HMENU16 hMenu
, UINT16 flags
, UINT16 id
, SEGPTR data
)
2194 return InsertMenu16( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2198 /*******************************************************************
2199 * AppendMenu32A (USER32.4)
2201 BOOL32
AppendMenu32A( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCSTR data
)
2203 return InsertMenu32A( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2207 /*******************************************************************
2208 * AppendMenu32W (USER32.5)
2210 BOOL32
AppendMenu32W( HMENU32 hMenu
, UINT32 flags
, UINT32 id
, LPCWSTR data
)
2212 return InsertMenu32W( hMenu
, -1, flags
| MF_BYPOSITION
, id
, data
);
2216 /**********************************************************************
2217 * RemoveMenu [USER.412]
2219 BOOL
RemoveMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2224 dprintf_menu(stddeb
,"RemoveMenu (%04x, %04x, %04x)\n",hMenu
, nPos
, wFlags
);
2225 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2226 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return FALSE
;
2230 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2231 HeapFree( SystemHeap
, 0, item
->text
);
2232 if (--menu
->nItems
== 0)
2234 HeapFree( SystemHeap
, 0, menu
->items
);
2239 while(nPos
< menu
->nItems
)
2245 menu
->items
= HeapReAlloc( SystemHeap
, 0, menu
->items
,
2246 menu
->nItems
* sizeof(MENUITEM
) );
2252 /**********************************************************************
2253 * DeleteMenu [USER.413]
2255 BOOL
DeleteMenu(HMENU hMenu
, UINT nPos
, UINT wFlags
)
2257 MENUITEM
*item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
);
2258 if (!item
) return FALSE
;
2259 if (item
->item_flags
& MF_POPUP
) DestroyMenu( (HMENU
)item
->item_id
);
2260 /* nPos is now the position of the item */
2261 RemoveMenu( hMenu
, nPos
, wFlags
| MF_BYPOSITION
);
2266 /*******************************************************************
2267 * ModifyMenu16 (USER.414)
2269 BOOL16
ModifyMenu16( HMENU16 hMenu
, UINT16 pos
, UINT16 flags
,
2270 UINT16 id
, SEGPTR data
)
2272 if (IS_STRING_ITEM(flags
))
2273 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
,
2274 (LPSTR
)PTR_SEG_TO_LIN(data
) );
2275 return ModifyMenu32A( hMenu
, (INT32
)(INT16
)pos
, flags
, id
, (LPSTR
)data
);
2279 /*******************************************************************
2280 * ModifyMenu32A (USER32.396)
2282 BOOL32
ModifyMenu32A( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2283 UINT32 id
, LPCSTR str
)
2286 HMENU hMenu16
= hMenu
;
2289 if (IS_STRING_ITEM(flags
))
2291 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x '%s'\n",
2292 hMenu
, pos
, flags
, id
, str
? str
: "#NULL#" );
2293 if (!str
) return FALSE
;
2297 dprintf_menu( stddeb
, "ModifyMenu: %04x %d %04x %04x %08lx\n",
2298 hMenu
, pos
, flags
, id
, (DWORD
)str
);
2301 if (!(item
= MENU_FindItem( &hMenu16
, &pos16
, flags
))) return FALSE
;
2302 return MENU_SetItemData( item
, flags
, id
, str
);
2306 /*******************************************************************
2307 * ModifyMenu32W (USER32.397)
2309 BOOL32
ModifyMenu32W( HMENU32 hMenu
, UINT32 pos
, UINT32 flags
,
2310 UINT32 id
, LPCWSTR str
)
2314 if (IS_STRING_ITEM(flags
) && str
)
2316 LPSTR newstr
= STRING32_DupUniToAnsi( str
);
2317 ret
= ModifyMenu32A( hMenu
, pos
, flags
, id
, newstr
);
2321 else return ModifyMenu32A( hMenu
, pos
, flags
, id
, (LPCSTR
)str
);
2325 /**********************************************************************
2326 * CreatePopupMenu [USER.415]
2328 HMENU
CreatePopupMenu()
2333 if (!(hmenu
= CreateMenu())) return 0;
2334 menu
= (POPUPMENU
*) USER_HEAP_LIN_ADDR( hmenu
);
2335 menu
->wFlags
|= MF_POPUP
;
2340 /**********************************************************************
2341 * GetMenuCheckMarkDimensions [USER.417]
2343 DWORD
GetMenuCheckMarkDimensions()
2345 return MAKELONG( check_bitmap_width
, check_bitmap_height
);
2349 /**********************************************************************
2350 * SetMenuItemBitmaps [USER.418]
2352 BOOL
SetMenuItemBitmaps(HMENU hMenu
, UINT nPos
, UINT wFlags
,
2353 HBITMAP hNewUnCheck
, HBITMAP hNewCheck
)
2356 dprintf_menu(stddeb
,"SetMenuItemBitmaps(%04x, %04x, %04x, %04x, %04x)\n",
2357 hMenu
, nPos
, wFlags
, hNewCheck
, hNewUnCheck
);
2358 if (!(item
= MENU_FindItem( &hMenu
, &nPos
, wFlags
))) return FALSE
;
2360 if (!hNewCheck
&& !hNewUnCheck
)
2362 /* If both are NULL, restore default bitmaps */
2363 item
->hCheckBit
= hStdCheck
;
2364 item
->hUnCheckBit
= 0;
2365 item
->item_flags
&= ~MF_USECHECKBITMAPS
;
2367 else /* Install new bitmaps */
2369 item
->hCheckBit
= hNewCheck
;
2370 item
->hUnCheckBit
= hNewUnCheck
;
2371 item
->item_flags
|= MF_USECHECKBITMAPS
;
2377 /**********************************************************************
2378 * CreateMenu [USER.151]
2384 dprintf_menu(stddeb
,"CreateMenu !\n");
2385 if (!(hMenu
= USER_HEAP_ALLOC( sizeof(POPUPMENU
) )))
2387 menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2389 menu
->wMagic
= MENU_MAGIC
;
2396 menu
->FocusedItem
= NO_SELECTED_ITEM
;
2397 dprintf_menu(stddeb
,"CreateMenu // return %04x\n", hMenu
);
2402 /**********************************************************************
2403 * DestroyMenu [USER.152]
2405 BOOL
DestroyMenu(HMENU hMenu
)
2408 dprintf_menu(stddeb
,"DestroyMenu (%04x) !\n", hMenu
);
2410 if (hMenu
== 0) return FALSE
;
2411 /* Silently ignore attempts to destroy default system menu */
2412 if (hMenu
== MENU_DefSysMenu
) return TRUE
;
2413 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2414 if (!lppop
|| (lppop
->wMagic
!= MENU_MAGIC
)) return FALSE
;
2415 lppop
->wMagic
= 0; /* Mark it as destroyed */
2416 if ((lppop
->wFlags
& MF_POPUP
) && lppop
->hWnd
&& (!pTopPWnd
|| (lppop
->hWnd
!= pTopPWnd
->hwndSelf
)))
2417 DestroyWindow( lppop
->hWnd
);
2422 MENUITEM
*item
= lppop
->items
;
2423 for (i
= lppop
->nItems
; i
> 0; i
--, item
++)
2425 if (item
->item_flags
& MF_POPUP
)
2426 DestroyMenu( (HMENU
)item
->item_id
);
2427 if (IS_STRING_ITEM(item
->item_flags
) && item
->text
)
2428 HeapFree( SystemHeap
, 0, item
->text
);
2430 HeapFree( SystemHeap
, 0, lppop
->items
);
2432 USER_HEAP_FREE( hMenu
);
2433 dprintf_menu(stddeb
,"DestroyMenu (%04x) // End !\n", hMenu
);
2437 /**********************************************************************
2438 * GetSystemMenu [USER.156]
2440 HMENU
GetSystemMenu(HWND hWnd
, BOOL bRevert
)
2442 WND
*wndPtr
= WIN_FindWndPtr( hWnd
);
2443 if (!wndPtr
) return 0;
2445 if (!wndPtr
->hSysMenu
|| (wndPtr
->hSysMenu
== MENU_DefSysMenu
))
2447 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2448 return wndPtr
->hSysMenu
;
2450 if (!bRevert
) return wndPtr
->hSysMenu
;
2451 if (wndPtr
->hSysMenu
) DestroyMenu(wndPtr
->hSysMenu
);
2452 wndPtr
->hSysMenu
= MENU_CopySysMenu();
2453 return wndPtr
->hSysMenu
;
2457 /*******************************************************************
2458 * SetSystemMenu (USER.280)
2460 BOOL
SetSystemMenu( HWND hwnd
, HMENU hMenu
)
2464 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
2465 if (wndPtr
->hSysMenu
&& (wndPtr
->hSysMenu
!= MENU_DefSysMenu
))
2466 DestroyMenu( wndPtr
->hSysMenu
);
2467 wndPtr
->hSysMenu
= hMenu
;
2472 /**********************************************************************
2473 * GetMenu [USER.157]
2475 HMENU
GetMenu(HWND hWnd
)
2477 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2478 if (wndPtr
== NULL
) return 0;
2479 return (HMENU
)wndPtr
->wIDmenu
;
2483 /**********************************************************************
2484 * SetMenu [USER.158]
2486 BOOL
SetMenu(HWND hWnd
, HMENU hMenu
)
2489 WND
* wndPtr
= WIN_FindWndPtr(hWnd
);
2490 if (wndPtr
== NULL
) {
2491 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad window handle !\n",
2495 dprintf_menu(stddeb
,"SetMenu(%04x, %04x);\n", hWnd
, hMenu
);
2496 if (GetCapture() == hWnd
) ReleaseCapture();
2497 wndPtr
->wIDmenu
= (UINT
)hMenu
;
2500 lpmenu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
);
2501 if (lpmenu
== NULL
) {
2502 fprintf(stderr
,"SetMenu(%04x, %04x) // Bad menu handle !\n",
2506 lpmenu
->hWnd
= hWnd
;
2507 lpmenu
->wFlags
&= ~MF_POPUP
; /* Can't be a popup */
2508 lpmenu
->Height
= 0; /* Make sure we recalculate the size */
2510 if (IsWindowVisible(hWnd
))
2511 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2512 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2518 /**********************************************************************
2519 * GetSubMenu [USER.159]
2521 HMENU
GetSubMenu(HMENU hMenu
, short nPos
)
2525 dprintf_menu(stddeb
,"GetSubMenu (%04x, %04X) !\n", hMenu
, nPos
);
2526 if (!(lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR(hMenu
))) return 0;
2527 if ((UINT
)nPos
>= lppop
->nItems
) return 0;
2528 if (!(lppop
->items
[nPos
].item_flags
& MF_POPUP
)) return 0;
2529 return (HMENU
)lppop
->items
[nPos
].item_id
;
2533 /**********************************************************************
2534 * DrawMenuBar [USER.160]
2536 void DrawMenuBar(HWND hWnd
)
2540 dprintf_menu(stddeb
,"DrawMenuBar (%04x)\n", hWnd
);
2541 wndPtr
= WIN_FindWndPtr(hWnd
);
2542 if (wndPtr
!= NULL
&& (wndPtr
->dwStyle
& WS_CHILD
) == 0 &&
2543 wndPtr
->wIDmenu
!= 0) {
2544 dprintf_menu(stddeb
,"DrawMenuBar wIDmenu=%04X \n",
2546 lppop
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR((HMENU
)wndPtr
->wIDmenu
);
2547 if (lppop
== NULL
) return;
2549 lppop
->Height
= 0; /* Make sure we call MENU_MenuBarCalcSize */
2550 SetWindowPos( hWnd
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
|
2551 SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
);
2556 /***********************************************************************
2557 * EndMenu (USER.187)
2561 /* FIXME: this won't work when we have multiple tasks... */
2562 fEndMenuCalled
= TRUE
;
2566 /***********************************************************************
2567 * LookupMenuHandle (USER.217)
2569 HMENU
LookupMenuHandle( HMENU hmenu
, INT id
)
2571 if (!MENU_FindItem( &hmenu
, &id
, MF_BYCOMMAND
)) return 0;
2576 /**********************************************************************
2577 * LoadMenu (USER.150)
2579 HMENU16
LoadMenu16( HINSTANCE16 instance
, SEGPTR name
)
2587 char *str
= (char *)PTR_SEG_TO_LIN( name
);
2588 dprintf_menu( stddeb
, "LoadMenu(%04x,'%s')\n", instance
, str
);
2589 if (str
[0] == '#') name
= (SEGPTR
)atoi( str
+ 1 );
2592 dprintf_resource(stddeb
,"LoadMenu(%04x,%04x)\n",instance
,LOWORD(name
));
2594 if (!name
) return 0;
2596 /* check for Win32 module */
2597 instance
= GetExePtr( instance
);
2598 if (MODULE_GetPtr(instance
)->flags
& NE_FFLAGS_WIN32
)
2599 return LoadMenu32A(instance
,PTR_SEG_TO_LIN(name
));
2601 if (!(hRsrc
= FindResource16( instance
, name
, RT_MENU
))) return 0;
2602 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
2603 hMenu
= LoadMenuIndirect16(LockResource16(handle
));
2604 FreeResource16( handle
);
2609 /*****************************************************************
2610 * LoadMenu32A (USER32.370)
2612 HMENU32
LoadMenu32A( HINSTANCE32 instance
, LPCSTR name
)
2614 HRSRC32 hrsrc
= FindResource32A( instance
, name
, (LPSTR
)RT_MENU
);
2615 if (!hrsrc
) return 0;
2616 return LoadMenuIndirect32A( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2620 /*****************************************************************
2621 * LoadMenu32W (USER32.372)
2623 HMENU32
LoadMenu32W( HINSTANCE32 instance
, LPCWSTR name
)
2625 HRSRC32 hrsrc
= FindResource32W( instance
, name
, (LPWSTR
)RT_MENU
);
2626 if (!hrsrc
) return 0;
2627 return LoadMenuIndirect32W( (LPCVOID
)LoadResource32( instance
, hrsrc
));
2631 /**********************************************************************
2632 * LoadMenuIndirect16 (USER.220)
2634 HMENU16
LoadMenuIndirect16( LPCVOID
template )
2637 WORD version
, offset
;
2638 LPCSTR p
= (LPCSTR
)template;
2640 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2641 version
= GET_WORD(p
);
2645 fprintf( stderr
, "LoadMenuIndirect16: version must be 0 for Win16\n" );
2648 offset
= GET_WORD(p
);
2649 p
+= sizeof(WORD
) + offset
;
2650 if (!(hMenu
= CreateMenu())) return 0;
2651 if (!MENU_ParseResource( p
, hMenu
, FALSE
))
2653 DestroyMenu( hMenu
);
2660 /**********************************************************************
2661 * LoadMenuIndirect32A (USER32.370)
2663 HMENU32
LoadMenuIndirect32A( LPCVOID
template )
2666 WORD version
, offset
;
2667 LPCSTR p
= (LPCSTR
)template;
2669 dprintf_menu(stddeb
,"LoadMenuIndirect32A: %p\n", template );
2670 version
= GET_WORD(p
);
2674 fprintf( stderr
, "LoadMenuIndirect32A: version %d not supported.\n",
2678 offset
= GET_WORD(p
);
2679 p
+= sizeof(WORD
) + offset
;
2680 if (!(hMenu
= CreateMenu())) return 0;
2681 if (!MENU_ParseResource( p
, hMenu
, TRUE
))
2683 DestroyMenu( hMenu
);
2690 /**********************************************************************
2691 * LoadMenuIndirect32W (USER32.371)
2693 HMENU32
LoadMenuIndirect32W( LPCVOID
template )
2695 /* FIXME: is there anything different between A and W? */
2696 return LoadMenuIndirect32A( template );
2700 /**********************************************************************
2703 BOOL
IsMenu( HMENU hmenu
)
2706 if (!(menu
= (LPPOPUPMENU
) USER_HEAP_LIN_ADDR( hmenu
))) return FALSE
;
2707 return (menu
->wMagic
== MENU_MAGIC
);