Added missing dependency for 16-bit resource files.
[wine/multimedia.git] / controls / menu.c
blob7aa45a9f84840dd7feb4274faa3b7a365ca33226
1 /*
2 * Menu functions
4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
7 */
9 /*
10 * Note: the style MF_MOUSESELECT is used to mark popup items that
11 * have been selected, i.e. their popup menu is currently displayed.
12 * This is probably not the meaning this style has in MS-Windows.
15 #include <assert.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "windef.h"
21 #include "wingdi.h"
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "wine/winestring.h"
25 #include "win.h"
26 #include "task.h"
27 #include "heap.h"
28 #include "menu.h"
29 #include "nonclient.h"
30 #include "user.h"
31 #include "message.h"
32 #include "queue.h"
33 #include "tweak.h"
34 #include "wine/unicode.h"
36 #include "debugtools.h"
38 DEFAULT_DEBUG_CHANNEL(menu);
39 DECLARE_DEBUG_CHANNEL(accel);
41 /* internal popup menu window messages */
43 #define MM_SETMENUHANDLE (WM_USER + 0)
44 #define MM_GETMENUHANDLE (WM_USER + 1)
46 /* Menu item structure */
47 typedef struct {
48 /* ----------- MENUITEMINFO Stuff ----------- */
49 UINT fType; /* Item type. */
50 UINT fState; /* Item state. */
51 UINT wID; /* Item id. */
52 HMENU hSubMenu; /* Pop-up menu. */
53 HBITMAP hCheckBit; /* Bitmap when checked. */
54 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
55 LPWSTR text; /* Item text or bitmap handle. */
56 DWORD dwItemData; /* Application defined. */
57 DWORD dwTypeData; /* depends on fMask */
58 HBITMAP hbmpItem; /* bitmap in win98 style menus */
59 /* ----------- Wine stuff ----------- */
60 RECT rect; /* Item area (relative to menu window) */
61 UINT xTab; /* X position of text after Tab */
62 } MENUITEM;
64 /* Popup menu structure */
65 typedef struct {
66 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
67 WORD wMagic; /* Magic number */
68 HQUEUE16 hTaskQ; /* Task queue for this menu */
69 WORD Width; /* Width of the whole menu */
70 WORD Height; /* Height of the whole menu */
71 WORD nItems; /* Number of items in the menu */
72 HWND hWnd; /* Window containing the menu */
73 MENUITEM *items; /* Array of menu items */
74 UINT FocusedItem; /* Currently focused item */
75 HWND hwndOwner; /* window receiving the messages for ownerdraw */
76 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
77 /* ------------ MENUINFO members ------ */
78 DWORD dwStyle; /* Extended mennu style */
79 UINT cyMax; /* max hight of the whole menu, 0 is screen hight */
80 HBRUSH hbrBack; /* brush for menu background */
81 DWORD dwContextHelpID;
82 DWORD dwMenuData; /* application defined value */
83 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
84 } POPUPMENU, *LPPOPUPMENU;
86 /* internal flags for menu tracking */
88 #define TF_ENDMENU 0x0001
89 #define TF_SUSPENDPOPUP 0x0002
90 #define TF_SKIPREMOVE 0x0004
92 typedef struct
94 UINT trackFlags;
95 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
96 HMENU hTopMenu; /* initial menu */
97 HWND hOwnerWnd; /* where notifications are sent */
98 POINT pt;
99 } MTRACKER;
101 #define MENU_MAGIC 0x554d /* 'MU' */
102 #define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
104 #define ITEM_PREV -1
105 #define ITEM_NEXT 1
107 /* Internal MENU_TrackMenu() flags */
108 #define TPM_INTERNAL 0xF0000000
109 #define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
110 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
111 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
113 /* popup menu shade thickness */
114 #define POPUP_XSHADE 4
115 #define POPUP_YSHADE 4
117 /* Space between 2 menu bar items */
118 #define MENU_BAR_ITEMS_SPACE 12
120 /* Minimum width of a tab character */
121 #define MENU_TAB_SPACE 8
123 /* Height of a separator item */
124 #define SEPARATOR_HEIGHT 5
126 /* (other menu->FocusedItem values give the position of the focused item) */
127 #define NO_SELECTED_ITEM 0xffff
129 #define MENU_ITEM_TYPE(flags) \
130 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
132 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
133 #define IS_BITMAP_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_BITMAP)
135 #define IS_SYSTEM_MENU(menu) \
136 (!((menu)->wFlags & MF_POPUP) && (menu)->wFlags & MF_SYSMENU)
138 #define IS_SYSTEM_POPUP(menu) \
139 ((menu)->wFlags & MF_POPUP && (menu)->wFlags & MF_SYSMENU)
141 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
142 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
143 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
144 MF_POPUP | MF_SYSMENU | MF_HELP)
145 #define STATE_MASK (~TYPE_MASK)
147 /* Dimension of the menu bitmaps */
148 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
149 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
151 static HBITMAP hStdRadioCheck = 0;
152 static HBITMAP hStdCheck = 0;
153 static HBITMAP hStdMnArrow = 0;
155 /* Minimze/restore/close buttons to be inserted in menubar */
156 static HBITMAP hBmpMinimize = 0;
157 static HBITMAP hBmpMinimizeD = 0;
158 static HBITMAP hBmpMaximize = 0;
159 static HBITMAP hBmpMaximizeD = 0;
160 static HBITMAP hBmpClose = 0;
161 static HBITMAP hBmpCloseD = 0;
164 static HBRUSH hShadeBrush = 0;
165 static HFONT hMenuFont = 0;
166 static HFONT hMenuFontBold = 0;
168 static HMENU MENU_DefSysPopup = 0; /* Default system menu popup */
170 /* Use global popup window because there's no way 2 menus can
171 * be tracked at the same time. */
173 static WND* pTopPopupWnd = 0;
174 static UINT uSubPWndLevel = 0;
176 /* Flag set by EndMenu() to force an exit from menu tracking */
177 static BOOL fEndMenu = FALSE;
180 /***********************************************************************
181 * debug_print_menuitem
183 * Print a menuitem in readable form.
186 #define debug_print_menuitem(pre, mp, post) \
187 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
189 #define MENUOUT(text) \
190 DPRINTF("%s%s", (count++ ? "," : ""), (text))
192 #define MENUFLAG(bit,text) \
193 do { \
194 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
195 } while (0)
197 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
198 const char *postfix)
200 TRACE("%s ", prefix);
201 if (mp) {
202 UINT flags = mp->fType;
203 int typ = MENU_ITEM_TYPE(flags);
204 DPRINTF( "{ ID=0x%x", mp->wID);
205 if (flags & MF_POPUP)
206 DPRINTF( ", Sub=0x%x", mp->hSubMenu);
207 if (flags) {
208 int count = 0;
209 DPRINTF( ", Typ=");
210 if (typ == MFT_STRING)
211 /* Nothing */ ;
212 else if (typ == MFT_SEPARATOR)
213 MENUOUT("sep");
214 else if (typ == MFT_OWNERDRAW)
215 MENUOUT("own");
216 else if (typ == MFT_BITMAP)
217 MENUOUT("bit");
218 else
219 MENUOUT("???");
220 flags -= typ;
222 MENUFLAG(MF_POPUP, "pop");
223 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
224 MENUFLAG(MFT_MENUBREAK, "brk");
225 MENUFLAG(MFT_RADIOCHECK, "radio");
226 MENUFLAG(MFT_RIGHTORDER, "rorder");
227 MENUFLAG(MF_SYSMENU, "sys");
228 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
230 if (flags)
231 DPRINTF( "+0x%x", flags);
233 flags = mp->fState;
234 if (flags) {
235 int count = 0;
236 DPRINTF( ", State=");
237 MENUFLAG(MFS_GRAYED, "grey");
238 MENUFLAG(MFS_DEFAULT, "default");
239 MENUFLAG(MFS_DISABLED, "dis");
240 MENUFLAG(MFS_CHECKED, "check");
241 MENUFLAG(MFS_HILITE, "hi");
242 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
243 MENUFLAG(MF_MOUSESELECT, "mouse");
244 if (flags)
245 DPRINTF( "+0x%x", flags);
247 if (mp->hCheckBit)
248 DPRINTF( ", Chk=0x%x", mp->hCheckBit);
249 if (mp->hUnCheckBit)
250 DPRINTF( ", Unc=0x%x", mp->hUnCheckBit);
252 if (typ == MFT_STRING) {
253 if (mp->text)
254 DPRINTF( ", Text=\"%s\"", debugstr_w(mp->text));
255 else
256 DPRINTF( ", Text=Null");
257 } else if (mp->text == NULL)
258 /* Nothing */ ;
259 else
260 DPRINTF( ", Text=%p", mp->text);
261 if (mp->dwItemData)
262 DPRINTF( ", ItemData=0x%08lx", mp->dwItemData);
263 DPRINTF( " }");
264 } else {
265 DPRINTF( "NULL");
268 DPRINTF(" %s\n", postfix);
271 #undef MENUOUT
272 #undef MENUFLAG
275 /***********************************************************************
276 * MENU_GetMenu
278 * Validate the given menu handle and returns the menu structure pointer.
280 POPUPMENU *MENU_GetMenu(HMENU hMenu)
282 POPUPMENU *menu;
283 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
284 if (!IS_A_MENU(menu))
286 WARN("invalid menu handle=%x, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
287 menu = NULL;
289 return menu;
292 /***********************************************************************
293 * MENU_CopySysPopup
295 * Return the default system menu.
297 static HMENU MENU_CopySysPopup(void)
299 HMENU hMenu = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU");
301 if( hMenu ) {
302 POPUPMENU* menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
303 menu->wFlags |= MF_SYSMENU | MF_POPUP;
304 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
306 else {
307 hMenu = 0;
308 ERR("Unable to load default system menu\n" );
311 TRACE("returning %x.\n", hMenu );
313 return hMenu;
316 /***********************************************************************
317 * MENU_GetTopPopupWnd()
319 * Return the locked pointer pTopPopupWnd.
321 static WND *MENU_GetTopPopupWnd()
323 return WIN_LockWndPtr(pTopPopupWnd);
325 /***********************************************************************
326 * MENU_ReleaseTopPopupWnd()
328 * Realease the locked pointer pTopPopupWnd.
330 static void MENU_ReleaseTopPopupWnd()
332 WIN_ReleaseWndPtr(pTopPopupWnd);
334 /***********************************************************************
335 * MENU_DestroyTopPopupWnd()
337 * Destroy the locked pointer pTopPopupWnd.
339 static void MENU_DestroyTopPopupWnd()
341 WND *tmpWnd = pTopPopupWnd;
342 pTopPopupWnd = NULL;
343 WIN_ReleaseWndPtr(tmpWnd);
348 /**********************************************************************
349 * MENU_GetSysMenu
351 * Create a copy of the system menu. System menu in Windows is
352 * a special menu-bar with the single entry - system menu popup.
353 * This popup is presented to the outside world as a "system menu".
354 * However, the real system menu handle is sometimes seen in the
355 * WM_MENUSELECT paramemters (and Word 6 likes it this way).
357 HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
359 HMENU hMenu;
361 if ((hMenu = CreateMenu()))
363 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
364 menu->wFlags = MF_SYSMENU;
365 menu->hWnd = hWnd;
367 if (hPopupMenu == (HMENU)(-1))
368 hPopupMenu = MENU_CopySysPopup();
369 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
371 if (hPopupMenu)
373 InsertMenuA( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
375 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
376 menu->items[0].fState = 0;
377 menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hPopupMenu);
378 menu->wFlags |= MF_SYSMENU;
380 TRACE("GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
381 return hMenu;
383 DestroyMenu( hMenu );
385 ERR("failed to load system menu!\n");
386 return 0;
390 /***********************************************************************
391 * MENU_Init
393 * Menus initialisation.
395 BOOL MENU_Init()
397 HBITMAP hBitmap;
398 NONCLIENTMETRICSA ncm;
400 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
401 0x55, 0, 0xAA, 0,
402 0x55, 0, 0xAA, 0,
403 0x55, 0, 0xAA, 0 };
405 /* Load menu bitmaps */
406 hStdCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECK));
407 hStdRadioCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_RADIOCHECK));
408 hStdMnArrow = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_MNARROW));
409 /* Load system buttons bitmaps */
410 hBmpMinimize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCE));
411 hBmpMinimizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCED));
412 hBmpMaximize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORE));
413 hBmpMaximizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORED));
414 hBmpClose = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSE));
415 hBmpCloseD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSED));
417 if (hStdCheck)
419 BITMAP bm;
420 GetObjectA( hStdCheck, sizeof(bm), &bm );
421 check_bitmap_width = bm.bmWidth;
422 check_bitmap_height = bm.bmHeight;
423 } else
424 return FALSE;
426 /* Assume that radio checks have the same size as regular check. */
427 if (!hStdRadioCheck)
428 return FALSE;
430 if (hStdMnArrow)
432 BITMAP bm;
433 GetObjectA( hStdMnArrow, sizeof(bm), &bm );
434 arrow_bitmap_width = bm.bmWidth;
435 arrow_bitmap_height = bm.bmHeight;
436 } else
437 return FALSE;
439 if (! (hBitmap = CreateBitmap( 8, 8, 1, 1, shade_bits)))
440 return FALSE;
442 if(!(hShadeBrush = CreatePatternBrush( hBitmap )))
443 return FALSE;
445 DeleteObject( hBitmap );
446 if (!(MENU_DefSysPopup = MENU_CopySysPopup()))
447 return FALSE;
449 ncm.cbSize = sizeof (NONCLIENTMETRICSA);
450 if (!(SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSA), &ncm, 0)))
451 return FALSE;
453 if (!(hMenuFont = CreateFontIndirectA( &ncm.lfMenuFont )))
454 return FALSE;
456 ncm.lfMenuFont.lfWeight += 300;
457 if ( ncm.lfMenuFont.lfWeight > 1000)
458 ncm.lfMenuFont.lfWeight = 1000;
460 if (!(hMenuFontBold = CreateFontIndirectA( &ncm.lfMenuFont )))
461 return FALSE;
463 return TRUE;
466 /***********************************************************************
467 * MENU_InitSysMenuPopup
469 * Grey the appropriate items in System menu.
471 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
473 BOOL gray;
475 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
476 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
477 gray = ((style & WS_MAXIMIZE) != 0);
478 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
479 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
480 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
481 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
482 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
483 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
484 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
485 gray = (clsStyle & CS_NOCLOSE) != 0;
487 /* The menu item must keep its state if it's disabled */
488 if(gray)
489 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
493 /******************************************************************************
495 * UINT MENU_GetStartOfNextColumn(
496 * HMENU hMenu )
498 *****************************************************************************/
500 static UINT MENU_GetStartOfNextColumn(
501 HMENU hMenu )
503 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
504 UINT i = menu->FocusedItem + 1;
506 if(!menu)
507 return NO_SELECTED_ITEM;
509 if( i == NO_SELECTED_ITEM )
510 return i;
512 for( ; i < menu->nItems; ++i ) {
513 if (menu->items[i].fType & MF_MENUBARBREAK)
514 return i;
517 return NO_SELECTED_ITEM;
521 /******************************************************************************
523 * UINT MENU_GetStartOfPrevColumn(
524 * HMENU hMenu )
526 *****************************************************************************/
528 static UINT MENU_GetStartOfPrevColumn(
529 HMENU hMenu )
531 POPUPMENU const *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
532 UINT i;
534 if( !menu )
535 return NO_SELECTED_ITEM;
537 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
538 return NO_SELECTED_ITEM;
540 /* Find the start of the column */
542 for(i = menu->FocusedItem; i != 0 &&
543 !(menu->items[i].fType & MF_MENUBARBREAK);
544 --i); /* empty */
546 if(i == 0)
547 return NO_SELECTED_ITEM;
549 for(--i; i != 0; --i) {
550 if (menu->items[i].fType & MF_MENUBARBREAK)
551 break;
554 TRACE("ret %d.\n", i );
556 return i;
561 /***********************************************************************
562 * MENU_FindItem
564 * Find a menu item. Return a pointer on the item, and modifies *hmenu
565 * in case the item was in a sub-menu.
567 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
569 POPUPMENU *menu;
570 UINT i;
572 if (((*hmenu)==0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
573 if (wFlags & MF_BYPOSITION)
575 if (*nPos >= menu->nItems) return NULL;
576 return &menu->items[*nPos];
578 else
580 MENUITEM *item = menu->items;
581 for (i = 0; i < menu->nItems; i++, item++)
583 if (item->wID == *nPos)
585 *nPos = i;
586 return item;
588 else if (item->fType & MF_POPUP)
590 HMENU hsubmenu = item->hSubMenu;
591 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
592 if (subitem)
594 *hmenu = hsubmenu;
595 return subitem;
600 return NULL;
603 /***********************************************************************
604 * MENU_FindSubMenu
606 * Find a Sub menu. Return the position of the submenu, and modifies
607 * *hmenu in case it is found in another sub-menu.
608 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
610 UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
612 POPUPMENU *menu;
613 UINT i;
614 MENUITEM *item;
615 if (((*hmenu)==0xffff) ||
616 (!(menu = MENU_GetMenu(*hmenu))))
617 return NO_SELECTED_ITEM;
618 item = menu->items;
619 for (i = 0; i < menu->nItems; i++, item++) {
620 if(!(item->fType & MF_POPUP)) continue;
621 if (item->hSubMenu == hSubTarget) {
622 return i;
624 else {
625 HMENU hsubmenu = item->hSubMenu;
626 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
627 if (pos != NO_SELECTED_ITEM) {
628 *hmenu = hsubmenu;
629 return pos;
633 return NO_SELECTED_ITEM;
636 /***********************************************************************
637 * MENU_FreeItemData
639 static void MENU_FreeItemData( MENUITEM* item )
641 /* delete text */
642 if (IS_STRING_ITEM(item->fType) && item->text)
643 HeapFree( SystemHeap, 0, item->text );
646 /***********************************************************************
647 * MENU_FindItemByCoords
649 * Find the item at the specified coordinates (screen coords). Does
650 * not work for child windows and therefore should not be called for
651 * an arbitrary system menu.
653 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu,
654 POINT pt, UINT *pos )
656 MENUITEM *item;
657 UINT i;
658 RECT wrect;
660 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
661 pt.x -= wrect.left;pt.y -= wrect.top;
662 item = menu->items;
663 for (i = 0; i < menu->nItems; i++, item++)
665 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
666 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
668 if (pos) *pos = i;
669 return item;
672 return NULL;
676 /***********************************************************************
677 * MENU_FindItemByKey
679 * Find the menu item selected by a key press.
680 * Return item id, -1 if none, -2 if we should close the menu.
682 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
683 UINT key, BOOL forceMenuChar )
685 TRACE("\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
687 if (!IsMenu( hmenu ))
689 WND* w = WIN_FindWndPtr(hwndOwner);
690 hmenu = GetSubMenu(w->hSysMenu, 0);
691 WIN_ReleaseWndPtr(w);
694 if (hmenu)
696 POPUPMENU *menu = MENU_GetMenu( hmenu );
697 MENUITEM *item = menu->items;
698 LONG menuchar;
700 if( !forceMenuChar )
702 UINT i;
704 key = toupper(key);
705 for (i = 0; i < menu->nItems; i++, item++)
707 if (item->text && (IS_STRING_ITEM(item->fType)))
709 WCHAR *p = item->text - 2;
712 p = strchrW (p + 2, '&');
714 while (p != NULL && p [1] == '&');
715 if (p && (toupper(p[1]) == key)) return i;
719 menuchar = SendMessageA( hwndOwner, WM_MENUCHAR,
720 MAKEWPARAM( key, menu->wFlags ), hmenu );
721 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
722 if (HIWORD(menuchar) == 1) return (UINT)(-2);
724 return (UINT)(-1);
726 /***********************************************************************
727 * MENU_LoadMagicItem
729 * Load the bitmap associated with the magic menu item and its style
732 static HBITMAP MENU_LoadMagicItem(UINT id, BOOL hilite, DWORD dwItemData)
735 * Magic menu item id's section
736 * These magic id's are used by windows to insert "standard" mdi
737 * buttons (minimize,restore,close) on menu. Under windows,
738 * these magic id's make sure the right things appear when those
739 * bitmap buttons are pressed/selected/released.
742 switch(id & 0xffff)
743 { case HBMMENU_SYSTEM:
744 return (dwItemData) ?
745 (HBITMAP)dwItemData :
746 (hilite ? hBmpMinimizeD : hBmpMinimize);
747 case HBMMENU_MBAR_RESTORE:
748 return (hilite ? hBmpMaximizeD: hBmpMaximize);
749 case HBMMENU_MBAR_MINIMIZE:
750 return (hilite ? hBmpMinimizeD : hBmpMinimize);
751 case HBMMENU_MBAR_CLOSE:
752 return (hilite ? hBmpCloseD : hBmpClose);
753 case HBMMENU_CALLBACK:
754 case HBMMENU_MBAR_CLOSE_D:
755 case HBMMENU_MBAR_MINIMIZE_D:
756 case HBMMENU_POPUP_CLOSE:
757 case HBMMENU_POPUP_RESTORE:
758 case HBMMENU_POPUP_MAXIMIZE:
759 case HBMMENU_POPUP_MINIMIZE:
760 default:
761 FIXME("Magic 0x%08x not implemented\n", id);
762 return 0;
767 /***********************************************************************
768 * MENU_CalcItemSize
770 * Calculate the size of the menu item and store it in lpitem->rect.
772 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
773 INT orgX, INT orgY, BOOL menuBar )
775 WCHAR *p;
777 TRACE("dc=0x%04x owner=0x%04x (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
778 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
779 (menuBar ? " (MenuBar)" : ""));
781 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
783 if (lpitem->fType & MF_OWNERDRAW)
786 ** Experimentation under Windows reveals that an owner-drawn
787 ** menu is expected to return the size of the content part of
788 ** the menu item, not including the checkmark nor the submenu
789 ** arrow. Windows adds those values itself and returns the
790 ** enlarged rectangle on subsequent WM_DRAWITEM messages.
792 MEASUREITEMSTRUCT mis;
793 mis.CtlType = ODT_MENU;
794 mis.CtlID = 0;
795 mis.itemID = lpitem->wID;
796 mis.itemData = (DWORD)lpitem->dwItemData;
797 mis.itemHeight = 0;
798 mis.itemWidth = 0;
799 SendMessageA( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
800 lpitem->rect.right += mis.itemWidth;
802 if (menuBar)
804 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
807 /* under at least win95 you seem to be given a standard
808 height for the menu and the height value is ignored */
810 if (TWEAK_WineLook == WIN31_LOOK)
811 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENU);
812 else
813 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENU)-1;
815 else
816 lpitem->rect.bottom += mis.itemHeight;
818 TRACE("id=%04x size=%dx%d\n",
819 lpitem->wID, mis.itemWidth, mis.itemHeight);
820 /* Fall through to get check/arrow width calculation. */
823 if (lpitem->fType & MF_SEPARATOR)
825 lpitem->rect.bottom += SEPARATOR_HEIGHT;
826 return;
829 if (!menuBar)
831 lpitem->rect.right += 2 * check_bitmap_width;
832 if (lpitem->fType & MF_POPUP)
833 lpitem->rect.right += arrow_bitmap_width;
836 if (lpitem->fType & MF_OWNERDRAW)
837 return;
839 if (IS_BITMAP_ITEM(lpitem->fType))
841 BITMAP bm;
842 HBITMAP resBmp = 0;
844 /* Check if there is a magic menu item associated with this item */
845 if((LOWORD((int)lpitem->text))<12)
847 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fType & MF_HILITE),
848 lpitem->dwItemData);
850 else
851 resBmp = (HBITMAP)lpitem->text;
853 if (GetObjectA(resBmp, sizeof(bm), &bm ))
855 lpitem->rect.right += bm.bmWidth;
856 lpitem->rect.bottom += bm.bmHeight;
857 if (TWEAK_WineLook == WIN98_LOOK) {
858 /* Leave space for the sunken border */
859 lpitem->rect.right += 2;
860 lpitem->rect.bottom += 2;
867 /* If we get here, then it must be a text item */
868 if (IS_STRING_ITEM( lpitem->fType ))
869 { SIZE size;
871 GetTextExtentPoint32W(hdc, lpitem->text, strlenW(lpitem->text), &size);
873 lpitem->rect.right += size.cx;
874 if (TWEAK_WineLook == WIN31_LOOK)
875 lpitem->rect.bottom += max( size.cy, GetSystemMetrics(SM_CYMENU) );
876 else
877 lpitem->rect.bottom += max(size.cy, GetSystemMetrics(SM_CYMENU)-1);
878 lpitem->xTab = 0;
880 if (menuBar)
882 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
884 else if ((p = strchrW( lpitem->text, '\t' )) != NULL)
886 /* Item contains a tab (only meaningful in popup menus) */
887 GetTextExtentPoint32W(hdc, lpitem->text, (int)(p - lpitem->text) , &size);
888 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + size.cx;
889 lpitem->rect.right += MENU_TAB_SPACE;
891 else
893 if (strchrW( lpitem->text, '\b' ))
894 lpitem->rect.right += MENU_TAB_SPACE;
895 lpitem->xTab = lpitem->rect.right - check_bitmap_width
896 - arrow_bitmap_width;
899 TRACE("(%d,%d)-(%d,%d)\n", lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom);
903 /***********************************************************************
904 * MENU_PopupMenuCalcSize
906 * Calculate the size of a popup menu.
908 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
910 MENUITEM *lpitem;
911 HDC hdc;
912 int start, i;
913 int orgX, orgY, maxX, maxTab, maxTabWidth;
915 lppop->Width = lppop->Height = 0;
916 if (lppop->nItems == 0) return;
917 hdc = GetDC( 0 );
919 SelectObject( hdc, hMenuFont);
921 start = 0;
922 maxX = (TWEAK_WineLook == WIN31_LOOK) ? GetSystemMetrics(SM_CXBORDER) : 2+1 ;
924 while (start < lppop->nItems)
926 lpitem = &lppop->items[start];
927 orgX = maxX;
928 orgY = (TWEAK_WineLook == WIN31_LOOK) ? GetSystemMetrics(SM_CYBORDER) : 2;
930 maxTab = maxTabWidth = 0;
932 /* Parse items until column break or end of menu */
933 for (i = start; i < lppop->nItems; i++, lpitem++)
935 if ((i != start) &&
936 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
938 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
940 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
941 maxX = max( maxX, lpitem->rect.right );
942 orgY = lpitem->rect.bottom;
943 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
945 maxTab = max( maxTab, lpitem->xTab );
946 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
950 /* Finish the column (set all items to the largest width found) */
951 maxX = max( maxX, maxTab + maxTabWidth );
952 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
954 lpitem->rect.right = maxX;
955 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
956 lpitem->xTab = maxTab;
959 lppop->Height = max( lppop->Height, orgY );
962 lppop->Width = maxX;
964 /* space for 3d border */
965 if(TWEAK_WineLook > WIN31_LOOK)
967 lppop->Height += 2;
968 lppop->Width += 2;
971 ReleaseDC( 0, hdc );
975 /***********************************************************************
976 * MENU_MenuBarCalcSize
978 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
979 * height is off by 1 pixel which causes lengthy window relocations when
980 * active document window is maximized/restored.
982 * Calculate the size of the menu bar.
984 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
985 LPPOPUPMENU lppop, HWND hwndOwner )
987 MENUITEM *lpitem;
988 int start, i, orgX, orgY, maxY, helpPos;
990 if ((lprect == NULL) || (lppop == NULL)) return;
991 if (lppop->nItems == 0) return;
992 TRACE("left=%d top=%d right=%d bottom=%d\n",
993 lprect->left, lprect->top, lprect->right, lprect->bottom);
994 lppop->Width = lprect->right - lprect->left;
995 lppop->Height = 0;
996 maxY = lprect->top+1;
997 start = 0;
998 helpPos = -1;
999 while (start < lppop->nItems)
1001 lpitem = &lppop->items[start];
1002 orgX = lprect->left;
1003 orgY = maxY;
1005 /* Parse items until line break or end of menu */
1006 for (i = start; i < lppop->nItems; i++, lpitem++)
1008 if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
1009 if ((i != start) &&
1010 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1012 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n",
1013 orgX, orgY );
1014 debug_print_menuitem (" item: ", lpitem, "");
1015 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
1017 if (lpitem->rect.right > lprect->right)
1019 if (i != start) break;
1020 else lpitem->rect.right = lprect->right;
1022 maxY = max( maxY, lpitem->rect.bottom );
1023 orgX = lpitem->rect.right;
1026 /* Finish the line (set all items to the largest height found) */
1027 while (start < i) lppop->items[start++].rect.bottom = maxY;
1030 lprect->bottom = maxY;
1031 lppop->Height = lprect->bottom - lprect->top;
1033 if (TWEAK_WineLook == WIN31_LOOK) {
1034 /* Flush right all magic items and items between the MF_HELP and */
1035 /* the last item (if several lines, only move the last line) */
1036 lpitem = &lppop->items[lppop->nItems-1];
1037 orgY = lpitem->rect.top;
1038 orgX = lprect->right;
1039 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--) {
1040 if ( !IS_BITMAP_ITEM(lpitem->fType) && ((helpPos==-1) || (helpPos>i) ))
1041 break; /* done */
1042 if (lpitem->rect.top != orgY) break; /* Other line */
1043 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1044 lpitem->rect.left += orgX - lpitem->rect.right;
1045 lpitem->rect.right = orgX;
1046 orgX = lpitem->rect.left;
1051 /***********************************************************************
1052 * MENU_DrawMenuItem
1054 * Draw a single menu item.
1056 static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1057 UINT height, BOOL menuBar, UINT odaction )
1059 RECT rect;
1061 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1063 if (lpitem->fType & MF_SYSMENU)
1065 if( !IsIconic(hwnd) ) {
1066 if (TWEAK_WineLook > WIN31_LOOK)
1067 NC_DrawSysButton95( hwnd, hdc,
1068 lpitem->fState &
1069 (MF_HILITE | MF_MOUSESELECT) );
1070 else
1071 NC_DrawSysButton( hwnd, hdc,
1072 lpitem->fState &
1073 (MF_HILITE | MF_MOUSESELECT) );
1076 return;
1079 if (lpitem->fType & MF_OWNERDRAW)
1082 ** Experimentation under Windows reveals that an owner-drawn
1083 ** menu is given the rectangle which includes the space it requested
1084 ** in its response to WM_MEASUREITEM _plus_ width for a checkmark
1085 ** and a popup-menu arrow. This is the value of lpitem->rect.
1086 ** Windows will leave all drawing to the application except for
1087 ** the popup-menu arrow. Windows always draws that itself, after
1088 ** the menu owner has finished drawing.
1090 DRAWITEMSTRUCT dis;
1092 dis.CtlType = ODT_MENU;
1093 dis.CtlID = 0;
1094 dis.itemID = lpitem->wID;
1095 dis.itemData = (DWORD)lpitem->dwItemData;
1096 dis.itemState = 0;
1097 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1098 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED;
1099 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1100 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1101 dis.hwndItem = hmenu;
1102 dis.hDC = hdc;
1103 dis.rcItem = lpitem->rect;
1104 TRACE("Ownerdraw: owner=%04x itemID=%d, itemState=%d, itemAction=%d, "
1105 "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n", hwndOwner,
1106 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1107 dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
1108 dis.rcItem.bottom);
1109 SendMessageA( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1110 /* Fall through to draw popup-menu arrow */
1113 TRACE("rect={%d,%d,%d,%d}\n", lpitem->rect.left, lpitem->rect.top,
1114 lpitem->rect.right,lpitem->rect.bottom);
1116 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1118 rect = lpitem->rect;
1120 if (!(lpitem->fType & MF_OWNERDRAW))
1122 if (lpitem->fState & MF_HILITE)
1124 if(TWEAK_WineLook == WIN98_LOOK)
1126 if(menuBar)
1127 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1128 else
1129 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1131 else /* Not Win98 Look */
1133 if(!IS_BITMAP_ITEM(lpitem->fType))
1134 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1137 else
1138 FillRect( hdc, &rect, GetSysColorBrush(COLOR_MENU) );
1141 SetBkMode( hdc, TRANSPARENT );
1143 if (!(lpitem->fType & MF_OWNERDRAW))
1145 /* vertical separator */
1146 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1148 if (TWEAK_WineLook > WIN31_LOOK)
1150 RECT rc = rect;
1151 rc.top = 3;
1152 rc.bottom = height - 3;
1153 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1155 else
1157 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1158 MoveToEx( hdc, rect.left, 0, NULL );
1159 LineTo( hdc, rect.left, height );
1163 /* horizontal separator */
1164 if (lpitem->fType & MF_SEPARATOR)
1166 if (TWEAK_WineLook > WIN31_LOOK)
1168 RECT rc = rect;
1169 rc.left++;
1170 rc.right--;
1171 rc.top += SEPARATOR_HEIGHT / 2;
1172 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1174 else
1176 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1177 MoveToEx( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2, NULL );
1178 LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
1180 return;
1184 /* Setup colors */
1186 if (lpitem->fState & MF_HILITE)
1188 if(TWEAK_WineLook == WIN98_LOOK)
1190 if(menuBar)
1191 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1192 else
1194 if(lpitem->fState & MF_GRAYED)
1195 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1196 else
1197 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1199 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1201 else /* Not Win98 Look */
1203 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1204 if(!IS_BITMAP_ITEM(lpitem->fType))
1205 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1206 else
1207 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1210 else
1212 if (lpitem->fState & MF_GRAYED)
1213 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1214 else
1215 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1216 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
1219 /* helper lines for debugging */
1220 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1221 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1222 MoveToEx( hdc, rect.left, (rect.top + rect.bottom)/2, NULL );
1223 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1226 if (!menuBar)
1228 INT y = rect.top + rect.bottom;
1230 if (!(lpitem->fType & MF_OWNERDRAW))
1232 /* Draw the check mark
1234 * FIXME:
1235 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1238 if (lpitem->fState & MF_CHECKED)
1240 HBITMAP bm = lpitem->hCheckBit ? lpitem->hCheckBit :
1241 ((lpitem->fType & MFT_RADIOCHECK) ? hStdRadioCheck : hStdCheck);
1242 HDC hdcMem = CreateCompatibleDC( hdc );
1244 SelectObject( hdcMem, bm );
1245 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1246 check_bitmap_width, check_bitmap_height,
1247 hdcMem, 0, 0, SRCCOPY );
1248 DeleteDC( hdcMem );
1250 else if (lpitem->hUnCheckBit)
1252 HDC hdcMem = CreateCompatibleDC( hdc );
1254 SelectObject( hdcMem, lpitem->hUnCheckBit );
1255 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1256 check_bitmap_width, check_bitmap_height,
1257 hdcMem, 0, 0, SRCCOPY );
1258 DeleteDC( hdcMem );
1262 /* Draw the popup-menu arrow */
1263 if (lpitem->fType & MF_POPUP)
1265 HDC hdcMem = CreateCompatibleDC( hdc );
1266 HBITMAP hOrigBitmap;
1268 hOrigBitmap = SelectObject( hdcMem, hStdMnArrow );
1269 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1270 (y - arrow_bitmap_height) / 2,
1271 arrow_bitmap_width, arrow_bitmap_height,
1272 hdcMem, 0, 0, SRCCOPY );
1273 SelectObject( hdcMem, hOrigBitmap );
1274 DeleteDC( hdcMem );
1277 rect.left += check_bitmap_width;
1278 rect.right -= arrow_bitmap_width;
1281 /* Done for owner-drawn */
1282 if (lpitem->fType & MF_OWNERDRAW)
1283 return;
1285 /* Draw the item text or bitmap */
1286 if (IS_BITMAP_ITEM(lpitem->fType))
1287 { int top;
1289 HBITMAP resBmp = 0;
1291 HDC hdcMem = CreateCompatibleDC( hdc );
1294 * Check if there is a magic menu item associated with this item
1295 * and load the appropriate bitmap
1297 if((LOWORD((int)lpitem->text)) < 12)
1299 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fState & MF_HILITE),
1300 lpitem->dwItemData);
1302 else
1303 resBmp = (HBITMAP)lpitem->text;
1305 if (resBmp)
1307 BITMAP bm;
1308 GetObjectA( resBmp, sizeof(bm), &bm );
1310 SelectObject(hdcMem,resBmp );
1312 /* handle fontsize > bitmap_height */
1313 top = ((rect.bottom-rect.top)>bm.bmHeight) ?
1314 rect.top+(rect.bottom-rect.top-bm.bmHeight)/2 : rect.top;
1316 BitBlt( hdc, rect.left, top, rect.right - rect.left,
1317 rect.bottom - rect.top, hdcMem, 0, 0,
1318 ((lpitem->fState & MF_HILITE) && !((LOWORD((DWORD)lpitem->text)) < 12)) ? NOTSRCCOPY : SRCCOPY );
1320 DeleteDC( hdcMem );
1322 return;
1325 /* No bitmap - process text if present */
1326 else if (IS_STRING_ITEM(lpitem->fType))
1328 register int i;
1329 HFONT hfontOld = 0;
1331 UINT uFormat = (menuBar) ?
1332 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1333 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1335 if ( lpitem->fState & MFS_DEFAULT )
1337 hfontOld = SelectObject( hdc, hMenuFontBold);
1340 if (menuBar)
1342 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1343 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1344 i = strlenW( lpitem->text );
1346 else
1348 for (i = 0; lpitem->text[i]; i++)
1349 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1350 break;
1353 if( !(TWEAK_WineLook == WIN31_LOOK) && (lpitem->fState & MF_GRAYED))
1355 if (!(lpitem->fState & MF_HILITE) )
1357 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1358 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1359 DrawTextW( hdc, lpitem->text, i, &rect, uFormat );
1360 --rect.left; --rect.top; --rect.right; --rect.bottom;
1362 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1365 DrawTextW( hdc, lpitem->text, i, &rect, uFormat);
1367 /* paint the shortcut text */
1368 if (lpitem->text[i]) /* There's a tab or flush-right char */
1370 if (lpitem->text[i] == '\t')
1372 rect.left = lpitem->xTab;
1373 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1375 else
1377 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1380 if( !(TWEAK_WineLook == WIN31_LOOK) && (lpitem->fState & MF_GRAYED))
1382 if (!(lpitem->fState & MF_HILITE) )
1384 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1385 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1386 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1387 --rect.left; --rect.top; --rect.right; --rect.bottom;
1389 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1391 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1394 if (hfontOld)
1395 SelectObject (hdc, hfontOld);
1400 /***********************************************************************
1401 * MENU_DrawPopupMenu
1403 * Paint a popup menu.
1405 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1407 HBRUSH hPrevBrush = 0;
1408 RECT rect;
1410 TRACE("wnd=0x%04x dc=0x%04x menu=0x%04x\n", hwnd, hdc, hmenu);
1412 GetClientRect( hwnd, &rect );
1414 if(TWEAK_WineLook == WIN31_LOOK)
1416 rect.bottom -= POPUP_YSHADE * GetSystemMetrics(SM_CYBORDER);
1417 rect.right -= POPUP_XSHADE * GetSystemMetrics(SM_CXBORDER);
1420 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1421 && (SelectObject( hdc, hMenuFont)))
1423 HPEN hPrevPen;
1425 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1427 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1428 if( hPrevPen )
1430 INT ropPrev, i;
1431 POPUPMENU *menu;
1433 /* draw 3-d shade */
1434 if(TWEAK_WineLook == WIN31_LOOK) {
1435 SelectObject( hdc, hShadeBrush );
1436 SetBkMode( hdc, TRANSPARENT );
1437 ropPrev = SetROP2( hdc, R2_MASKPEN );
1439 i = rect.right; /* why SetBrushOrg() doesn't? */
1440 PatBlt( hdc, i & 0xfffffffe,
1441 rect.top + POPUP_YSHADE*GetSystemMetrics(SM_CYBORDER),
1442 i%2 + POPUP_XSHADE*GetSystemMetrics(SM_CXBORDER),
1443 rect.bottom - rect.top, 0x00a000c9 );
1444 i = rect.bottom;
1445 PatBlt( hdc, rect.left + POPUP_XSHADE*GetSystemMetrics(SM_CXBORDER),
1446 i & 0xfffffffe,rect.right - rect.left,
1447 i%2 + POPUP_YSHADE*GetSystemMetrics(SM_CYBORDER), 0x00a000c9 );
1448 SelectObject( hdc, hPrevPen );
1449 SelectObject( hdc, hPrevBrush );
1450 SetROP2( hdc, ropPrev );
1452 else
1453 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1455 /* draw menu items */
1457 menu = MENU_GetMenu( hmenu );
1458 if (menu && menu->nItems)
1460 MENUITEM *item;
1461 UINT u;
1463 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1464 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc, item,
1465 menu->Height, FALSE, ODA_DRAWENTIRE );
1468 } else
1470 SelectObject( hdc, hPrevBrush );
1475 /***********************************************************************
1476 * MENU_DrawMenuBar
1478 * Paint a menu bar. Returns the height of the menu bar.
1479 * called from [windows/nonclient.c]
1481 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1482 BOOL suppress_draw)
1484 LPPOPUPMENU lppop;
1485 UINT i,retvalue;
1486 HFONT hfontOld = 0;
1488 WND *wndPtr = WIN_FindWndPtr( hwnd );
1490 lppop = MENU_GetMenu ((HMENU)wndPtr->wIDmenu );
1491 if (lppop == NULL || lprect == NULL)
1493 retvalue = GetSystemMetrics(SM_CYMENU);
1494 goto END;
1497 TRACE("(%04x, %p, %p)\n", hDC, lprect, lppop);
1499 hfontOld = SelectObject( hDC, hMenuFont);
1501 if (lppop->Height == 0)
1502 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1504 lprect->bottom = lprect->top + lppop->Height;
1506 if (suppress_draw)
1508 retvalue = lppop->Height;
1509 goto END;
1512 FillRect(hDC, lprect, GetSysColorBrush(COLOR_MENU) );
1514 if (TWEAK_WineLook == WIN31_LOOK)
1516 SelectObject( hDC, GetSysColorPen(COLOR_WINDOWFRAME) );
1517 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
1518 LineTo( hDC, lprect->right, lprect->bottom );
1520 else
1522 SelectObject( hDC, GetSysColorPen(COLOR_3DFACE));
1523 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
1524 LineTo( hDC, lprect->right, lprect->bottom );
1527 if (lppop->nItems == 0)
1529 retvalue = GetSystemMetrics(SM_CYMENU);
1530 goto END;
1533 for (i = 0; i < lppop->nItems; i++)
1535 MENU_DrawMenuItem( hwnd, (HMENU)wndPtr->wIDmenu, hwnd,
1536 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
1538 retvalue = lppop->Height;
1540 END:
1541 if (hfontOld)
1542 SelectObject (hDC, hfontOld);
1544 WIN_ReleaseWndPtr(wndPtr);
1545 return retvalue;
1548 /***********************************************************************
1549 * MENU_PatchResidentPopup
1551 BOOL MENU_PatchResidentPopup( HQUEUE16 checkQueue, WND* checkWnd )
1553 WND *pTPWnd = MENU_GetTopPopupWnd();
1555 if( pTPWnd )
1557 HTASK16 hTask = 0;
1559 TRACE("patching resident popup: %04x %04x [%04x %04x]\n",
1560 checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTPWnd->hmemTaskQ,
1561 pTPWnd->owner ? pTPWnd->owner->hwndSelf : 0);
1563 switch( checkQueue )
1565 case 0: /* checkWnd is the new popup owner */
1566 if( checkWnd )
1568 pTPWnd->owner = checkWnd;
1569 if( pTPWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1570 hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1572 break;
1574 case 0xFFFF: /* checkWnd is destroyed */
1575 if( pTPWnd->owner == checkWnd )
1576 pTPWnd->owner = NULL;
1577 MENU_ReleaseTopPopupWnd();
1578 return TRUE;
1580 default: /* checkQueue is exiting */
1581 if( pTPWnd->hmemTaskQ == checkQueue )
1583 hTask = QUEUE_GetQueueTask( pTPWnd->hmemTaskQ );
1584 hTask = TASK_GetNextTask( hTask );
1586 break;
1589 if( hTask )
1591 TDB* task = (TDB*)GlobalLock16( hTask );
1592 if( task )
1594 pTPWnd->hInstance = task->hInstance;
1595 pTPWnd->hmemTaskQ = task->hQueue;
1596 MENU_ReleaseTopPopupWnd();
1597 return TRUE;
1599 else WARN("failed to patch resident popup.\n");
1602 MENU_ReleaseTopPopupWnd();
1603 return FALSE;
1606 /***********************************************************************
1607 * MENU_ShowPopup
1609 * Display a popup menu.
1611 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1612 INT x, INT y, INT xanchor, INT yanchor )
1614 POPUPMENU *menu;
1615 WND *wndOwner = NULL;
1617 TRACE("owner=0x%04x hmenu=0x%04x id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1618 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1620 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1621 if (menu->FocusedItem != NO_SELECTED_ITEM)
1623 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1624 menu->FocusedItem = NO_SELECTED_ITEM;
1627 /* store the owner for DrawItem*/
1628 menu->hwndOwner = hwndOwner;
1630 if( (wndOwner = WIN_FindWndPtr( hwndOwner )) )
1632 UINT width, height;
1634 MENU_PopupMenuCalcSize( menu, hwndOwner );
1636 /* adjust popup menu pos so that it fits within the desktop */
1638 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1639 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1641 if( x + width > GetSystemMetrics(SM_CXSCREEN ))
1643 if( xanchor )
1644 x -= width - xanchor;
1645 if( x + width > GetSystemMetrics(SM_CXSCREEN))
1646 x = GetSystemMetrics(SM_CXSCREEN) - width;
1648 if( x < 0 ) x = 0;
1650 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1652 if( yanchor )
1653 y -= height + yanchor;
1654 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1655 y = GetSystemMetrics(SM_CYSCREEN) - height;
1657 if( y < 0 ) y = 0;
1659 if( TWEAK_WineLook == WIN31_LOOK )
1661 width += POPUP_XSHADE * GetSystemMetrics(SM_CXBORDER); /* add space for shading */
1662 height += POPUP_YSHADE * GetSystemMetrics(SM_CYBORDER);
1665 /* NOTE: In Windows, top menu popup is not owned. */
1666 if (!pTopPopupWnd) /* create top level popup menu window */
1668 assert( uSubPWndLevel == 0 );
1670 pTopPopupWnd = WIN_FindWndPtr(CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1671 WS_POPUP, x, y, width, height,
1672 hwndOwner, 0, wndOwner->hInstance,
1673 (LPVOID)hmenu ));
1674 if (!pTopPopupWnd)
1676 WIN_ReleaseWndPtr(wndOwner);
1677 return FALSE;
1679 menu->hWnd = pTopPopupWnd->hwndSelf;
1680 MENU_ReleaseTopPopupWnd();
1682 else
1683 if( uSubPWndLevel )
1685 /* create a new window for the submenu */
1687 menu->hWnd = CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1688 WS_POPUP, x, y, width, height,
1689 hwndOwner, 0, wndOwner->hInstance,
1690 (LPVOID)hmenu );
1691 if( !menu->hWnd )
1693 WIN_ReleaseWndPtr(wndOwner);
1694 return FALSE;
1697 else /* top level popup menu window already exists */
1699 WND *pTPWnd = MENU_GetTopPopupWnd();
1700 menu->hWnd = pTPWnd->hwndSelf;
1702 MENU_PatchResidentPopup( 0, wndOwner );
1703 SendMessageA( pTPWnd->hwndSelf, MM_SETMENUHANDLE, (WPARAM16)hmenu, 0L);
1705 /* adjust its size */
1707 SetWindowPos( menu->hWnd, 0, x, y, width, height,
1708 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1709 MENU_ReleaseTopPopupWnd();
1712 uSubPWndLevel++; /* menu level counter */
1714 /* Display the window */
1716 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1717 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1718 UpdateWindow( menu->hWnd );
1719 WIN_ReleaseWndPtr(wndOwner);
1720 return TRUE;
1722 return FALSE;
1726 /***********************************************************************
1727 * MENU_SelectItem
1729 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1730 BOOL sendMenuSelect, HMENU topmenu )
1732 LPPOPUPMENU lppop;
1733 HDC hdc;
1735 TRACE("owner=0x%04x menu=0x%04x index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1737 lppop = MENU_GetMenu( hmenu );
1738 if ((!lppop) || (!lppop->nItems)) return;
1740 if (lppop->FocusedItem == wIndex) return;
1741 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1742 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1744 SelectObject( hdc, hMenuFont);
1746 /* Clear previous highlighted item */
1747 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1749 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1750 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1751 lppop->Height, !(lppop->wFlags & MF_POPUP),
1752 ODA_SELECT );
1755 /* Highlight new item (if any) */
1756 lppop->FocusedItem = wIndex;
1757 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1759 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1760 lppop->items[wIndex].fState |= MF_HILITE;
1761 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1762 &lppop->items[wIndex], lppop->Height,
1763 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1765 if (sendMenuSelect)
1767 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1768 SendMessageA( hwndOwner, WM_MENUSELECT,
1769 MAKELONG(ip->fType & MF_POPUP ? wIndex: ip->wID,
1770 ip->fType | ip->fState | MF_MOUSESELECT |
1771 (lppop->wFlags & MF_SYSMENU)), hmenu);
1774 else if (sendMenuSelect) {
1775 if(topmenu){
1776 int pos;
1777 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1778 POPUPMENU *ptm = (POPUPMENU *) USER_HEAP_LIN_ADDR( topmenu );
1779 MENUITEM *ip = &ptm->items[pos];
1780 SendMessageA( hwndOwner, WM_MENUSELECT, MAKELONG(pos,
1781 ip->fType | ip->fState | MF_MOUSESELECT |
1782 (ptm->wFlags & MF_SYSMENU)), topmenu);
1786 ReleaseDC( lppop->hWnd, hdc );
1790 /***********************************************************************
1791 * MENU_MoveSelection
1793 * Moves currently selected item according to the offset parameter.
1794 * If there is no selection then it should select the last item if
1795 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1797 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1799 INT i;
1800 POPUPMENU *menu;
1802 TRACE("hwnd=0x%04x hmenu=0x%04x off=0x%04x\n", hwndOwner, hmenu, offset);
1804 menu = MENU_GetMenu( hmenu );
1805 if ((!menu) || (!menu->items)) return;
1807 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1809 if( menu->nItems == 1 ) return; else
1810 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1811 ; i += offset)
1812 if (!(menu->items[i].fType & MF_SEPARATOR))
1814 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1815 return;
1819 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1820 i >= 0 && i < menu->nItems ; i += offset)
1821 if (!(menu->items[i].fType & MF_SEPARATOR))
1823 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1824 return;
1829 /**********************************************************************
1830 * MENU_SetItemData
1832 * Set an item flags, id and text ptr. Called by InsertMenu() and
1833 * ModifyMenu().
1835 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT id,
1836 LPCWSTR str )
1838 LPWSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1840 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1842 if (IS_STRING_ITEM(flags))
1844 if (!str || !*str)
1846 flags |= MF_SEPARATOR;
1847 item->text = NULL;
1849 else
1851 LPWSTR text;
1852 /* Item beginning with a backspace is a help item */
1853 if (*str == '\b')
1855 flags |= MF_HELP;
1856 str++;
1858 if (!(text = HEAP_strdupW( SystemHeap, 0, str ))) return FALSE;
1859 item->text = text;
1862 else if (IS_BITMAP_ITEM(flags))
1863 item->text = (LPWSTR)(HBITMAP)LOWORD(str);
1864 else item->text = NULL;
1866 if (flags & MF_OWNERDRAW)
1867 item->dwItemData = (DWORD)str;
1868 else
1869 item->dwItemData = 0;
1871 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1872 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1874 if (flags & MF_POPUP)
1876 POPUPMENU *menu = MENU_GetMenu((UINT16)id);
1877 if (menu) menu->wFlags |= MF_POPUP;
1878 else
1880 item->wID = 0;
1881 item->hSubMenu = 0;
1882 item->fType = 0;
1883 item->fState = 0;
1884 return FALSE;
1888 item->wID = id;
1889 if (flags & MF_POPUP)
1890 item->hSubMenu = id;
1892 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1893 flags |= MF_POPUP; /* keep popup */
1895 item->fType = flags & TYPE_MASK;
1896 item->fState = (flags & STATE_MASK) &
1897 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1900 /* Don't call SetRectEmpty here! */
1903 if (prevText) HeapFree( SystemHeap, 0, prevText );
1905 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1906 return TRUE;
1910 /**********************************************************************
1911 * MENU_InsertItem
1913 * Insert a new item into a menu.
1915 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1917 MENUITEM *newItems;
1918 POPUPMENU *menu;
1920 if (!(menu = MENU_GetMenu(hMenu)))
1921 return NULL;
1923 /* Find where to insert new item */
1925 if (flags & MF_BYPOSITION) {
1926 if (pos > menu->nItems)
1927 pos = menu->nItems;
1928 } else {
1929 if (!MENU_FindItem( &hMenu, &pos, flags ))
1930 pos = menu->nItems;
1931 else {
1932 if (!(menu = MENU_GetMenu( hMenu )))
1933 return NULL;
1937 /* Create new items array */
1939 newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1940 if (!newItems)
1942 WARN("allocation failed\n" );
1943 return NULL;
1945 if (menu->nItems > 0)
1947 /* Copy the old array into the new */
1948 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1949 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1950 (menu->nItems-pos)*sizeof(MENUITEM) );
1951 HeapFree( SystemHeap, 0, menu->items );
1953 menu->items = newItems;
1954 menu->nItems++;
1955 memset( &newItems[pos], 0, sizeof(*newItems) );
1956 menu->Height = 0; /* force size recalculate */
1957 return &newItems[pos];
1961 /**********************************************************************
1962 * MENU_ParseResource
1964 * Parse a standard menu resource and add items to the menu.
1965 * Return a pointer to the end of the resource.
1967 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1969 WORD flags, id = 0;
1970 LPCSTR str;
1974 flags = GET_WORD(res);
1975 res += sizeof(WORD);
1976 if (!(flags & MF_POPUP))
1978 id = GET_WORD(res);
1979 res += sizeof(WORD);
1981 if (!IS_STRING_ITEM(flags))
1982 ERR("not a string item %04x\n", flags );
1983 str = res;
1984 if (!unicode) res += strlen(str) + 1;
1985 else res += (strlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1986 if (flags & MF_POPUP)
1988 HMENU hSubMenu = CreatePopupMenu();
1989 if (!hSubMenu) return NULL;
1990 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1991 return NULL;
1992 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1993 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1995 else /* Not a popup */
1997 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1998 else AppendMenuW( hMenu, flags, id,
1999 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
2001 } while (!(flags & MF_END));
2002 return res;
2006 /**********************************************************************
2007 * MENUEX_ParseResource
2009 * Parse an extended menu resource and add items to the menu.
2010 * Return a pointer to the end of the resource.
2012 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
2014 WORD resinfo;
2015 do {
2016 MENUITEMINFOW mii;
2018 mii.cbSize = sizeof(mii);
2019 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
2020 mii.fType = GET_DWORD(res);
2021 res += sizeof(DWORD);
2022 mii.fState = GET_DWORD(res);
2023 res += sizeof(DWORD);
2024 mii.wID = GET_DWORD(res);
2025 res += sizeof(DWORD);
2026 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
2027 res += sizeof(WORD);
2028 /* Align the text on a word boundary. */
2029 res += (~((int)res - 1)) & 1;
2030 mii.dwTypeData = (LPWSTR) res;
2031 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
2032 /* Align the following fields on a dword boundary. */
2033 res += (~((int)res - 1)) & 3;
2035 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
2037 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
2038 0, mii.dwTypeData);
2039 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
2040 mii.fType, mii.fState, mii.wID, resinfo, newstr);
2041 HeapFree( GetProcessHeap(), 0, newstr );
2044 if (resinfo & 1) { /* Pop-up? */
2045 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
2046 res += sizeof(DWORD);
2047 mii.hSubMenu = CreatePopupMenu();
2048 if (!mii.hSubMenu)
2049 return NULL;
2050 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
2051 DestroyMenu(mii.hSubMenu);
2052 return NULL;
2054 mii.fMask |= MIIM_SUBMENU;
2055 mii.fType |= MF_POPUP;
2057 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2058 } while (!(resinfo & MF_END));
2059 return res;
2063 /***********************************************************************
2064 * MENU_GetSubPopup
2066 * Return the handle of the selected sub-popup menu (if any).
2068 static HMENU MENU_GetSubPopup( HMENU hmenu )
2070 POPUPMENU *menu;
2071 MENUITEM *item;
2073 menu = MENU_GetMenu( hmenu );
2075 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2077 item = &menu->items[menu->FocusedItem];
2078 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2079 return item->hSubMenu;
2080 return 0;
2084 /***********************************************************************
2085 * MENU_HideSubPopups
2087 * Hide the sub-popup menus of this menu.
2089 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2090 BOOL sendMenuSelect )
2092 POPUPMENU *menu = MENU_GetMenu( hmenu );
2094 TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2096 if (menu && uSubPWndLevel)
2098 HMENU hsubmenu;
2099 POPUPMENU *submenu;
2100 MENUITEM *item;
2102 if (menu->FocusedItem != NO_SELECTED_ITEM)
2104 item = &menu->items[menu->FocusedItem];
2105 if (!(item->fType & MF_POPUP) ||
2106 !(item->fState & MF_MOUSESELECT)) return;
2107 item->fState &= ~MF_MOUSESELECT;
2108 hsubmenu = item->hSubMenu;
2109 } else return;
2111 submenu = MENU_GetMenu( hsubmenu );
2112 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2113 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2115 if (submenu->hWnd == MENU_GetTopPopupWnd()->hwndSelf )
2117 ShowWindow( submenu->hWnd, SW_HIDE );
2118 uSubPWndLevel = 0;
2120 else
2122 DestroyWindow( submenu->hWnd );
2123 submenu->hWnd = 0;
2125 MENU_ReleaseTopPopupWnd();
2130 /***********************************************************************
2131 * MENU_ShowSubPopup
2133 * Display the sub-menu of the selected item of this menu.
2134 * Return the handle of the submenu, or hmenu if no submenu to display.
2136 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2137 BOOL selectFirst, UINT wFlags )
2139 RECT rect;
2140 POPUPMENU *menu;
2141 MENUITEM *item;
2142 WND *wndPtr;
2143 HDC hdc;
2145 TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, selectFirst);
2147 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2149 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd )) ||
2150 (menu->FocusedItem == NO_SELECTED_ITEM))
2152 WIN_ReleaseWndPtr(wndPtr);
2153 return hmenu;
2156 item = &menu->items[menu->FocusedItem];
2157 if (!(item->fType & MF_POPUP) ||
2158 (item->fState & (MF_GRAYED | MF_DISABLED)))
2160 WIN_ReleaseWndPtr(wndPtr);
2161 return hmenu;
2164 /* message must be send before using item,
2165 because nearly everything may by changed by the application ! */
2167 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2168 if (!(wFlags & TPM_NONOTIFY))
2169 SendMessageA( hwndOwner, WM_INITMENUPOPUP, item->hSubMenu,
2170 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2172 item = &menu->items[menu->FocusedItem];
2173 rect = item->rect;
2175 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
2176 if (!(item->fState & MF_HILITE))
2178 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2179 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2181 SelectObject( hdc, hMenuFont);
2183 item->fState |= MF_HILITE;
2184 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2185 ReleaseDC( menu->hWnd, hdc );
2187 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2188 item->rect = rect;
2190 item->fState |= MF_MOUSESELECT;
2192 if (IS_SYSTEM_MENU(menu))
2194 MENU_InitSysMenuPopup(item->hSubMenu, wndPtr->dwStyle, GetClassLongA(wndPtr->hwndSelf, GCL_STYLE));
2196 NC_GetSysPopupPos( wndPtr, &rect );
2197 rect.top = rect.bottom;
2198 rect.right = GetSystemMetrics(SM_CXSIZE);
2199 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2201 else
2203 if (menu->wFlags & MF_POPUP)
2205 rect.left = wndPtr->rectWindow.left + item->rect.right - GetSystemMetrics(SM_CXBORDER);
2206 rect.top = wndPtr->rectWindow.top + item->rect.top;
2207 rect.right = item->rect.left - item->rect.right + GetSystemMetrics(SM_CXBORDER);
2208 rect.bottom = item->rect.top - item->rect.bottom;
2210 else
2212 rect.left = wndPtr->rectWindow.left + item->rect.left;
2213 rect.top = wndPtr->rectWindow.top + item->rect.bottom;
2214 rect.right = item->rect.right - item->rect.left;
2215 rect.bottom = item->rect.bottom - item->rect.top;
2219 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2220 rect.left, rect.top, rect.right, rect.bottom );
2221 if (selectFirst)
2222 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2223 WIN_ReleaseWndPtr(wndPtr);
2224 return item->hSubMenu;
2229 /**********************************************************************
2230 * MENU_IsMenuActive
2232 BOOL MENU_IsMenuActive(void)
2234 return pTopPopupWnd && (pTopPopupWnd->dwStyle & WS_VISIBLE);
2237 /***********************************************************************
2238 * MENU_PtMenu
2240 * Walks menu chain trying to find a menu pt maps to.
2242 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2244 POPUPMENU *menu = MENU_GetMenu( hMenu );
2245 register UINT ht = menu->FocusedItem;
2247 /* try subpopup first (if any) */
2248 ht = (ht != NO_SELECTED_ITEM &&
2249 (menu->items[ht].fType & MF_POPUP) &&
2250 (menu->items[ht].fState & MF_MOUSESELECT))
2251 ? (UINT) MENU_PtMenu(menu->items[ht].hSubMenu, pt) : 0;
2253 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
2255 ht = (UINT)NC_HandleNCHitTest( menu->hWnd, pt );
2256 if( menu->wFlags & MF_POPUP )
2257 ht = (ht != (UINT)HTNOWHERE &&
2258 ht != (UINT)HTERROR) ? (UINT)hMenu : 0;
2259 else
2261 WND* wndPtr = WIN_FindWndPtr(menu->hWnd);
2263 ht = ( ht == HTSYSMENU ) ? (UINT)(wndPtr->hSysMenu)
2264 : ( ht == HTMENU ) ? (UINT)(wndPtr->wIDmenu) : 0;
2265 WIN_ReleaseWndPtr(wndPtr);
2268 return (HMENU)ht;
2271 /***********************************************************************
2272 * MENU_ExecFocusedItem
2274 * Execute a menu item (for instance when user pressed Enter).
2275 * Return the wID of the executed item. Otherwise, -1 indicating
2276 * that no menu item was executed;
2277 * Have to receive the flags for the TrackPopupMenu options to avoid
2278 * sending unwanted message.
2281 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2283 MENUITEM *item;
2284 POPUPMENU *menu = MENU_GetMenu( hMenu );
2286 TRACE("%p hmenu=0x%04x\n", pmt, hMenu);
2288 if (!menu || !menu->nItems ||
2289 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2291 item = &menu->items[menu->FocusedItem];
2293 TRACE("%08x %08x %08x\n",
2294 hMenu, item->wID, item->hSubMenu);
2296 if (!(item->fType & MF_POPUP))
2298 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2300 /* If TPM_RETURNCMD is set you return the id, but
2301 do not send a message to the owner */
2302 if(!(wFlags & TPM_RETURNCMD))
2304 if( menu->wFlags & MF_SYSMENU )
2305 PostMessageA( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2306 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2307 else
2308 PostMessageA( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2310 return item->wID;
2313 else
2314 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2316 return -1;
2319 /***********************************************************************
2320 * MENU_SwitchTracking
2322 * Helper function for menu navigation routines.
2324 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2326 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2327 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2329 TRACE("%p hmenu=0x%04x 0x%04x\n", pmt, hPtMenu, id);
2331 if( pmt->hTopMenu != hPtMenu &&
2332 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2334 /* both are top level menus (system and menu-bar) */
2335 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2336 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2337 pmt->hTopMenu = hPtMenu;
2339 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2340 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2344 /***********************************************************************
2345 * MENU_ButtonDown
2347 * Return TRUE if we can go on with menu tracking.
2349 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2351 TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2353 if (hPtMenu)
2355 UINT id = 0;
2356 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2357 MENUITEM *item;
2359 if( IS_SYSTEM_MENU(ptmenu) )
2360 item = ptmenu->items;
2361 else
2362 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2364 if( item )
2366 if( ptmenu->FocusedItem != id )
2367 MENU_SwitchTracking( pmt, hPtMenu, id );
2369 /* If the popup menu is not already "popped" */
2370 if(!(item->fState & MF_MOUSESELECT ))
2372 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2374 /* In win31, a newly popped menu always remain opened for the next buttonup */
2375 if(TWEAK_WineLook == WIN31_LOOK)
2376 ptmenu->bTimeToHide = FALSE;
2379 return TRUE;
2381 /* Else the click was on the menu bar, finish the tracking */
2383 return FALSE;
2386 /***********************************************************************
2387 * MENU_ButtonUp
2389 * Return the value of MENU_ExecFocusedItem if
2390 * the selected item was not a popup. Else open the popup.
2391 * A -1 return value indicates that we go on with menu tracking.
2394 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2396 TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2398 if (hPtMenu)
2400 UINT id = 0;
2401 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2402 MENUITEM *item;
2404 if( IS_SYSTEM_MENU(ptmenu) )
2405 item = ptmenu->items;
2406 else
2407 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2409 if( item && (ptmenu->FocusedItem == id ))
2411 if( !(item->fType & MF_POPUP) )
2412 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2414 /* If we are dealing with the top-level menu and that this */
2415 /* is a click on an already "poppped" item */
2416 /* Stop the menu tracking and close the opened submenus */
2417 if((pmt->hTopMenu == hPtMenu) && (ptmenu->bTimeToHide == TRUE))
2418 return 0;
2420 ptmenu->bTimeToHide = TRUE;
2422 return -1;
2426 /***********************************************************************
2427 * MENU_MouseMove
2429 * Return TRUE if we can go on with menu tracking.
2431 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2433 UINT id = NO_SELECTED_ITEM;
2434 POPUPMENU *ptmenu = NULL;
2436 if( hPtMenu )
2438 ptmenu = MENU_GetMenu( hPtMenu );
2439 if( IS_SYSTEM_MENU(ptmenu) )
2440 id = 0;
2441 else
2442 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2445 if( id == NO_SELECTED_ITEM )
2447 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2448 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2451 else if( ptmenu->FocusedItem != id )
2453 MENU_SwitchTracking( pmt, hPtMenu, id );
2454 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2456 return TRUE;
2460 /***********************************************************************
2461 * MENU_DoNextMenu
2463 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2465 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2467 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2469 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2470 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2472 WND* wndPtr;
2473 HMENU hNewMenu;
2474 HWND hNewWnd;
2475 UINT id = 0;
2476 LRESULT l = SendMessageA( pmt->hOwnerWnd, WM_NEXTMENU, vk,
2477 (IS_SYSTEM_MENU(menu)) ? GetSubMenu16(pmt->hTopMenu,0) : pmt->hTopMenu );
2479 TRACE("%04x [%04x] -> %04x [%04x]\n",
2480 (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2482 if( l == 0 )
2484 wndPtr = WIN_FindWndPtr(pmt->hOwnerWnd);
2486 hNewWnd = pmt->hOwnerWnd;
2487 if( IS_SYSTEM_MENU(menu) )
2489 /* switch to the menu bar */
2491 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu )
2493 WIN_ReleaseWndPtr(wndPtr);
2494 return FALSE;
2497 hNewMenu = wndPtr->wIDmenu;
2498 if( vk == VK_LEFT )
2500 menu = MENU_GetMenu( hNewMenu );
2501 id = menu->nItems - 1;
2504 else if( wndPtr->dwStyle & WS_SYSMENU )
2506 /* switch to the system menu */
2507 hNewMenu = wndPtr->hSysMenu;
2509 else
2511 WIN_ReleaseWndPtr(wndPtr);
2512 return FALSE;
2514 WIN_ReleaseWndPtr(wndPtr);
2516 else /* application returned a new menu to switch to */
2518 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2520 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2522 wndPtr = WIN_FindWndPtr(hNewWnd);
2524 if( wndPtr->dwStyle & WS_SYSMENU &&
2525 GetSubMenu16(wndPtr->hSysMenu, 0) == hNewMenu )
2527 /* get the real system menu */
2528 hNewMenu = wndPtr->hSysMenu;
2530 else if( wndPtr->dwStyle & WS_CHILD || wndPtr->wIDmenu != hNewMenu )
2532 /* FIXME: Not sure what to do here, perhaps,
2533 * try to track hNewMenu as a popup? */
2535 TRACE(" -- got confused.\n");
2536 WIN_ReleaseWndPtr(wndPtr);
2537 return FALSE;
2539 WIN_ReleaseWndPtr(wndPtr);
2541 else return FALSE;
2544 if( hNewMenu != pmt->hTopMenu )
2546 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2547 FALSE, 0 );
2548 if( pmt->hCurrentMenu != pmt->hTopMenu )
2549 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2552 if( hNewWnd != pmt->hOwnerWnd )
2554 ReleaseCapture();
2555 pmt->hOwnerWnd = hNewWnd;
2556 EVENT_Capture( pmt->hOwnerWnd, HTMENU );
2559 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2560 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2562 return TRUE;
2564 return FALSE;
2567 /***********************************************************************
2568 * MENU_SuspendPopup
2570 * The idea is not to show the popup if the next input message is
2571 * going to hide it anyway.
2573 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2575 MSG msg;
2577 msg.hwnd = pmt->hOwnerWnd;
2579 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2580 pmt->trackFlags |= TF_SKIPREMOVE;
2582 switch( uMsg )
2584 case WM_KEYDOWN:
2585 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2586 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2588 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2589 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2590 if( msg.message == WM_KEYDOWN &&
2591 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2593 pmt->trackFlags |= TF_SUSPENDPOPUP;
2594 return TRUE;
2597 break;
2600 /* failures go through this */
2601 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2602 return FALSE;
2605 /***********************************************************************
2606 * MENU_KeyLeft
2608 * Handle a VK_LEFT key event in a menu.
2610 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2612 POPUPMENU *menu;
2613 HMENU hmenutmp, hmenuprev;
2614 UINT prevcol;
2616 hmenuprev = hmenutmp = pmt->hTopMenu;
2617 menu = MENU_GetMenu( hmenutmp );
2619 /* Try to move 1 column left (if possible) */
2620 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2621 NO_SELECTED_ITEM ) {
2623 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2624 prevcol, TRUE, 0 );
2625 return;
2628 /* close topmost popup */
2629 while (hmenutmp != pmt->hCurrentMenu)
2631 hmenuprev = hmenutmp;
2632 hmenutmp = MENU_GetSubPopup( hmenuprev );
2635 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2636 pmt->hCurrentMenu = hmenuprev;
2638 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2640 /* move menu bar selection if no more popups are left */
2642 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2643 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2645 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2647 /* A sublevel menu was displayed - display the next one
2648 * unless there is another displacement coming up */
2650 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2651 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2652 pmt->hTopMenu, TRUE, wFlags);
2658 /***********************************************************************
2659 * MENU_KeyRight
2661 * Handle a VK_RIGHT key event in a menu.
2663 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2665 HMENU hmenutmp;
2666 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2667 UINT nextcol;
2669 TRACE("MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2670 pmt->hCurrentMenu,
2671 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->
2672 items[0].text),
2673 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2675 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2677 /* If already displaying a popup, try to display sub-popup */
2679 hmenutmp = pmt->hCurrentMenu;
2680 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2682 /* if subpopup was displayed then we are done */
2683 if (hmenutmp != pmt->hCurrentMenu) return;
2686 /* Check to see if there's another column */
2687 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2688 NO_SELECTED_ITEM ) {
2689 TRACE("Going to %d.\n", nextcol );
2690 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2691 nextcol, TRUE, 0 );
2692 return;
2695 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2697 if( pmt->hCurrentMenu != pmt->hTopMenu )
2699 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2700 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2701 } else hmenutmp = 0;
2703 /* try to move to the next item */
2704 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2705 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2707 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2708 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2709 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2710 pmt->hTopMenu, TRUE, wFlags);
2714 /***********************************************************************
2715 * MENU_TrackMenu
2717 * Menu tracking code.
2719 static INT MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
2720 HWND hwnd, const RECT *lprect )
2722 MSG msg;
2723 POPUPMENU *menu;
2724 BOOL fRemove;
2725 INT executedMenuId = -1;
2726 MTRACKER mt;
2727 BOOL enterIdleSent = FALSE;
2729 mt.trackFlags = 0;
2730 mt.hCurrentMenu = hmenu;
2731 mt.hTopMenu = hmenu;
2732 mt.hOwnerWnd = hwnd;
2733 mt.pt.x = x;
2734 mt.pt.y = y;
2736 TRACE("hmenu=0x%04x flags=0x%08x (%d,%d) hwnd=0x%04x (%d,%d)-(%d,%d)\n",
2737 hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2738 (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2740 fEndMenu = FALSE;
2741 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
2743 if (wFlags & TPM_BUTTONDOWN)
2745 /* Get the result in order to start the tracking or not */
2746 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2747 fEndMenu = !fRemove;
2750 EVENT_Capture( mt.hOwnerWnd, HTMENU );
2752 while (!fEndMenu)
2754 menu = MENU_GetMenu( mt.hCurrentMenu );
2755 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2757 /* we have to keep the message in the queue until it's
2758 * clear that menu loop is not over yet. */
2760 if (!MSG_InternalGetMessage( QMSG_WIN32A, &msg, msg.hwnd, mt.hOwnerWnd,
2761 MSGF_MENU, PM_NOREMOVE, !enterIdleSent, &enterIdleSent )) break;
2763 /* check if EndMenu() tried to cancel us, by posting this message */
2764 if(msg.message == WM_CANCELMODE)
2766 /* we are now out of the loop */
2767 fEndMenu = TRUE;
2769 /* remove the message from the queue */
2770 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
2772 /* break out of internal loop, ala ESCAPE */
2773 break;
2776 TranslateMessage( &msg );
2777 mt.pt = msg.pt;
2779 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2780 enterIdleSent=FALSE;
2782 fRemove = FALSE;
2783 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2785 /* Find a menu for this mouse event */
2786 hmenu = MENU_PtMenu( mt.hTopMenu, msg.pt );
2788 switch(msg.message)
2790 /* no WM_NC... messages in captured state */
2792 case WM_RBUTTONDBLCLK:
2793 case WM_RBUTTONDOWN:
2794 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2795 /* fall through */
2796 case WM_LBUTTONDBLCLK:
2797 case WM_LBUTTONDOWN:
2798 /* If the message belongs to the menu, removes it from the queue */
2799 /* Else, end menu tracking */
2800 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2801 fEndMenu = !fRemove;
2802 break;
2804 case WM_RBUTTONUP:
2805 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2806 /* fall through */
2807 case WM_LBUTTONUP:
2808 /* Check if a menu was selected by the mouse */
2809 if (hmenu)
2811 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2813 /* End the loop if executedMenuId is an item ID */
2814 /* or if the job was done (executedMenuId = 0). */
2815 fEndMenu = fRemove = (executedMenuId != -1);
2817 /* No menu was selected by the mouse */
2818 /* if the function was called by TrackPopupMenu, continue
2819 with the menu tracking. If not, stop it */
2820 else
2821 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2823 break;
2825 case WM_MOUSEMOVE:
2826 /* In win95 winelook, the selected menu item must be changed every time the
2827 mouse moves. In Win31 winelook, the mouse button has to be held down */
2829 if ( (TWEAK_WineLook > WIN31_LOOK) ||
2830 ( (msg.wParam & MK_LBUTTON) ||
2831 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2833 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2835 } /* switch(msg.message) - mouse */
2837 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2839 fRemove = TRUE; /* Keyboard messages are always removed */
2840 switch(msg.message)
2842 case WM_KEYDOWN:
2843 switch(msg.wParam)
2845 case VK_HOME:
2846 case VK_END:
2847 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2848 NO_SELECTED_ITEM, FALSE, 0 );
2849 /* fall through */
2850 case VK_UP:
2851 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2852 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2853 break;
2855 case VK_DOWN: /* If on menu bar, pull-down the menu */
2857 menu = MENU_GetMenu( mt.hCurrentMenu );
2858 if (!(menu->wFlags & MF_POPUP))
2859 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
2860 else /* otherwise try to move selection */
2861 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2862 break;
2864 case VK_LEFT:
2865 MENU_KeyLeft( &mt, wFlags );
2866 break;
2868 case VK_RIGHT:
2869 MENU_KeyRight( &mt, wFlags );
2870 break;
2872 case VK_ESCAPE:
2873 fEndMenu = TRUE;
2874 break;
2876 case VK_F1:
2878 HELPINFO hi;
2879 hi.cbSize = sizeof(HELPINFO);
2880 hi.iContextType = HELPINFO_MENUITEM;
2881 if (menu->FocusedItem == NO_SELECTED_ITEM)
2882 hi.iCtrlId = 0;
2883 else
2884 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2885 hi.hItemHandle = hmenu;
2886 hi.dwContextId = menu->dwContextHelpID;
2887 hi.MousePos = msg.pt;
2888 SendMessageA(hwnd, WM_HELP, 0, (LPARAM)&hi);
2889 break;
2892 default:
2893 break;
2895 break; /* WM_KEYDOWN */
2897 case WM_SYSKEYDOWN:
2898 switch(msg.wParam)
2900 case VK_MENU:
2901 fEndMenu = TRUE;
2902 break;
2905 break; /* WM_SYSKEYDOWN */
2907 case WM_CHAR:
2909 UINT pos;
2911 if (msg.wParam == '\r' || msg.wParam == ' ')
2913 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2914 fEndMenu = (executedMenuId != -1);
2916 break;
2919 /* Hack to avoid control chars. */
2920 /* We will find a better way real soon... */
2921 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2923 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2924 LOWORD(msg.wParam), FALSE );
2925 if (pos == (UINT)-2) fEndMenu = TRUE;
2926 else if (pos == (UINT)-1) MessageBeep(0);
2927 else
2929 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
2930 TRUE, 0 );
2931 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2932 fEndMenu = (executedMenuId != -1);
2935 break;
2936 } /* switch(msg.message) - kbd */
2938 else
2940 DispatchMessageA( &msg );
2943 if (!fEndMenu) fRemove = TRUE;
2945 /* finally remove message from the queue */
2947 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2948 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
2949 else mt.trackFlags &= ~TF_SKIPREMOVE;
2952 ReleaseCapture();
2954 /* If dropdown is still painted and the close box is clicked on
2955 then the menu will be destroyed as part of the DispatchMessage above.
2956 This will then invalidate the menu handle in mt.hTopMenu. We should
2957 check for this first. */
2958 if( IsMenu( mt.hTopMenu ) )
2960 menu = MENU_GetMenu( mt.hTopMenu );
2962 if( IsWindow( mt.hOwnerWnd ) )
2964 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2966 if (menu && menu->wFlags & MF_POPUP)
2968 ShowWindow( menu->hWnd, SW_HIDE );
2969 uSubPWndLevel = 0;
2971 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2972 SendMessageA( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
2975 /* Reset the variable for hiding menu */
2976 if( menu ) menu->bTimeToHide = FALSE;
2979 /* The return value is only used by TrackPopupMenu */
2980 return ((executedMenuId != -1) ? executedMenuId : 0);
2983 /***********************************************************************
2984 * MENU_InitTracking
2986 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
2988 TRACE("hwnd=0x%04x hmenu=0x%04x\n", hWnd, hMenu);
2990 HideCaret(0);
2992 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
2993 if (!(wFlags & TPM_NONOTIFY))
2994 SendMessageA( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
2996 SendMessageA( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2998 if (!(wFlags & TPM_NONOTIFY))
2999 SendMessageA( hWnd, WM_INITMENU, hMenu, 0 );
3001 return TRUE;
3003 /***********************************************************************
3004 * MENU_ExitTracking
3006 static BOOL MENU_ExitTracking(HWND hWnd)
3008 TRACE("hwnd=0x%04x\n", hWnd);
3010 SendMessageA( hWnd, WM_EXITMENULOOP, 0, 0 );
3011 ShowCaret(0);
3012 return TRUE;
3015 /***********************************************************************
3016 * MENU_TrackMouseMenuBar
3018 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3020 void MENU_TrackMouseMenuBar( WND* wndPtr, INT ht, POINT pt )
3022 HWND hWnd = wndPtr->hwndSelf;
3023 HMENU hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
3024 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3026 TRACE("pwnd=%p ht=0x%04x (%ld,%ld)\n", wndPtr, ht, pt.x, pt.y);
3028 if (IsMenu(hMenu))
3030 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3031 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3032 MENU_ExitTracking(hWnd);
3037 /***********************************************************************
3038 * MENU_TrackKbdMenuBar
3040 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3042 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT wParam, INT vkey)
3044 UINT uItem = NO_SELECTED_ITEM;
3045 HMENU hTrackMenu;
3046 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3048 /* find window that has a menu */
3050 while( wndPtr->dwStyle & WS_CHILD)
3051 if( !(wndPtr = wndPtr->parent) ) return;
3053 /* check if we have to track a system menu */
3055 if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) ||
3056 !wndPtr->wIDmenu || vkey == VK_SPACE )
3058 if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
3059 hTrackMenu = wndPtr->hSysMenu;
3060 uItem = 0;
3061 wParam |= HTSYSMENU; /* prevent item lookup */
3063 else
3064 hTrackMenu = wndPtr->wIDmenu;
3066 if (IsMenu( hTrackMenu ))
3068 MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu, FALSE, wFlags );
3070 if( vkey && vkey != VK_SPACE )
3072 uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu,
3073 vkey, (wParam & HTSYSMENU) );
3074 if( uItem >= (UINT)(-2) )
3076 if( uItem == (UINT)(-1) ) MessageBeep(0);
3077 hTrackMenu = 0;
3081 if( hTrackMenu )
3083 MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE, 0 );
3085 if( uItem == NO_SELECTED_ITEM )
3086 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
3087 else if( vkey )
3088 PostMessageA( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
3090 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, wndPtr->hwndSelf, NULL );
3093 MENU_ExitTracking (wndPtr->hwndSelf);
3098 /**********************************************************************
3099 * TrackPopupMenu16 (USER.416)
3101 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
3102 INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
3104 RECT r;
3105 if (lpRect)
3106 CONV_RECT16TO32( lpRect, &r );
3107 return TrackPopupMenu( hMenu, wFlags, x, y, nReserved, hWnd,
3108 lpRect ? &r : NULL );
3112 /**********************************************************************
3113 * TrackPopupMenu (USER32.549)
3115 * Like the win32 API, the function return the command ID only if the
3116 * flag TPM_RETURNCMD is on.
3119 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3120 INT nReserved, HWND hWnd, const RECT *lpRect )
3122 BOOL ret = FALSE;
3124 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3126 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3127 if (!(wFlags & TPM_NONOTIFY))
3128 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3130 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3131 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
3132 MENU_ExitTracking(hWnd);
3134 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3135 ret = 1;
3137 return ret;
3140 /**********************************************************************
3141 * TrackPopupMenuEx (USER32.550)
3143 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3144 HWND hWnd, LPTPMPARAMS lpTpm )
3146 FIXME("not fully implemented\n" );
3147 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3148 lpTpm ? &lpTpm->rcExclude : NULL );
3151 /***********************************************************************
3152 * PopupMenuWndProc
3154 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3156 LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3157 LPARAM lParam )
3159 WND* wndPtr = WIN_FindWndPtr(hwnd);
3160 LRESULT retvalue;
3162 TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3163 hwnd, message, wParam, lParam);
3165 switch(message)
3167 case WM_CREATE:
3169 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3170 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3171 retvalue = 0;
3172 goto END;
3175 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3176 retvalue = MA_NOACTIVATE;
3177 goto END;
3179 case WM_PAINT:
3181 PAINTSTRUCT ps;
3182 BeginPaint( hwnd, &ps );
3183 MENU_DrawPopupMenu( hwnd, ps.hdc,
3184 (HMENU)GetWindowLongA( hwnd, 0 ) );
3185 EndPaint( hwnd, &ps );
3186 retvalue = 0;
3187 goto END;
3189 case WM_ERASEBKGND:
3190 retvalue = 1;
3191 goto END;
3193 case WM_DESTROY:
3195 /* zero out global pointer in case resident popup window
3196 * was somehow destroyed. */
3198 if(MENU_GetTopPopupWnd() )
3200 if( hwnd == pTopPopupWnd->hwndSelf )
3202 ERR("resident popup destroyed!\n");
3204 MENU_DestroyTopPopupWnd();
3205 uSubPWndLevel = 0;
3207 else
3208 uSubPWndLevel--;
3209 MENU_ReleaseTopPopupWnd();
3211 break;
3213 case WM_SHOWWINDOW:
3215 if( wParam )
3217 if( !(*(HMENU*)wndPtr->wExtra) )
3218 ERR("no menu to display\n");
3220 else
3221 *(HMENU*)wndPtr->wExtra = 0;
3222 break;
3224 case MM_SETMENUHANDLE:
3226 *(HMENU*)wndPtr->wExtra = (HMENU)wParam;
3227 break;
3229 case MM_GETMENUHANDLE:
3231 retvalue = *(HMENU*)wndPtr->wExtra;
3232 goto END;
3234 default:
3235 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3236 goto END;
3238 retvalue = 0;
3239 END:
3240 WIN_ReleaseWndPtr(wndPtr);
3241 return retvalue;
3245 /***********************************************************************
3246 * MENU_GetMenuBarHeight
3248 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3250 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3251 INT orgX, INT orgY )
3253 HDC hdc;
3254 RECT rectBar;
3255 WND *wndPtr;
3256 LPPOPUPMENU lppop;
3257 UINT retvalue;
3259 TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3260 hwnd, menubarWidth, orgX, orgY );
3262 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
3263 return 0;
3265 if (!(lppop = MENU_GetMenu((HMENU16)wndPtr->wIDmenu)))
3267 WIN_ReleaseWndPtr(wndPtr);
3268 return 0;
3271 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3272 SelectObject( hdc, hMenuFont);
3273 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3274 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3275 ReleaseDC( hwnd, hdc );
3276 retvalue = lppop->Height;
3277 WIN_ReleaseWndPtr(wndPtr);
3278 return retvalue;
3282 /*******************************************************************
3283 * ChangeMenu16 (USER.153)
3285 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
3286 UINT16 id, UINT16 flags )
3288 TRACE("menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
3289 hMenu, pos, (DWORD)data, id, flags );
3290 if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
3291 id, data );
3293 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
3294 /* for MF_DELETE. We should check the parameters for all others */
3295 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
3297 if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
3298 if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
3299 id, data );
3300 if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
3301 flags & MF_BYPOSITION ? pos : id,
3302 flags & ~MF_REMOVE );
3303 /* Default: MF_INSERT */
3304 return InsertMenu16( hMenu, pos, flags, id, data );
3308 /*******************************************************************
3309 * ChangeMenuA (USER32.23)
3311 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3312 UINT id, UINT flags )
3314 TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3315 hMenu, pos, (DWORD)data, id, flags );
3316 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3317 id, data );
3318 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3319 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3320 id, data );
3321 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3322 flags & MF_BYPOSITION ? pos : id,
3323 flags & ~MF_REMOVE );
3324 /* Default: MF_INSERT */
3325 return InsertMenuA( hMenu, pos, flags, id, data );
3329 /*******************************************************************
3330 * ChangeMenuW (USER32.24)
3332 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3333 UINT id, UINT flags )
3335 TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3336 hMenu, pos, (DWORD)data, id, flags );
3337 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3338 id, data );
3339 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3340 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3341 id, data );
3342 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3343 flags & MF_BYPOSITION ? pos : id,
3344 flags & ~MF_REMOVE );
3345 /* Default: MF_INSERT */
3346 return InsertMenuW( hMenu, pos, flags, id, data );
3350 /*******************************************************************
3351 * CheckMenuItem16 (USER.154)
3353 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
3355 return (BOOL16)CheckMenuItem( hMenu, id, flags );
3359 /*******************************************************************
3360 * CheckMenuItem (USER32.46)
3362 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3364 MENUITEM *item;
3365 DWORD ret;
3367 TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3368 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3369 ret = item->fState & MF_CHECKED;
3370 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3371 else item->fState &= ~MF_CHECKED;
3372 return ret;
3376 /**********************************************************************
3377 * EnableMenuItem16 (USER.155)
3379 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3381 return EnableMenuItem( hMenu, wItemID, wFlags );
3385 /**********************************************************************
3386 * EnableMenuItem (USER32.170)
3388 UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3390 UINT oldflags;
3391 MENUITEM *item;
3392 POPUPMENU *menu;
3394 TRACE("(%04x, %04X, %04X) !\n",
3395 hMenu, wItemID, wFlags);
3397 /* Get the Popupmenu to access the owner menu */
3398 if (!(menu = MENU_GetMenu(hMenu)))
3399 return (UINT)-1;
3401 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3402 return (UINT)-1;
3404 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3405 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3407 /* In win95 if the close item in the system menu change update the close button */
3408 if (TWEAK_WineLook == WIN95_LOOK)
3409 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3411 if (menu->hSysMenuOwner != 0)
3413 POPUPMENU* parentMenu;
3415 /* Get the parent menu to access*/
3416 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3417 return (UINT)-1;
3419 /* Refresh the frame to reflect the change*/
3420 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3421 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3425 return oldflags;
3429 /*******************************************************************
3430 * GetMenuString16 (USER.161)
3432 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
3433 LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
3435 return GetMenuStringA( hMenu, wItemID, str, nMaxSiz, wFlags );
3439 /*******************************************************************
3440 * GetMenuStringA (USER32.268)
3442 INT WINAPI GetMenuStringA(
3443 HMENU hMenu, /* [in] menuhandle */
3444 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3445 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3446 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3447 UINT wFlags /* [in] MF_ flags */
3449 MENUITEM *item;
3451 TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3452 hMenu, wItemID, str, nMaxSiz, wFlags );
3453 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3454 if (!IS_STRING_ITEM(item->fType)) return 0;
3455 if (!str || !nMaxSiz) return strlenW(item->text);
3456 str[0] = '\0';
3457 lstrcpynWtoA( str, item->text, nMaxSiz );
3458 TRACE("returning '%s'\n", str );
3459 return strlen(str);
3463 /*******************************************************************
3464 * GetMenuStringW (USER32.269)
3466 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3467 LPWSTR str, INT nMaxSiz, UINT wFlags )
3469 MENUITEM *item;
3471 TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3472 hMenu, wItemID, str, nMaxSiz, wFlags );
3473 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3474 if (!IS_STRING_ITEM(item->fType)) return 0;
3475 if (!str || !nMaxSiz) return strlenW(item->text);
3476 str[0] = '\0';
3477 lstrcpynW( str, item->text, nMaxSiz );
3478 return strlenW(str);
3482 /**********************************************************************
3483 * HiliteMenuItem16 (USER.162)
3485 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
3486 UINT16 wHilite )
3488 return HiliteMenuItem( hWnd, hMenu, wItemID, wHilite );
3492 /**********************************************************************
3493 * HiliteMenuItem (USER32.318)
3495 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3496 UINT wHilite )
3498 LPPOPUPMENU menu;
3499 TRACE("(%04x, %04x, %04x, %04x);\n",
3500 hWnd, hMenu, wItemID, wHilite);
3501 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3502 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3503 if (menu->FocusedItem == wItemID) return TRUE;
3504 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3505 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3506 return TRUE;
3510 /**********************************************************************
3511 * GetMenuState16 (USER.250)
3513 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3515 return GetMenuState( hMenu, wItemID, wFlags );
3519 /**********************************************************************
3520 * GetMenuState (USER32.267)
3522 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3524 MENUITEM *item;
3525 TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3526 hMenu, wItemID, wFlags);
3527 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3528 debug_print_menuitem (" item: ", item, "");
3529 if (item->fType & MF_POPUP)
3531 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3532 if (!menu) return -1;
3533 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3535 else
3537 /* We used to (from way back then) mask the result to 0xff. */
3538 /* I don't know why and it seems wrong as the documented */
3539 /* return flag MF_SEPARATOR is outside that mask. */
3540 return (item->fType | item->fState);
3545 /**********************************************************************
3546 * GetMenuItemCount16 (USER.263)
3548 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
3550 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3551 if (!menu) return -1;
3552 TRACE("(%04x) returning %d\n",
3553 hMenu, menu->nItems );
3554 return menu->nItems;
3558 /**********************************************************************
3559 * GetMenuItemCount (USER32.262)
3561 INT WINAPI GetMenuItemCount( HMENU hMenu )
3563 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3564 if (!menu) return -1;
3565 TRACE("(%04x) returning %d\n",
3566 hMenu, menu->nItems );
3567 return menu->nItems;
3570 /**********************************************************************
3571 * GetMenuItemID16 (USER.264)
3573 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
3575 return (UINT16) GetMenuItemID (hMenu, nPos);
3578 /**********************************************************************
3579 * GetMenuItemID (USER32.263)
3581 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3583 MENUITEM * lpmi;
3585 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return 0;
3586 if (lpmi->fType & MF_POPUP) return -1;
3587 return lpmi->wID;
3591 /*******************************************************************
3592 * InsertMenu16 (USER.410)
3594 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3595 UINT16 id, SEGPTR data )
3597 UINT pos32 = (UINT)pos;
3598 if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT)-1;
3599 if (IS_STRING_ITEM(flags) && data)
3600 return InsertMenuA( hMenu, pos32, flags, id,
3601 (LPSTR)PTR_SEG_TO_LIN(data) );
3602 return InsertMenuA( hMenu, pos32, flags, id, (LPSTR)data );
3606 /*******************************************************************
3607 * InsertMenuW (USER32.325)
3609 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3610 UINT id, LPCWSTR str )
3612 MENUITEM *item;
3614 if (IS_STRING_ITEM(flags) && str)
3615 TRACE("hMenu %04x, pos %d, flags %08x, "
3616 "id %04x, str '%s'\n",
3617 hMenu, pos, flags, id, debugstr_w(str) );
3618 else TRACE("hMenu %04x, pos %d, flags %08x, "
3619 "id %04x, str %08lx (not a string)\n",
3620 hMenu, pos, flags, id, (DWORD)str );
3622 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3624 if (!(MENU_SetItemData( item, flags, id, str )))
3626 RemoveMenu( hMenu, pos, flags );
3627 return FALSE;
3630 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3631 (MENU_GetMenu((HMENU16)id))->wFlags |= MF_POPUP;
3633 item->hCheckBit = item->hUnCheckBit = 0;
3634 return TRUE;
3638 /*******************************************************************
3639 * InsertMenuA (USER32.322)
3641 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3642 UINT id, LPCSTR str )
3644 BOOL ret;
3646 if (IS_STRING_ITEM(flags) && str)
3648 LPWSTR newstr = HEAP_strdupAtoW( GetProcessHeap(), 0, str );
3649 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3650 HeapFree( GetProcessHeap(), 0, newstr );
3651 return ret;
3653 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3657 /*******************************************************************
3658 * AppendMenu16 (USER.411)
3660 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3662 return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3666 /*******************************************************************
3667 * AppendMenuA (USER32.5)
3669 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3670 UINT id, LPCSTR data )
3672 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3676 /*******************************************************************
3677 * AppendMenuW (USER32.6)
3679 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3680 UINT id, LPCWSTR data )
3682 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3686 /**********************************************************************
3687 * RemoveMenu16 (USER.412)
3689 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3691 return RemoveMenu( hMenu, nPos, wFlags );
3695 /**********************************************************************
3696 * RemoveMenu (USER32.441)
3698 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3700 LPPOPUPMENU menu;
3701 MENUITEM *item;
3703 TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3704 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3705 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3707 /* Remove item */
3709 MENU_FreeItemData( item );
3711 if (--menu->nItems == 0)
3713 HeapFree( SystemHeap, 0, menu->items );
3714 menu->items = NULL;
3716 else
3718 while(nPos < menu->nItems)
3720 *item = *(item+1);
3721 item++;
3722 nPos++;
3724 menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3725 menu->nItems * sizeof(MENUITEM) );
3727 return TRUE;
3731 /**********************************************************************
3732 * DeleteMenu16 (USER.413)
3734 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3736 return DeleteMenu( hMenu, nPos, wFlags );
3740 /**********************************************************************
3741 * DeleteMenu (USER32.129)
3743 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3745 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3746 if (!item) return FALSE;
3747 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3748 /* nPos is now the position of the item */
3749 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3750 return TRUE;
3754 /*******************************************************************
3755 * ModifyMenu16 (USER.414)
3757 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3758 UINT16 id, SEGPTR data )
3760 if (IS_STRING_ITEM(flags))
3761 return ModifyMenuA( hMenu, pos, flags, id,
3762 (LPSTR)PTR_SEG_TO_LIN(data) );
3763 return ModifyMenuA( hMenu, pos, flags, id, (LPSTR)data );
3767 /*******************************************************************
3768 * ModifyMenuW (USER32.398)
3770 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3771 UINT id, LPCWSTR str )
3773 MENUITEM *item;
3775 if (IS_STRING_ITEM(flags))
3777 TRACE("%04x %d %04x %04x '%s'\n",
3778 hMenu, pos, flags, id, str ? debugstr_w(str) : "#NULL#" );
3779 if (!str) return FALSE;
3781 else
3783 TRACE("%04x %d %04x %04x %08lx\n",
3784 hMenu, pos, flags, id, (DWORD)str );
3787 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3788 return MENU_SetItemData( item, flags, id, str );
3792 /*******************************************************************
3793 * ModifyMenuA (USER32.397)
3795 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3796 UINT id, LPCSTR str )
3798 BOOL ret;
3800 if (IS_STRING_ITEM(flags) && str)
3802 LPWSTR newstr = HEAP_strdupAtoW( GetProcessHeap(), 0, str );
3803 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
3804 HeapFree( GetProcessHeap(), 0, newstr );
3805 return ret;
3807 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3811 /**********************************************************************
3812 * CreatePopupMenu16 (USER.415)
3814 HMENU16 WINAPI CreatePopupMenu16(void)
3816 return CreatePopupMenu();
3820 /**********************************************************************
3821 * CreatePopupMenu (USER32.82)
3823 HMENU WINAPI CreatePopupMenu(void)
3825 HMENU hmenu;
3826 POPUPMENU *menu;
3828 if (!(hmenu = CreateMenu())) return 0;
3829 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3830 menu->wFlags |= MF_POPUP;
3831 menu->bTimeToHide = FALSE;
3832 return hmenu;
3836 /**********************************************************************
3837 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3839 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3841 return MAKELONG( check_bitmap_width, check_bitmap_height );
3845 /**********************************************************************
3846 * SetMenuItemBitmaps16 (USER.418)
3848 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3849 HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3851 return SetMenuItemBitmaps( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3855 /**********************************************************************
3856 * SetMenuItemBitmaps (USER32.490)
3858 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
3859 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3861 MENUITEM *item;
3862 TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3863 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3864 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3866 if (!hNewCheck && !hNewUnCheck)
3868 item->fState &= ~MF_USECHECKBITMAPS;
3870 else /* Install new bitmaps */
3872 item->hCheckBit = hNewCheck;
3873 item->hUnCheckBit = hNewUnCheck;
3874 item->fState |= MF_USECHECKBITMAPS;
3876 return TRUE;
3880 /**********************************************************************
3881 * CreateMenu16 (USER.151)
3883 HMENU16 WINAPI CreateMenu16(void)
3885 return CreateMenu();
3889 /**********************************************************************
3890 * CreateMenu (USER32.81)
3892 HMENU WINAPI CreateMenu(void)
3894 HMENU hMenu;
3895 LPPOPUPMENU menu;
3896 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3897 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3899 ZeroMemory(menu, sizeof(POPUPMENU));
3900 menu->wMagic = MENU_MAGIC;
3901 menu->FocusedItem = NO_SELECTED_ITEM;
3902 menu->bTimeToHide = FALSE;
3904 TRACE("return %04x\n", hMenu );
3906 return hMenu;
3910 /**********************************************************************
3911 * DestroyMenu16 (USER.152)
3913 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3915 return DestroyMenu( hMenu );
3919 /**********************************************************************
3920 * DestroyMenu (USER32.134)
3922 BOOL WINAPI DestroyMenu( HMENU hMenu )
3924 TRACE("(%04x)\n", hMenu);
3926 /* Silently ignore attempts to destroy default system popup */
3928 if (hMenu && hMenu != MENU_DefSysPopup)
3930 LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3931 WND *pTPWnd = MENU_GetTopPopupWnd();
3933 if( pTPWnd && (hMenu == *(HMENU*)pTPWnd->wExtra) )
3934 *(UINT*)pTPWnd->wExtra = 0;
3936 if (!IS_A_MENU(lppop)) lppop = NULL;
3937 if ( lppop )
3939 lppop->wMagic = 0; /* Mark it as destroyed */
3941 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3942 (!pTPWnd || (lppop->hWnd != pTPWnd->hwndSelf)))
3943 DestroyWindow( lppop->hWnd );
3945 if (lppop->items) /* recursively destroy submenus */
3947 int i;
3948 MENUITEM *item = lppop->items;
3949 for (i = lppop->nItems; i > 0; i--, item++)
3951 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3952 MENU_FreeItemData( item );
3954 HeapFree( SystemHeap, 0, lppop->items );
3956 USER_HEAP_FREE( hMenu );
3957 MENU_ReleaseTopPopupWnd();
3959 else
3961 MENU_ReleaseTopPopupWnd();
3962 return FALSE;
3965 return (hMenu != MENU_DefSysPopup);
3969 /**********************************************************************
3970 * GetSystemMenu16 (USER.156)
3972 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3974 return GetSystemMenu( hWnd, bRevert );
3978 /**********************************************************************
3979 * GetSystemMenu (USER32.291)
3981 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3983 WND *wndPtr = WIN_FindWndPtr( hWnd );
3984 HMENU retvalue = 0;
3986 if (wndPtr)
3988 if( wndPtr->hSysMenu )
3990 if( bRevert )
3992 DestroyMenu(wndPtr->hSysMenu);
3993 wndPtr->hSysMenu = 0;
3995 else
3997 POPUPMENU *menu = MENU_GetMenu( wndPtr->hSysMenu );
3998 if( menu )
4000 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
4001 menu->items[0].hSubMenu = MENU_CopySysPopup();
4003 else
4005 WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
4006 wndPtr->hSysMenu, hWnd);
4007 wndPtr->hSysMenu = 0;
4012 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
4013 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
4015 if( wndPtr->hSysMenu )
4017 POPUPMENU *menu;
4018 retvalue = GetSubMenu16(wndPtr->hSysMenu, 0);
4020 /* Store the dummy sysmenu handle to facilitate the refresh */
4021 /* of the close button if the SC_CLOSE item change */
4022 menu = MENU_GetMenu(retvalue);
4023 if ( menu )
4024 menu->hSysMenuOwner = wndPtr->hSysMenu;
4026 WIN_ReleaseWndPtr(wndPtr);
4028 return bRevert ? 0 : retvalue;
4032 /*******************************************************************
4033 * SetSystemMenu16 (USER.280)
4035 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
4037 return SetSystemMenu( hwnd, hMenu );
4041 /*******************************************************************
4042 * SetSystemMenu (USER32.508)
4044 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4046 WND *wndPtr = WIN_FindWndPtr(hwnd);
4048 if (wndPtr)
4050 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
4051 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
4052 WIN_ReleaseWndPtr(wndPtr);
4053 return TRUE;
4055 return FALSE;
4059 /**********************************************************************
4060 * GetMenu16 (USER.157)
4062 HMENU16 WINAPI GetMenu16( HWND16 hWnd )
4064 return (HMENU16)GetMenu(hWnd);
4068 /**********************************************************************
4069 * GetMenu (USER32.257)
4071 HMENU WINAPI GetMenu( HWND hWnd )
4073 HMENU retvalue;
4074 WND * wndPtr = WIN_FindWndPtr(hWnd);
4076 if (!wndPtr) return 0;
4078 retvalue = (HMENU)wndPtr->wIDmenu;
4079 TRACE("for %swindow %04x returning %04x\n",
4080 (wndPtr->dwStyle & WS_CHILD) ? "child " : "", hWnd, retvalue);
4081 WIN_ReleaseWndPtr(wndPtr);
4082 return retvalue;
4086 /**********************************************************************
4087 * SetMenu16 (USER.158)
4089 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
4091 return SetMenu( hWnd, hMenu );
4095 /**********************************************************************
4096 * SetMenu (USER32.487)
4098 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4100 WND * wndPtr = WIN_FindWndPtr(hWnd);
4101 BOOL res = FALSE;
4103 TRACE("(%04x, %04x);\n", hWnd, hMenu);
4105 if (hMenu && !IsMenu(hMenu))
4107 WARN("hMenu is not a menu handle\n");
4108 goto exit;
4111 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
4113 if (GetCapture() == hWnd) ReleaseCapture();
4115 wndPtr->wIDmenu = (UINT)hMenu;
4116 if (hMenu != 0)
4118 LPPOPUPMENU lpmenu;
4120 if (!(lpmenu = MENU_GetMenu(hMenu)))
4121 goto exit;
4123 lpmenu->hWnd = hWnd;
4124 lpmenu->Height = 0; /* Make sure we recalculate the size */
4126 if (IsWindowVisible(hWnd))
4127 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4128 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4129 res = TRUE;
4131 exit:
4132 WIN_ReleaseWndPtr(wndPtr);
4133 return res;
4138 /**********************************************************************
4139 * GetSubMenu16 (USER.159)
4141 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
4143 return GetSubMenu( hMenu, nPos );
4147 /**********************************************************************
4148 * GetSubMenu (USER32.288)
4150 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4152 MENUITEM * lpmi;
4154 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return 0;
4155 if (!(lpmi->fType & MF_POPUP)) return 0;
4156 return lpmi->hSubMenu;
4160 /**********************************************************************
4161 * DrawMenuBar16 (USER.160)
4163 void WINAPI DrawMenuBar16( HWND16 hWnd )
4165 DrawMenuBar( hWnd );
4169 /**********************************************************************
4170 * DrawMenuBar (USER32.161)
4172 BOOL WINAPI DrawMenuBar( HWND hWnd )
4174 LPPOPUPMENU lppop;
4175 WND *wndPtr = WIN_FindWndPtr(hWnd);
4176 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
4178 lppop = MENU_GetMenu((HMENU16)wndPtr->wIDmenu);
4179 if (lppop == NULL)
4181 WIN_ReleaseWndPtr(wndPtr);
4182 return FALSE;
4185 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4186 lppop->hwndOwner = hWnd;
4187 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4188 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4189 WIN_ReleaseWndPtr(wndPtr);
4190 return TRUE;
4192 WIN_ReleaseWndPtr(wndPtr);
4193 return FALSE;
4197 /***********************************************************************
4198 * EndMenu (USER.187) (USER32.175)
4200 void WINAPI EndMenu(void)
4202 /* if we are in the menu code, and it is active */
4203 if (fEndMenu == FALSE && MENU_IsMenuActive())
4205 /* terminate the menu handling code */
4206 fEndMenu = TRUE;
4208 /* needs to be posted to wakeup the internal menu handler */
4209 /* which will now terminate the menu, in the event that */
4210 /* the main window was minimized, or lost focus, so we */
4211 /* don't end up with an orphaned menu */
4212 PostMessageA( pTopPopupWnd->hwndSelf, WM_CANCELMODE, 0, 0);
4217 /***********************************************************************
4218 * LookupMenuHandle (USER.217)
4220 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
4222 HMENU hmenu32 = hmenu;
4223 UINT id32 = id;
4224 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
4225 else return hmenu32;
4229 /**********************************************************************
4230 * LoadMenu16 (USER.150)
4232 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
4234 HRSRC16 hRsrc;
4235 HGLOBAL16 handle;
4236 HMENU16 hMenu;
4238 if (HIWORD(name))
4240 char *str = (char *)PTR_SEG_TO_LIN( name );
4241 TRACE("(%04x,'%s')\n", instance, str );
4242 if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
4244 else
4245 TRACE("(%04x,%04x)\n",instance,LOWORD(name));
4247 if (!name) return 0;
4249 /* check for Win32 module */
4250 if (HIWORD(instance))
4251 return LoadMenuA(instance,PTR_SEG_TO_LIN(name));
4252 instance = GetExePtr( instance );
4254 if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
4255 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
4256 hMenu = LoadMenuIndirect16(LockResource16(handle));
4257 FreeResource16( handle );
4258 return hMenu;
4262 /*****************************************************************
4263 * LoadMenuA (USER32.370)
4265 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4267 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4268 if (!hrsrc) return 0;
4269 return LoadMenuIndirectA( (LPCVOID)LoadResource( instance, hrsrc ));
4273 /*****************************************************************
4274 * LoadMenuW (USER32.373)
4276 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4278 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4279 if (!hrsrc) return 0;
4280 return LoadMenuIndirectW( (LPCVOID)LoadResource( instance, hrsrc ));
4284 /**********************************************************************
4285 * LoadMenuIndirect16 (USER.220)
4287 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4289 HMENU16 hMenu;
4290 WORD version, offset;
4291 LPCSTR p = (LPCSTR)template;
4293 TRACE("(%p)\n", template );
4294 version = GET_WORD(p);
4295 p += sizeof(WORD);
4296 if (version)
4298 WARN("version must be 0 for Win16\n" );
4299 return 0;
4301 offset = GET_WORD(p);
4302 p += sizeof(WORD) + offset;
4303 if (!(hMenu = CreateMenu())) return 0;
4304 if (!MENU_ParseResource( p, hMenu, FALSE ))
4306 DestroyMenu( hMenu );
4307 return 0;
4309 return hMenu;
4313 /**********************************************************************
4314 * LoadMenuIndirectA (USER32.371)
4316 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4318 HMENU16 hMenu;
4319 WORD version, offset;
4320 LPCSTR p = (LPCSTR)template;
4322 TRACE("%p\n", template );
4323 version = GET_WORD(p);
4324 p += sizeof(WORD);
4325 switch (version)
4327 case 0:
4328 offset = GET_WORD(p);
4329 p += sizeof(WORD) + offset;
4330 if (!(hMenu = CreateMenu())) return 0;
4331 if (!MENU_ParseResource( p, hMenu, TRUE ))
4333 DestroyMenu( hMenu );
4334 return 0;
4336 return hMenu;
4337 case 1:
4338 offset = GET_WORD(p);
4339 p += sizeof(WORD) + offset;
4340 if (!(hMenu = CreateMenu())) return 0;
4341 if (!MENUEX_ParseResource( p, hMenu))
4343 DestroyMenu( hMenu );
4344 return 0;
4346 return hMenu;
4347 default:
4348 ERR("version %d not supported.\n", version);
4349 return 0;
4354 /**********************************************************************
4355 * LoadMenuIndirectW (USER32.372)
4357 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4359 /* FIXME: is there anything different between A and W? */
4360 return LoadMenuIndirectA( template );
4364 /**********************************************************************
4365 * IsMenu16 (USER.358)
4367 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
4369 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
4370 return IS_A_MENU(menu);
4374 /**********************************************************************
4375 * IsMenu (USER32.346)
4377 BOOL WINAPI IsMenu(HMENU hmenu)
4379 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
4380 return IS_A_MENU(menu);
4383 /**********************************************************************
4384 * GetMenuItemInfo_common
4387 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4388 LPMENUITEMINFOW lpmii, BOOL unicode)
4390 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4392 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4394 if (!menu)
4395 return FALSE;
4397 if (lpmii->fMask & MIIM_TYPE) {
4398 lpmii->fType = menu->fType;
4399 switch (MENU_ITEM_TYPE(menu->fType)) {
4400 case MF_STRING:
4401 if (menu->text) {
4402 int len = strlenW(menu->text);
4403 if(lpmii->dwTypeData && lpmii->cch) {
4404 if (unicode)
4405 lstrcpynW(lpmii->dwTypeData, menu->text,
4406 lpmii->cch);
4407 else
4408 lstrcpynWtoA((LPSTR)lpmii->dwTypeData, menu->text, lpmii->cch);
4409 /* if we've copied a substring we return its length */
4410 if(lpmii->cch <= len)
4411 lpmii->cch--;
4412 } else /* return length of string */
4413 lpmii->cch = len;
4415 break;
4416 case MF_OWNERDRAW:
4417 case MF_BITMAP:
4418 lpmii->dwTypeData = menu->text;
4419 /* fall through */
4420 default:
4421 lpmii->cch = 0;
4425 if (lpmii->fMask & MIIM_STRING) {
4426 if(lpmii->dwTypeData && lpmii->cch) {
4427 if (unicode)
4428 lstrcpynW((LPWSTR) lpmii->dwTypeData, menu->text,
4429 lpmii->cch);
4430 else
4431 lstrcpynWtoA((LPSTR)lpmii->dwTypeData, menu->text, lpmii->cch);
4433 lpmii->cch = strlenW(menu->text);
4436 if (lpmii->fMask & MIIM_FTYPE)
4437 lpmii->fType = menu->fType;
4439 if (lpmii->fMask & MIIM_BITMAP)
4440 lpmii->hbmpItem = menu->hbmpItem;
4442 if (lpmii->fMask & MIIM_STATE)
4443 lpmii->fState = menu->fState;
4445 if (lpmii->fMask & MIIM_ID)
4446 lpmii->wID = menu->wID;
4448 if (lpmii->fMask & MIIM_SUBMENU)
4449 lpmii->hSubMenu = menu->hSubMenu;
4451 if (lpmii->fMask & MIIM_CHECKMARKS) {
4452 lpmii->hbmpChecked = menu->hCheckBit;
4453 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4455 if (lpmii->fMask & MIIM_DATA)
4456 lpmii->dwItemData = menu->dwItemData;
4458 return TRUE;
4461 /**********************************************************************
4462 * GetMenuItemInfoA (USER32.264)
4464 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4465 LPMENUITEMINFOA lpmii)
4467 return GetMenuItemInfo_common (hmenu, item, bypos,
4468 (LPMENUITEMINFOW)lpmii, FALSE);
4471 /**********************************************************************
4472 * GetMenuItemInfoW (USER32.265)
4474 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4475 LPMENUITEMINFOW lpmii)
4477 return GetMenuItemInfo_common (hmenu, item, bypos,
4478 lpmii, TRUE);
4481 /**********************************************************************
4482 * SetMenuItemInfo_common
4485 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4486 const MENUITEMINFOW *lpmii,
4487 BOOL unicode)
4489 if (!menu) return FALSE;
4491 if (lpmii->fMask & MIIM_TYPE ) {
4492 /* Get rid of old string. */
4493 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4494 HeapFree(SystemHeap, 0, menu->text);
4495 menu->text = NULL;
4498 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4499 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4500 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4502 menu->text = lpmii->dwTypeData;
4504 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4505 if (unicode)
4506 menu->text = HEAP_strdupW(SystemHeap, 0, lpmii->dwTypeData);
4507 else
4508 menu->text = HEAP_strdupAtoW(SystemHeap, 0, (LPSTR)lpmii->dwTypeData);
4512 if (lpmii->fMask & MIIM_FTYPE ) {
4513 /* free the string when the type is changing */
4514 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4515 HeapFree(SystemHeap, 0, menu->text);
4516 menu->text = NULL;
4518 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4519 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4522 if (lpmii->fMask & MIIM_STRING ) {
4523 /* free the string when used */
4524 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4525 HeapFree(SystemHeap, 0, menu->text);
4526 if (unicode)
4527 menu->text = HEAP_strdupW(SystemHeap, 0, lpmii->dwTypeData);
4528 else
4529 menu->text = HEAP_strdupAtoW(SystemHeap, 0, (LPSTR) lpmii->dwTypeData);
4533 if (lpmii->fMask & MIIM_STATE)
4535 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4536 menu->fState = lpmii->fState;
4539 if (lpmii->fMask & MIIM_ID)
4540 menu->wID = lpmii->wID;
4542 if (lpmii->fMask & MIIM_SUBMENU) {
4543 menu->hSubMenu = lpmii->hSubMenu;
4544 if (menu->hSubMenu) {
4545 POPUPMENU *subMenu = MENU_GetMenu((UINT16)menu->hSubMenu);
4546 if (subMenu) {
4547 subMenu->wFlags |= MF_POPUP;
4548 menu->fType |= MF_POPUP;
4550 else
4551 /* FIXME: Return an error ? */
4552 menu->fType &= ~MF_POPUP;
4554 else
4555 menu->fType &= ~MF_POPUP;
4558 if (lpmii->fMask & MIIM_CHECKMARKS)
4560 if (lpmii->fType & MFT_RADIOCHECK)
4561 menu->fType |= MFT_RADIOCHECK;
4563 menu->hCheckBit = lpmii->hbmpChecked;
4564 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4566 if (lpmii->fMask & MIIM_DATA)
4567 menu->dwItemData = lpmii->dwItemData;
4569 debug_print_menuitem("SetMenuItemInfo_common: ", menu, "");
4570 return TRUE;
4573 /**********************************************************************
4574 * SetMenuItemInfoA (USER32.491)
4576 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4577 const MENUITEMINFOA *lpmii)
4579 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4580 (const MENUITEMINFOW *)lpmii, FALSE);
4583 /**********************************************************************
4584 * SetMenuItemInfoW (USER32.492)
4586 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4587 const MENUITEMINFOW *lpmii)
4589 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4590 lpmii, TRUE);
4593 /**********************************************************************
4594 * SetMenuDefaultItem (USER32.489)
4597 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4599 UINT i;
4600 POPUPMENU *menu;
4601 MENUITEM *item;
4603 TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4605 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4607 /* reset all default-item flags */
4608 item = menu->items;
4609 for (i = 0; i < menu->nItems; i++, item++)
4611 item->fState &= ~MFS_DEFAULT;
4614 /* no default item */
4615 if ( -1 == uItem)
4617 return TRUE;
4620 item = menu->items;
4621 if ( bypos )
4623 if ( uItem >= menu->nItems ) return FALSE;
4624 item[uItem].fState |= MFS_DEFAULT;
4625 return TRUE;
4627 else
4629 for (i = 0; i < menu->nItems; i++, item++)
4631 if (item->wID == uItem)
4633 item->fState |= MFS_DEFAULT;
4634 return TRUE;
4639 return FALSE;
4642 /**********************************************************************
4643 * GetMenuDefaultItem (USER32.260)
4645 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4647 POPUPMENU *menu;
4648 MENUITEM * item;
4649 UINT i = 0;
4651 TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4653 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4655 /* find default item */
4656 item = menu->items;
4658 /* empty menu */
4659 if (! item) return -1;
4661 while ( !( item->fState & MFS_DEFAULT ) )
4663 i++; item++;
4664 if (i >= menu->nItems ) return -1;
4667 /* default: don't return disabled items */
4668 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4670 /* search rekursiv when needed */
4671 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4673 UINT ret;
4674 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4675 if ( -1 != ret ) return ret;
4677 /* when item not found in submenu, return the popup item */
4679 return ( bypos ) ? i : item->wID;
4683 /*******************************************************************
4684 * InsertMenuItem16 (USER.441)
4686 * FIXME: untested
4688 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
4689 const MENUITEMINFO16 *mii )
4691 MENUITEMINFOA miia;
4693 miia.cbSize = sizeof(miia);
4694 miia.fMask = mii->fMask;
4695 miia.dwTypeData = mii->dwTypeData;
4696 miia.fType = mii->fType;
4697 miia.fState = mii->fState;
4698 miia.wID = mii->wID;
4699 miia.hSubMenu = mii->hSubMenu;
4700 miia.hbmpChecked = mii->hbmpChecked;
4701 miia.hbmpUnchecked = mii->hbmpUnchecked;
4702 miia.dwItemData = mii->dwItemData;
4703 miia.cch = mii->cch;
4704 if (IS_STRING_ITEM(miia.fType))
4705 miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
4706 return InsertMenuItemA( hmenu, pos, byposition, &miia );
4710 /**********************************************************************
4711 * InsertMenuItemA (USER32.323)
4713 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4714 const MENUITEMINFOA *lpmii)
4716 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4717 return SetMenuItemInfo_common(item, (const MENUITEMINFOW *)lpmii, FALSE);
4721 /**********************************************************************
4722 * InsertMenuItemW (USER32.324)
4724 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4725 const MENUITEMINFOW *lpmii)
4727 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4728 return SetMenuItemInfo_common(item, lpmii, TRUE);
4731 /**********************************************************************
4732 * CheckMenuRadioItem (USER32.47)
4735 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4736 UINT first, UINT last, UINT check,
4737 UINT bypos)
4739 MENUITEM *mifirst, *milast, *micheck;
4740 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4742 TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4743 hMenu, first, last, check, bypos);
4745 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4746 milast = MENU_FindItem (&mlast, &last, bypos);
4747 micheck = MENU_FindItem (&mcheck, &check, bypos);
4749 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4750 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4751 micheck > milast || micheck < mifirst)
4752 return FALSE;
4754 while (mifirst <= milast)
4756 if (mifirst == micheck)
4758 mifirst->fType |= MFT_RADIOCHECK;
4759 mifirst->fState |= MFS_CHECKED;
4760 } else {
4761 mifirst->fType &= ~MFT_RADIOCHECK;
4762 mifirst->fState &= ~MFS_CHECKED;
4764 mifirst++;
4767 return TRUE;
4770 /**********************************************************************
4771 * CheckMenuRadioItem16 (not a Windows API)
4774 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4775 UINT16 first, UINT16 last, UINT16 check,
4776 BOOL16 bypos)
4778 return CheckMenuRadioItem (hMenu, first, last, check, bypos);
4781 /**********************************************************************
4782 * GetMenuItemRect (USER32.266)
4784 * ATTENTION: Here, the returned values in rect are the screen
4785 * coordinates of the item just like if the menu was
4786 * always on the upper left side of the application.
4789 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4790 LPRECT rect)
4792 POPUPMENU *itemMenu;
4793 MENUITEM *item;
4794 HWND referenceHwnd;
4796 TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4798 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4799 referenceHwnd = hwnd;
4801 if(!hwnd)
4803 itemMenu = MENU_GetMenu(hMenu);
4804 if (itemMenu == NULL)
4805 return FALSE;
4807 if(itemMenu->hWnd == 0)
4808 return FALSE;
4809 referenceHwnd = itemMenu->hWnd;
4812 if ((rect == NULL) || (item == NULL))
4813 return FALSE;
4815 *rect = item->rect;
4817 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4819 return TRUE;
4822 /**********************************************************************
4823 * GetMenuItemRect16 (USER.665)
4826 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4827 LPRECT16 rect)
4829 RECT r32;
4830 BOOL res;
4832 if (!rect) return FALSE;
4833 res = GetMenuItemRect (hwnd, hMenu, uItem, &r32);
4834 CONV_RECT32TO16 (&r32, rect);
4835 return res;
4838 /**********************************************************************
4839 * SetMenuInfo
4841 * FIXME
4842 * MIM_APPLYTOSUBMENUS
4843 * actually use the items to draw the menu
4845 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4847 POPUPMENU *menu;
4849 TRACE("(0x%04x %p)\n", hMenu, lpmi);
4851 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
4854 if (lpmi->fMask & MIM_BACKGROUND)
4855 menu->hbrBack = lpmi->hbrBack;
4857 if (lpmi->fMask & MIM_HELPID)
4858 menu->dwContextHelpID = lpmi->dwContextHelpID;
4860 if (lpmi->fMask & MIM_MAXHEIGHT)
4861 menu->cyMax = lpmi->cyMax;
4863 if (lpmi->fMask & MIM_MENUDATA)
4864 menu->dwMenuData = lpmi->dwMenuData;
4866 if (lpmi->fMask & MIM_STYLE)
4867 menu->dwStyle = lpmi->dwStyle;
4869 return TRUE;
4871 return FALSE;
4874 /**********************************************************************
4875 * GetMenuInfo
4877 * NOTES
4878 * win98/NT5.0
4881 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4882 { POPUPMENU *menu;
4884 TRACE("(0x%04x %p)\n", hMenu, lpmi);
4886 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4889 if (lpmi->fMask & MIM_BACKGROUND)
4890 lpmi->hbrBack = menu->hbrBack;
4892 if (lpmi->fMask & MIM_HELPID)
4893 lpmi->dwContextHelpID = menu->dwContextHelpID;
4895 if (lpmi->fMask & MIM_MAXHEIGHT)
4896 lpmi->cyMax = menu->cyMax;
4898 if (lpmi->fMask & MIM_MENUDATA)
4899 lpmi->dwMenuData = menu->dwMenuData;
4901 if (lpmi->fMask & MIM_STYLE)
4902 lpmi->dwStyle = menu->dwStyle;
4904 return TRUE;
4906 return FALSE;
4909 /**********************************************************************
4910 * SetMenuContextHelpId16 (USER.384)
4912 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpID)
4914 return SetMenuContextHelpId( hMenu, dwContextHelpID );
4918 /**********************************************************************
4919 * SetMenuContextHelpId (USER32.488)
4921 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4923 LPPOPUPMENU menu;
4925 TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4927 if ((menu = MENU_GetMenu(hMenu)))
4929 menu->dwContextHelpID = dwContextHelpID;
4930 return TRUE;
4932 return FALSE;
4935 /**********************************************************************
4936 * GetMenuContextHelpId16 (USER.385)
4938 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4940 return GetMenuContextHelpId( hMenu );
4943 /**********************************************************************
4944 * GetMenuContextHelpId (USER32.488)
4946 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4948 LPPOPUPMENU menu;
4950 TRACE("(0x%04x)\n", hMenu);
4952 if ((menu = MENU_GetMenu(hMenu)))
4954 return menu->dwContextHelpID;
4956 return 0;
4959 /**********************************************************************
4960 * MenuItemFromPoint (USER32.387)
4962 UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4964 FIXME("(0x%04x,0x%04x,(%ld,%ld)):stub\n",
4965 hWnd, hMenu, ptScreen.x, ptScreen.y);
4966 return 0;
4970 /**********************************************************************
4971 * translate_accelerator
4973 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
4974 BYTE fVirt, WORD key, WORD cmd )
4976 UINT mesg = 0;
4978 if (wParam != key) return FALSE;
4980 if (message == WM_CHAR)
4982 if ( !(fVirt & FALT) && !(fVirt & FVIRTKEY) )
4984 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", wParam & 0xff);
4985 goto found;
4988 else
4990 if(fVirt & FVIRTKEY)
4992 INT mask = 0;
4993 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
4994 wParam, 0xff & HIWORD(lParam));
4995 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
4996 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
4997 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
4998 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
4999 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
5001 else
5003 if (!(lParam & 0x01000000)) /* no special_key */
5005 if ((fVirt & FALT) && (lParam & 0x20000000))
5006 { /* ^^ ALT pressed */
5007 TRACE_(accel)("found accel for Alt-%c\n", wParam & 0xff);
5008 goto found;
5013 return FALSE;
5015 found:
5016 if (message == WM_KEYUP || message == WM_SYSKEYUP)
5017 mesg = 1;
5018 else if (GetCapture())
5019 mesg = 2;
5020 else if (!IsWindowEnabled(hWnd))
5021 mesg = 3;
5022 else
5024 HMENU hMenu, hSubMenu, hSysMenu;
5025 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
5026 WND* wndPtr = WIN_FindWndPtr(hWnd);
5028 hMenu = (wndPtr->dwStyle & WS_CHILD) ? 0 : (HMENU)wndPtr->wIDmenu;
5029 hSysMenu = wndPtr->hSysMenu;
5030 WIN_ReleaseWndPtr(wndPtr);
5032 /* find menu item and ask application to initialize it */
5033 /* 1. in the system menu */
5034 hSubMenu = hSysMenu;
5035 nPos = cmd;
5036 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5038 SendMessageA(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
5039 if(hSubMenu != hSysMenu)
5041 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
5042 TRACE_(accel)("hSysMenu = %04x, hSubMenu = %04x, nPos = %d\n", hSysMenu, hSubMenu, nPos);
5043 SendMessageA(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
5045 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
5047 else /* 2. in the window's menu */
5049 hSubMenu = hMenu;
5050 nPos = cmd;
5051 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5053 SendMessageA(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
5054 if(hSubMenu != hMenu)
5056 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
5057 TRACE_(accel)("hMenu = %04x, hSubMenu = %04x, nPos = %d\n", hMenu, hSubMenu, nPos);
5058 SendMessageA(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
5060 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
5064 if (uSysStat != (UINT)-1)
5066 if (uSysStat & (MF_DISABLED|MF_GRAYED))
5067 mesg=4;
5068 else
5069 mesg=WM_SYSCOMMAND;
5071 else
5073 if (uStat != (UINT)-1)
5075 if (IsIconic(hWnd))
5076 mesg=5;
5077 else
5079 if (uStat & (MF_DISABLED|MF_GRAYED))
5080 mesg=6;
5081 else
5082 mesg=WM_COMMAND;
5085 else
5086 mesg=WM_COMMAND;
5090 if( mesg==WM_COMMAND )
5092 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
5093 SendMessageA(hWnd, mesg, 0x10000 | cmd, 0L);
5095 else if( mesg==WM_SYSCOMMAND )
5097 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
5098 SendMessageA(hWnd, mesg, cmd, 0x00010000L);
5100 else
5102 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
5103 * #0: unknown (please report!)
5104 * #1: for WM_KEYUP,WM_SYSKEYUP
5105 * #2: mouse is captured
5106 * #3: window is disabled
5107 * #4: it's a disabled system menu option
5108 * #5: it's a menu option, but window is iconic
5109 * #6: it's a menu option, but disabled
5111 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
5112 if(mesg==0)
5113 ERR_(accel)(" unknown reason - please report!");
5115 return TRUE;
5118 /**********************************************************************
5119 * TranslateAccelerator (USER32.551)(USER32.552)(USER32.553)
5121 INT WINAPI TranslateAccelerator( HWND hWnd, HACCEL hAccel, LPMSG msg )
5123 /* YES, Accel16! */
5124 LPACCEL16 lpAccelTbl;
5125 int i;
5127 if (msg == NULL)
5129 WARN_(accel)("msg null; should hang here to be win compatible\n");
5130 return 0;
5132 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
5134 WARN_(accel)("invalid accel handle=%x\n", hAccel);
5135 return 0;
5137 if ((msg->message != WM_KEYDOWN &&
5138 msg->message != WM_KEYUP &&
5139 msg->message != WM_SYSKEYDOWN &&
5140 msg->message != WM_SYSKEYUP &&
5141 msg->message != WM_CHAR)) return 0;
5143 TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,"
5144 "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%lx\n",
5145 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5147 i = 0;
5150 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5151 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
5152 return 1;
5153 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
5154 WARN_(accel)("couldn't translate accelerator key\n");
5155 return 0;
5159 /**********************************************************************
5160 * TranslateAccelerator16 (USER.178)
5162 INT16 WINAPI TranslateAccelerator16( HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg )
5164 LPACCEL16 lpAccelTbl;
5165 int i;
5167 if (msg == NULL)
5169 WARN_(accel)("msg null; should hang here to be win compatible\n");
5170 return 0;
5172 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
5174 WARN_(accel)("invalid accel handle=%x\n", hAccel);
5175 return 0;
5177 if ((msg->message != WM_KEYDOWN &&
5178 msg->message != WM_KEYUP &&
5179 msg->message != WM_SYSKEYDOWN &&
5180 msg->message != WM_SYSKEYUP &&
5181 msg->message != WM_CHAR)) return 0;
5183 TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,"
5184 "msg->hwnd=%04x, msg->message=%04x, wParam=%04x, lParam=%lx\n",
5185 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5187 i = 0;
5190 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5191 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd ))
5192 return 1;
5193 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
5194 WARN_(accel)("couldn't translate accelerator key\n");
5195 return 0;