Newer
Older
static char Copyright[] = "Copyright Martin Ayotte, 1993";
static char Copyright2[] = "Copyright Alexandre Julliard, 1994";
/*
* Note: the style MF_MOUSESELECT is used to mark popup items that
* have been selected, i.e. their popup menu is currently displayed.
* This is probably not the meaning this style has in MS-Windows.
*/
#include "stddebug.h"
/* #define DEBUG_MENU */
/* #undef DEBUG_MENU */
/* #define DEBUG_MENUCALC */
/* #undef DEBUG_MENUCALC */
/* #define DEBUG_MENUSHORTCUT */
/* #undef DEBUG_MENUSHORTCUT */
/* Dimension of the menu bitmaps */
static WORD check_bitmap_width = 0, check_bitmap_height = 0;
static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
/* Flag set by EndMenu() to force an exit from menu tracking */
static BOOL fEndMenuCalled = FALSE;
/* Space between 2 menu bar items */
#define MENU_BAR_ITEMS_SPACE 16
/* Minimum width of a tab character */
#define MENU_TAB_SPACE 8
/* Height of a separator item */
#define SEPARATOR_HEIGHT 5
/* Values for menu->FocusedItem */
/* (other values give the position of the focused item) */
#define NO_SELECTED_ITEM 0xffff
#define SYSMENU_SELECTED 0xfffe /* Only valid on menu-bars */
#define IS_STRING_ITEM(flags) (!((flags) & (MF_BITMAP | MF_OWNERDRAW | \
MF_MENUBARBREAK | MF_MENUBREAK | MF_SEPARATOR)))
extern void NC_DrawSysButton(HWND hwnd, HDC hdc, BOOL down); /* nonclient.c */
extern void CURSOR_SetWinCursor( HWND hwnd, HCURSOR hcursor ); /* cursor.c */
extern BOOL GRAPH_DrawBitmap( HDC hdc, HBITMAP hbitmap, int xdest, int ydest,
int xsrc, int ysrc, int width, int height,
int rop ); /* graphics.c */
static HMENU hSysMenu = 0;
static HBITMAP hStdCheck = 0;
static HBITMAP hStdMnArrow = 0;
WORD * ParseMenuResource(WORD *first_item, int level, HMENU hMenu);
/***********************************************************************
* MENU_Init
*
* Menus initialisation.
*/
BOOL MENU_Init()
{
BITMAP bm;
/* Load bitmaps */
if (!(hStdCheck = LoadBitmap( 0, MAKEINTRESOURCE(OBM_CHECK) )))
return FALSE;
GetObject( hStdCheck, sizeof(BITMAP), (LPSTR)&bm );
check_bitmap_width = bm.bmWidth;
check_bitmap_height = bm.bmHeight;
if (!(hStdMnArrow = LoadBitmap( 0, MAKEINTRESOURCE(OBM_MNARROW) )))
return FALSE;
GetObject( hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm );
arrow_bitmap_width = bm.bmWidth;
arrow_bitmap_height = bm.bmHeight;
/* Load system menu */
if (!(hSysMenu = LoadMenu( hSysRes, "SYSMENU" )))
{
fprintf(stderr,"SysMenu not found in system resources !\n");
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
return FALSE;
}
return TRUE;
}
/***********************************************************************
* MENU_HasSysMenu
*
* Check whether the window owning the menu bar has a system menu.
*/
static BOOL MENU_HasSysMenu( POPUPMENU *menu )
{
WND *wndPtr;
if (menu->wFlags & MF_POPUP) return FALSE;
if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
return (wndPtr->dwStyle & WS_SYSMENU) != 0;
}
/***********************************************************************
* MENU_IsInSysMenu
*
* Check whether the point (in screen coords) is in the system menu
* of the window owning the given menu.
*/
static BOOL MENU_IsInSysMenu( POPUPMENU *menu, POINT pt )
{
WND *wndPtr;
if (menu->wFlags & MF_POPUP) return FALSE;
if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return FALSE;
if (!(wndPtr->dwStyle & WS_SYSMENU)) return FALSE;
if ((pt.x < wndPtr->rectClient.left) ||
(pt.x >= wndPtr->rectClient.left+SYSMETRICS_CXSIZE+SYSMETRICS_CXBORDER))
return FALSE;
if ((pt.y >= wndPtr->rectClient.top - menu->Height) ||
(pt.y < wndPtr->rectClient.top - menu->Height -
SYSMETRICS_CYSIZE - SYSMETRICS_CYBORDER)) return FALSE;
return TRUE;
}
/***********************************************************************
* MENU_FindItem
*
* Find a menu item. Return a pointer on the item, and modifies *hmenu
* in case the item was in a sub-menu.
*/
static MENUITEM *MENU_FindItem( HMENU *hmenu, WORD *nPos, WORD wFlags )
{
POPUPMENU *menu;
MENUITEM *item;
int i;
if (!(menu = (POPUPMENU *) USER_HEAP_ADDR(*hmenu))) return NULL;
item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
if (wFlags & MF_BYPOSITION)
{
if (*nPos >= menu->nItems) return NULL;
return &item[*nPos];
}
else
{
for (i = 0; i < menu->nItems; i++, item++)
{
if (item->item_id == *nPos)
{
*nPos = i;
return item;
}
else if (item->item_flags & MF_POPUP)
{
HMENU hsubmenu = (HMENU)item->item_id;
MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
if (subitem)
{
*hmenu = hsubmenu;
return subitem;
}
}
}
}
return NULL;
}
/***********************************************************************
* MENU_FindItemByCoords
*
* Find the item at the specified coordinates (screen coords).
*/
static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu, int x, int y, WORD *pos )
{
MENUITEM *item;
WND *wndPtr;
int i;
if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return NULL;
x -= wndPtr->rectWindow.left;
y -= wndPtr->rectWindow.top;
item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
for (i = 0; i < menu->nItems; i++, item++)
{
if ((x >= item->rect.left) && (x < item->rect.right) &&
(y >= item->rect.top) && (y < item->rect.bottom))
{
if (pos) *pos = i;
return item;
}
}
return NULL;
}
/***********************************************************************
* MENU_FindItemByKey
*
* Find the menu item selected by a key press.
* Return item id, -1 if none, -2 if we should close the menu.
*/
static WORD MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu, WORD key )
{
POPUPMENU *menu;
LPMENUITEM lpitem;
int i;
LONG menuchar;
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
lpitem = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
if (IS_STRING_ITEM(lpitem->item_flags))
{
char *p = strchr( lpitem->item_text, '&' );
if (p && (p[1] != '&') && (toupper(p[1]) == key)) return i;
}
}
menuchar = SendMessage( hwndOwner, WM_MENUCHAR, key,
MAKELONG( menu->wFlags, hmenu ) );
if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
if (HIWORD(menuchar) == 1) return -2;
return -1;
}
/***********************************************************************
* MENU_CalcItemSize
*
* Calculate the size of the menu item and store it in lpitem->rect.
*/
static void MENU_CalcItemSize( HDC hdc, LPMENUITEM lpitem, HWND hwndOwner,
int orgX, int orgY, BOOL menuBar )
{
DWORD dwSize;
if (lpitem->item_flags & MF_SEPARATOR)
{
lpitem->rect.bottom += SEPARATOR_HEIGHT;
return;
}
if (!menuBar)
{
lpitem->rect.right += 2 * check_bitmap_width;
if (lpitem->item_flags & MF_POPUP)
lpitem->rect.right += arrow_bitmap_width;
}
if (lpitem->item_flags & MF_BITMAP)
{
BITMAP bm;
GetObject( (HBITMAP)lpitem->hText, sizeof(BITMAP), (LPSTR)&bm );
lpitem->rect.right += bm.bmWidth;
lpitem->rect.bottom += bm.bmHeight;
return;
}
/* If we get here, then it is a text item */
dwSize = (lpitem->item_text == NULL) ? 0 : GetTextExtent( hdc, lpitem->item_text, strlen(lpitem->item_text));
lpitem->rect.right += LOWORD(dwSize);
lpitem->rect.bottom += max( HIWORD(dwSize), SYSMETRICS_CYMENU );
if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
else if ((p = strchr( lpitem->item_text, '\t' )) != NULL)
{
/* Item contains a tab (only meaningful in popup menus) */
lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE +
LOWORD( GetTextExtent( hdc, lpitem->item_text,
(int)(p - lpitem->item_text) ));
lpitem->rect.right += MENU_TAB_SPACE;
}
else
{
if (strchr( lpitem->item_text, '\b' ))
lpitem->rect.right += MENU_TAB_SPACE;
lpitem->xTab = lpitem->rect.right - check_bitmap_width
- arrow_bitmap_width;
}
}
/***********************************************************************
* MENU_PopupMenuCalcSize
*
* Calculate the size of a popup menu.
*/
static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
int start, i;
int orgX, orgY, maxX, maxTab, maxTabWidth;
lppop->Width = lppop->Height = 0;
if (lppop->nItems == 0) return;
items = (MENUITEM *)USER_HEAP_ADDR( lppop->hItems );
hdc = GetDC( 0 );
maxX = start = 0;
while (start < lppop->nItems)
/* Parse items until column break or end of menu */
(lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
maxX = max( maxX, lpitem->rect.right );
orgY = lpitem->rect.bottom;
if (lpitem->xTab)
{
maxTab = max( maxTab, lpitem->xTab );
maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
}
}
/* Finish the column (set all items to the largest width found) */
maxX = max( maxX, maxTab + maxTabWidth );
for (lpitem = &items[start]; start < i; start++, lpitem++)
{
lpitem->rect.right = maxX;
if (lpitem->xTab) lpitem->xTab = maxTab;
}
lppop->Height = max( lppop->Height, orgY );
}
lppop->Width = maxX;
}
/***********************************************************************
* MENU_MenuBarCalcSize
*
* Calculate the size of the menu bar.
*/
static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect, LPPOPUPMENU lppop,
HWND hwndOwner )
if ((lprect == NULL) || (lppop == NULL)) return;
if (lppop->nItems == 0) return;
dprintf_menucalc(stddeb,"MenuBarCalcSize left=%d top=%d right=%d bottom=%d !\n",
lprect->left, lprect->top, lprect->right, lprect->bottom);
items = (MENUITEM *)USER_HEAP_ADDR( lppop->hItems );
lppop->Width = lprect->right - lprect->left;
lppop->Height = 0;
maxY = lprect->top;
orgX = lprect->left;
orgY = maxY;
/* Parse items until line break or end of menu */
if ((helpPos == -1) && (lpitem->item_flags & MF_HELP)) helpPos = i;
(lpitem->item_flags & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
else lpitem->rect.right = lprect->right;
}
maxY = max( maxY, lpitem->rect.bottom );
orgX = lpitem->rect.right;
}
/* Finish the line (set all items to the largest height found) */
while (start < i) items[start++].rect.bottom = maxY;
}
lprect->bottom = maxY;
lppop->Height = lprect->bottom - lprect->top;
/* Flush right all items between the MF_HELP and the last item */
/* (if several lines, only move the last line) */
if (helpPos != -1)
{
lpitem = &items[lppop->nItems-1];
orgY = lpitem->rect.top;
orgX = lprect->right;
for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
{
if (lpitem->rect.top != orgY) break; /* Other line */
if (lpitem->rect.right >= orgX) break; /* Too far right already */
lpitem->rect.left += orgX - lpitem->rect.right;
lpitem->rect.right = orgX;
orgX = lpitem->rect.left;
}
}
}
/***********************************************************************
* MENU_DrawMenuItem
*
* Draw a single menu item.
*/
static void MENU_DrawMenuItem( HDC hdc, LPMENUITEM lpitem,
{
RECT rect;
if (menuBar && (lpitem->item_flags & MF_SEPARATOR)) return;
rect = lpitem->rect;
/* Draw the background */
if (lpitem->item_flags & MF_HILITE)
FillRect( hdc, &rect, sysColorObjects.hbrushHighlight );
else FillRect( hdc, &rect, sysColorObjects.hbrushMenu );
SetBkMode( hdc, TRANSPARENT );
/* Draw the separator bar (if any) */
if (!menuBar && (lpitem->item_flags & MF_MENUBARBREAK))
{
SelectObject( hdc, sysColorObjects.hpenWindowFrame );
MoveTo( hdc, rect.left, 0 );
LineTo( hdc, rect.left, height );
}
if (lpitem->item_flags & MF_SEPARATOR)
{
SelectObject( hdc, sysColorObjects.hpenWindowFrame );
MoveTo( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
/* Setup colors */
if (lpitem->item_flags & MF_HILITE)
{
if (lpitem->item_flags & MF_GRAYED)
SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
else
SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
}
else
{
if (lpitem->item_flags & MF_GRAYED)
SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
else
SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
}
if (!menuBar)
{
/* Draw the check mark */
if (lpitem->item_flags & MF_CHECKED)
{
GRAPH_DrawBitmap(hdc, lpitem->hCheckBit ? lpitem->hCheckBit :
hStdCheck, rect.left,
(rect.top+rect.bottom-check_bitmap_height) / 2,
0, 0, check_bitmap_width, check_bitmap_height,
SRCCOPY);
else if (lpitem->hUnCheckBit != 0) /* Not checked */
GRAPH_DrawBitmap(hdc, lpitem->hUnCheckBit, rect.left,
(rect.top+rect.bottom-check_bitmap_height) / 2,
0, 0, check_bitmap_width, check_bitmap_height,
SRCCOPY);
}
/* Draw the popup-menu arrow */
if (lpitem->item_flags & MF_POPUP)
{
GRAPH_DrawBitmap( hdc, hStdMnArrow,
rect.right-arrow_bitmap_width-1,
(rect.top+rect.bottom-arrow_bitmap_height) / 2,
0, 0, arrow_bitmap_width, arrow_bitmap_height,
SRCCOPY );
}
rect.left += check_bitmap_width;
rect.right -= arrow_bitmap_width;
}
/* Draw the item text or bitmap */
if (lpitem->item_flags & MF_BITMAP)
{
GRAPH_DrawBitmap( hdc, (HBITMAP)lpitem->hText, rect.left, rect.top,
0, 0, rect.right-rect.left, rect.bottom-rect.top,
SRCCOPY );
/* No bitmap - process text if present */
else if ((lpitem->item_text) != ((char *) NULL))
if (menuBar)
{
rect.left += MENU_BAR_ITEMS_SPACE / 2;
rect.right -= MENU_BAR_ITEMS_SPACE / 2;
i = strlen( lpitem->item_text );
}
else
{
for (i = 0; lpitem->item_text[i]; i++)
if ((lpitem->item_text[i] == '\t') ||
(lpitem->item_text[i] == '\b')) break;
DrawText( hdc, lpitem->item_text, i, &rect,
DT_LEFT | DT_VCENTER | DT_SINGLELINE );
if (lpitem->item_text[i]) /* There's a tab or flush-right char */
if (lpitem->item_text[i] == '\t')
{
rect.left = lpitem->xTab;
DrawText( hdc, lpitem->item_text + i + 1, -1, &rect,
DT_LEFT | DT_VCENTER | DT_SINGLELINE );
}
else DrawText( hdc, lpitem->item_text + i + 1, -1, &rect,
DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
/***********************************************************************
* MENU_DrawPopupMenu
*
* Paint a popup menu.
static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
{
POPUPMENU *menu;
MENUITEM *item;
RECT rect;
int i;
GetClientRect( hwnd, &rect );
FillRect( hdc, &rect, sysColorObjects.hbrushMenu );
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (!menu || !menu->nItems) return;
item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
for (i = menu->nItems; i > 0; i--, item++)
MENU_DrawMenuItem( hdc, item, menu->Height, FALSE );
}
/***********************************************************************
* MENU_DrawMenuBar
*
* Paint a menu bar. Returns the height of the menu bar.
*/
WORD MENU_DrawMenuBar(HDC hDC, LPRECT lprect, HWND hwnd, BOOL suppress_draw)
{
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
int i;
WND *wndPtr = WIN_FindWndPtr( hwnd );
lppop = (LPPOPUPMENU) USER_HEAP_ADDR( wndPtr->wIDmenu );
if (lppop == NULL || lprect == NULL) return SYSMETRICS_CYMENU;
dprintf_menu(stddeb,"MENU_DrawMenuBar(%04X, %p, %p); !\n",
if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
lprect->bottom = lprect->top + lppop->Height;
if (suppress_draw) return lppop->Height;
FillRect(hDC, lprect, sysColorObjects.hbrushMenu );
SelectObject( hDC, sysColorObjects.hpenWindowFrame );
MoveTo( hDC, lprect->left, lprect->bottom );
LineTo( hDC, lprect->right, lprect->bottom );
if (lppop->nItems == 0) return SYSMETRICS_CYMENU;
lpitem = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
for (i = 0; i < lppop->nItems; i++, lpitem++)
{
MENU_DrawMenuItem( hDC, lpitem, lppop->Height, TRUE );
/***********************************************************************
* MENU_ShowPopup
*
* Display a popup menu.
*/
static BOOL MENU_ShowPopup(HWND hwndOwner, HMENU hmenu, WORD id, int x, int y)
if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return FALSE;
if (menu->FocusedItem != NO_SELECTED_ITEM)
{
MENUITEM *item = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
item[menu->FocusedItem].item_flags &= ~(MF_HILITE | MF_MOUSESELECT);
menu->FocusedItem = NO_SELECTED_ITEM;
}
SendMessage( hwndOwner, WM_INITMENUPOPUP, hmenu,
MAKELONG( id, (menu->wFlags & MF_POPUP) ? 1 : 0 ));
if (!menu->hWnd)
{
WND *wndPtr = WIN_FindWndPtr( hwndOwner );
if (!wndPtr) return FALSE;
menu->hWnd = CreateWindow( POPUPMENU_CLASS_NAME, "",
WS_POPUP | WS_BORDER, x, y,
menu->Width + 2*SYSMETRICS_CXBORDER,
menu->Height + 2*SYSMETRICS_CYBORDER,
0, 0, wndPtr->hInstance,
(LPSTR)(DWORD)hmenu );
if (!menu->hWnd) return FALSE;
}
else SetWindowPos( menu->hWnd, 0, x, y,
menu->Width + 2*SYSMETRICS_CXBORDER,
menu->Height + 2*SYSMETRICS_CYBORDER,
SWP_NOACTIVATE | SWP_NOZORDER );
SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
UpdateWindow( menu->hWnd );
return TRUE;
/***********************************************************************
* MENU_SelectItem
*/
static void MENU_SelectItem( HMENU hmenu, WORD wIndex )
MENUITEM *items;
LPPOPUPMENU lppop;
HDC hdc;
lppop = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (!lppop->nItems) return;
items = (MENUITEM *) USER_HEAP_ADDR( lppop->hItems );
if ((wIndex != NO_SELECTED_ITEM) &&
(wIndex != SYSMENU_SELECTED) &&
(items[wIndex].item_flags & MF_SEPARATOR))
wIndex = NO_SELECTED_ITEM;
if (lppop->FocusedItem == wIndex) return;
if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
/* Clear previous highlighted item */
if (lppop->FocusedItem != NO_SELECTED_ITEM)
if (lppop->FocusedItem == SYSMENU_SELECTED)
NC_DrawSysButton( lppop->hWnd, hdc, FALSE );
else
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
items[lppop->FocusedItem].item_flags &=~(MF_HILITE|MF_MOUSESELECT);
MENU_DrawMenuItem( hdc, &items[lppop->FocusedItem], lppop->Height,
!(lppop->wFlags & MF_POPUP) );
}
}
/* Highlight new item (if any) */
lppop->FocusedItem = wIndex;
if (lppop->FocusedItem != NO_SELECTED_ITEM)
{
if (lppop->FocusedItem == SYSMENU_SELECTED)
NC_DrawSysButton( lppop->hWnd, hdc, TRUE );
else
{
items[lppop->FocusedItem].item_flags |= MF_HILITE;
MENU_DrawMenuItem( hdc, &items[lppop->FocusedItem], lppop->Height,
!(lppop->wFlags & MF_POPUP) );
SendMessage(lppop->hWnd, WM_MENUSELECT,
items[lppop->FocusedItem].item_id,
MAKELONG( hmenu, items[lppop->FocusedItem].item_flags));
}
}
ReleaseDC( lppop->hWnd, hdc );
}
/***********************************************************************
* MENU_SelectNextItem
*/
static void MENU_SelectNextItem( HMENU hmenu )
{
int i;
MENUITEM *items;
POPUPMENU *menu;
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (!menu->nItems) return;
items = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
(menu->FocusedItem != SYSMENU_SELECTED))
{
for (i = menu->FocusedItem+1; i < menu->nItems; i++)
{
if (!(items[i].item_flags & MF_SEPARATOR))
{
MENU_SelectItem( hmenu, i );
return;
}
if (MENU_HasSysMenu( menu ))
{
MENU_SelectItem( hmenu, SYSMENU_SELECTED );
return;
for (i = 0; i < menu->nItems; i++)
{
if (!(items[i].item_flags & MF_SEPARATOR))
{
MENU_SelectItem( hmenu, i );
return;
}
}
if (MENU_HasSysMenu( menu )) MENU_SelectItem( hmenu, SYSMENU_SELECTED );
/***********************************************************************
* MENU_SelectPrevItem
*/
static void MENU_SelectPrevItem( HMENU hmenu )
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
int i;
MENUITEM *items;
POPUPMENU *menu;
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (!menu->nItems) return;
items = (MENUITEM *) USER_HEAP_ADDR( menu->hItems );
if ((menu->FocusedItem != NO_SELECTED_ITEM) &&
(menu->FocusedItem != SYSMENU_SELECTED))
{
for (i = menu->FocusedItem - 1; i >= 0; i--)
{
if (!(items[i].item_flags & MF_SEPARATOR))
{
MENU_SelectItem( hmenu, i );
return;
}
}
if (MENU_HasSysMenu( menu ))
{
MENU_SelectItem( hmenu, SYSMENU_SELECTED );
return;
}
}
for (i = menu->nItems - 1; i > 0; i--)
{
if (!(items[i].item_flags & MF_SEPARATOR))
{
MENU_SelectItem( hmenu, i );
return;
}
}
if (MENU_HasSysMenu( menu )) MENU_SelectItem( hmenu, SYSMENU_SELECTED );
/***********************************************************************
* MENU_GetSubPopup
*
* Return the handle of the selected sub-popup menu (if any).
*/
static HMENU MENU_GetSubPopup( HMENU hmenu )
POPUPMENU *menu;
MENUITEM *item;
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
else if (menu->FocusedItem == SYSMENU_SELECTED)
return GetSystemMenu( menu->hWnd, FALSE );
item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
if (!(item->item_flags & MF_POPUP) || !(item->item_flags & MF_MOUSESELECT))
return 0;
return item->item_id;
/***********************************************************************
* MENU_HideSubPopups
*
* Hide the sub-popup menus of this menu.
*/
static void MENU_HideSubPopups( HMENU hmenu )
MENUITEM *item;
POPUPMENU *menu, *submenu;
HMENU hsubmenu;
if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return;
if (menu->FocusedItem == NO_SELECTED_ITEM) return;
if (menu->FocusedItem == SYSMENU_SELECTED)
{
hsubmenu = GetSystemMenu( menu->hWnd, FALSE );
}
else
{
item = ((MENUITEM *)USER_HEAP_ADDR(menu->hItems)) + menu->FocusedItem;
if (!(item->item_flags & MF_POPUP) ||
!(item->item_flags & MF_MOUSESELECT)) return;
item->item_flags &= ~MF_MOUSESELECT;
hsubmenu = item->item_id;
}
submenu = (POPUPMENU *) USER_HEAP_ADDR( hsubmenu );
MENU_HideSubPopups( hsubmenu );
if (submenu->hWnd) ShowWindow( submenu->hWnd, SW_HIDE );
MENU_SelectItem( hsubmenu, NO_SELECTED_ITEM );
/***********************************************************************
* MENU_ShowSubPopup
*
* Display the sub-menu of the selected item of this menu.
* Return the handle of the submenu, or hmenu if no submenu to display.
*/
static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu, BOOL selectFirst )
POPUPMENU *menu;
MENUITEM *item;
WND *wndPtr;
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
if (!(menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu ))) return hmenu;
if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return hmenu;
if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
if (menu->FocusedItem == SYSMENU_SELECTED)
{
MENU_ShowPopup(hwndOwner, wndPtr->hSysMenu, 0, wndPtr->rectClient.left,
wndPtr->rectClient.top - menu->Height - 2*SYSMETRICS_CYBORDER);
if (selectFirst) MENU_SelectNextItem( wndPtr->hSysMenu );
return wndPtr->hSysMenu;
}
item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
if (!(item->item_flags & MF_POPUP) ||
(item->item_flags & (MF_GRAYED | MF_DISABLED))) return hmenu;
item->item_flags |= MF_MOUSESELECT;
if (menu->wFlags & MF_POPUP)
{
MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width,
wndPtr->rectWindow.top + item->rect.top );
}
else
{
MENU_ShowPopup( hwndOwner, (HMENU)item->item_id, menu->FocusedItem,
wndPtr->rectWindow.left + item->rect.left,
wndPtr->rectWindow.top + item->rect.bottom );
}
if (selectFirst) MENU_SelectNextItem( (HMENU)item->item_id );
return (HMENU)item->item_id;
}
/***********************************************************************
* MENU_FindMenuByCoords
*
* Find the menu containing a given point (in screen coords).
*/
static HMENU MENU_FindMenuByCoords( HMENU hmenu, POINT pt )
{
POPUPMENU *menu;
HWND hwnd;
if (!(hwnd = WindowFromPoint( pt ))) return 0;
while (hmenu)
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (menu->hWnd == hwnd)
if (!(menu->wFlags & MF_POPUP))
{
/* Make sure it's in the menu bar (or in system menu) */
WND *wndPtr = WIN_FindWndPtr( menu->hWnd );
if ((pt.x < wndPtr->rectClient.left) ||
(pt.x >= wndPtr->rectClient.right) ||
(pt.y >= wndPtr->rectClient.top)) return 0;
if (pt.y < wndPtr->rectClient.top - menu->Height)
{
if (!MENU_IsInSysMenu( menu, pt )) return 0;
}
/* else it's in the menu bar */
}
return hmenu;
/***********************************************************************
* MENU_ExecFocusedItem
*
* Execute a menu item (for instance when user pressed Enter).
* Return TRUE if we can go on with menu tracking.
*/
static BOOL MENU_ExecFocusedItem( HWND hwndOwner, HMENU hmenu,
HMENU *hmenuCurrent )
{
MENUITEM *item;
POPUPMENU *menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
if (!menu || !menu->nItems || (menu->FocusedItem == NO_SELECTED_ITEM) ||
(menu->FocusedItem == SYSMENU_SELECTED)) return TRUE;
item = ((MENUITEM *)USER_HEAP_ADDR( menu->hItems )) + menu->FocusedItem;
if (!(item->item_flags & MF_POPUP))
if (!(item->item_flags & (MF_GRAYED | MF_DISABLED)))
PostMessage( hwndOwner, (menu->wFlags & MF_SYSMENU) ?
WM_SYSCOMMAND : WM_COMMAND, item->item_id, 0 );
return FALSE;
else return TRUE;
}
else
{
*hmenuCurrent = MENU_ShowSubPopup( hwndOwner, hmenu, TRUE );
return TRUE;
/***********************************************************************
* MENU_ButtonDown
*
* Handle a button-down event in a menu. Point is in screen coords.
* hmenuCurrent is the top-most visible popup.
* Return TRUE if we can go on with menu tracking.
*/
static BOOL MENU_ButtonDown( HWND hwndOwner, HMENU hmenu, HMENU *hmenuCurrent,
POINT pt )
POPUPMENU *menu;
MENUITEM *item;
WORD id;
if (!hmenu) return FALSE; /* Outside all menus */
menu = (POPUPMENU *) USER_HEAP_ADDR( hmenu );
item = MENU_FindItemByCoords( menu, pt.x, pt.y, &id );
if (!item) /* Maybe in system menu */
{
if (!MENU_IsInSysMenu( menu, pt )) return FALSE;
id = SYSMENU_SELECTED;
}
if (menu->FocusedItem == id)
{