source: trunk/src/user32/menu.cpp@ 6773

Last change on this file since 6773 was 6773, checked in by sandervl, 24 years ago

WM_MOUSEACTIVATE & WM_NEXTMENU fixes

File size: 147.6 KB
Line 
1/* $Id: menu.cpp,v 1.37 2001-09-20 12:57:15 sandervl Exp $*/
2/*
3 * Menu functions
4 *
5 * Copyright 1993 Martin Ayotte
6 * Copyright 1994 Alexandre Julliard
7 * Copyright 1997 Morten Welinder
8 *
9 * Copyright 1999 Christoph Bratschi
10 *
11 * Corel version: 200005313
12 * (WINE version: 20000130)
13 *
14 * Status: ???
15 * Version: ???
16 */
17
18/*
19 * Note: the style MF_MOUSESELECT is used to mark popup items that
20 * have been selected, i.e. their popup menu is currently displayed.
21 * This is probably not the meaning this style has in MS-Windows.
22 */
23
24#include <assert.h>
25#include <ctype.h>
26#include <stdlib.h>
27#include <string.h>
28#include <os2win.h>
29#include <heapstring.h>
30#include "controls.h"
31#include "menu.h"
32
33#ifdef __WIN32OS2__
34#include <objhandle.h>
35#include "pmwindow.h"
36#include "win32wmisc.h"
37
38#define DBG_LOCALLOG DBG_menu
39#include "dbglocal.h"
40#endif
41
42//DEFAULT_DEBUG_CHANNEL(menu)
43//DECLARE_DEBUG_CHANNEL(winhelp)
44
45
46/* internal popup menu window messages */
47
48#define MM_SETMENUHANDLE (WM_USER + 0)
49#define MM_GETMENUHANDLE (WM_USER + 1)
50
51/* Menu item structure */
52typedef struct {
53 /* ----------- MENUITEMINFO Stuff ----------- */
54 UINT fType; /* Item type. */
55 UINT fState; /* Item state. */
56 UINT wID; /* Item id. */
57 HMENU hSubMenu; /* Pop-up menu. */
58 HBITMAP hCheckBit; /* Bitmap when checked. */
59 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
60 LPSTR text; /* Item text or bitmap handle. */
61 DWORD dwItemData; /* Application defined. */
62 DWORD dwTypeData; /* depends on fMask */
63 HBITMAP hbmpItem; /* bitmap in win98 style menus */
64 /* ----------- Wine stuff ----------- */
65 RECT rect; /* Item area (relative to menu window) */
66 UINT xTab; /* X position of text after Tab */
67} MENUITEM;
68
69/* Popup menu structure */
70typedef struct {
71 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
72 WORD wMagic; /* Magic number */
73 HQUEUE hTaskQ; /* Task queue for this menu */
74 WORD Width; /* Width of the whole menu */
75 WORD Height; /* Height of the whole menu */
76 WORD nItems; /* Number of items in the menu */
77 HWND hWnd; /* Window containing the menu */
78 MENUITEM *items; /* Array of menu items */
79 UINT FocusedItem; /* Currently focused item */
80 HWND hwndOwner; /* window receiving the messages for ownerdraw */
81 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
82 /* ------------ MENUINFO members ------ */
83 DWORD dwStyle; /* Extended mennu style */
84 UINT cyMax; /* max hight of the whole menu, 0 is screen hight */
85 HBRUSH hbrBack; /* brush for menu background */
86 DWORD dwContextHelpID;
87 DWORD dwMenuData; /* application defined value */
88 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
89} POPUPMENU, *LPPOPUPMENU;
90
91/* internal flags for menu tracking */
92
93#define TF_ENDMENU 0x0001
94#define TF_SUSPENDPOPUP 0x0002
95#define TF_SKIPREMOVE 0x0004
96
97typedef struct
98{
99 UINT trackFlags;
100 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
101 HMENU hTopMenu; /* initial menu */
102 HWND hOwnerWnd; /* where notifications are sent */
103 POINT pt;
104} MTRACKER;
105
106#define MENU_MAGIC 0x554d /* 'MU' */
107
108#define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
109
110#define ITEM_PREV -1
111#define ITEM_NEXT 1
112
113 /* Internal MENU_TrackMenu() flags */
114#define TPM_INTERNAL 0xF0000000
115#define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
116#define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
117#define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
118#define TPM_CAPTIONSYSMENU 0x10000000
119
120 /* popup menu shade thickness */
121#define POPUP_XSHADE 4
122#define POPUP_YSHADE 4
123
124 /* Space between 2 menu bar items */
125#define MENU_BAR_ITEMS_SPACE 12
126
127 /* Minimum width of a tab character */
128#define MENU_TAB_SPACE 8
129
130 /* Height of a separator item */
131#define SEPARATOR_HEIGHT 5
132
133 /* (other menu->FocusedItem values give the position of the focused item) */
134#define NO_SELECTED_ITEM 0xffff
135
136#define MENU_ITEM_TYPE(flags) \
137 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
138
139#define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
140#define IS_SEPARATOR_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_SEPARATOR)
141#define IS_BITMAP_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_BITMAP)
142
143#define IS_SYSTEM_MENU(menu) \
144 (!((menu)->wFlags & MF_POPUP) && (menu)->wFlags & MF_SYSMENU)
145
146#define IS_SYSTEM_POPUP(menu) \
147 ((menu)->wFlags & MF_POPUP && (menu)->wFlags & MF_SYSMENU)
148
149#define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
150 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
151 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
152 MF_POPUP | MF_SYSMENU | MF_HELP)
153#define STATE_MASK (~TYPE_MASK)
154
155 /* Dimension of the menu bitmaps */
156static WORD check_bitmap_width = 0, check_bitmap_height = 0;
157static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
158
159static HBITMAP hStdRadioCheck = 0;
160static HBITMAP hStdCheck = 0;
161static HBITMAP hStdMnArrow = 0;
162
163/* Minimze/restore/close buttons to be inserted in menubar */
164static HBITMAP hBmpMinimize = 0;
165static HBITMAP hBmpMinimizeD = 0;
166static HBITMAP hBmpMaximize = 0;
167static HBITMAP hBmpMaximizeD = 0;
168static HBITMAP hBmpClose = 0;
169static HBITMAP hBmpCloseD = 0;
170
171
172static HBRUSH hShadeBrush = 0;
173static HFONT hMenuFont = 0;
174static HFONT hMenuFontBold = 0;
175
176static HMENU MENU_DefSysPopup = 0; /* Default system menu popup */
177
178/* Use global popup window because there's no way 2 menus can
179 * be tracked at the same time. */
180
181static HWND pTopPopupWnd = 0;
182static UINT uSubPWndLevel = 0;
183
184 /* Flag set by EndMenu() to force an exit from menu tracking */
185static BOOL fEndMenu = FALSE;
186
187/*
188 The following variables and defines are used to keep track of which
189 menu item the mouse is currently over. (Used to implement a pause before
190 popup menus are displayed. )
191
192 See: MENU_MouseMove()
193 MENU_TrackMenu()
194*/
195#define SUBMENU_POPUP_TIMERID 100
196#define POPUP_MENU_DELAY 500
197static UINT mouseOverMenuID = -1;
198static BOOL isTimerSet = FALSE;
199
200/***********************************************************************
201 * debug_print_menuitem
202 *
203 * Print a menuitem in readable form.
204 */
205
206#define debug_print_menuitem(pre, mp, post) \
207 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
208
209#define MENUOUT(text) \
210 DPRINTF("%s%s", (count++ ? "," : ""), (text))
211
212#define MENUFLAG(bit,text) \
213 do { \
214 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
215 } while (0)
216
217#if 0
218static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
219 const char *postfix)
220{
221 TRACE("%s ", prefix);
222 if (mp) {
223 UINT flags = mp->fType;
224 int typ = MENU_ITEM_TYPE(flags);
225 DPRINTF( "{ ID=0x%x", mp->wID);
226 if (flags & MF_POPUP)
227 DPRINTF( ", Sub=0x%x", mp->hSubMenu);
228 if (flags) {
229 int count = 0;
230 DPRINTF( ", Typ=");
231 if (typ == MFT_STRING)
232 /* Nothing */ ;
233 else if (typ == MFT_SEPARATOR)
234 MENUOUT("sep");
235 else if (typ == MFT_OWNERDRAW)
236 MENUOUT("own");
237 else if (typ == MFT_BITMAP)
238 MENUOUT("bit");
239 else
240 MENUOUT("???");
241 flags -= typ;
242
243 MENUFLAG(MF_POPUP, "pop");
244 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
245 MENUFLAG(MFT_MENUBREAK, "brk");
246 MENUFLAG(MFT_RADIOCHECK, "radio");
247 MENUFLAG(MFT_RIGHTORDER, "rorder");
248 MENUFLAG(MF_SYSMENU, "sys");
249 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
250
251 if (flags)
252 DPRINTF( "+0x%x", flags);
253 }
254 flags = mp->fState;
255 if (flags) {
256 int count = 0;
257 DPRINTF( ", State=");
258 MENUFLAG(MFS_GRAYED, "grey");
259 MENUFLAG(MFS_DEFAULT, "default");
260 MENUFLAG(MFS_DISABLED, "dis");
261 MENUFLAG(MFS_CHECKED, "check");
262 MENUFLAG(MFS_HILITE, "hi");
263 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
264 MENUFLAG(MF_MOUSESELECT, "mouse");
265 if (flags)
266 DPRINTF( "+0x%x", flags);
267 }
268 if (mp->hCheckBit)
269 DPRINTF( ", Chk=0x%x", mp->hCheckBit);
270 if (mp->hUnCheckBit)
271 DPRINTF( ", Unc=0x%x", mp->hUnCheckBit);
272
273 if (typ == MFT_STRING) {
274 if (mp->text)
275 DPRINTF( ", Text=\"%s\"", mp->text);
276 else
277 DPRINTF( ", Text=Null");
278 } else if (mp->text == NULL)
279 /* Nothing */ ;
280 else
281 DPRINTF( ", Text=%p", mp->text);
282 if (mp->dwItemData)
283 DPRINTF( ", ItemData=0x%08lx", mp->dwItemData);
284 DPRINTF( " }");
285 } else {
286 DPRINTF( "NULL");
287 }
288
289 DPRINTF(" %s\n", postfix);
290}
291#endif
292
293#undef MENUOUT
294#undef MENUFLAG
295
296//#define USER_HEAP_ALLOC(size) HeapAlloc(GetProcessHeap(),0,size)
297//#define USER_HEAP_FREE(handle) HeapFree(GetProcessHeap(),0,(LPVOID)handle)
298
299HMENU getMenu(HWND hwnd)
300{
301 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
302
303 if(win32wnd) {
304 HMENU hmenu = win32wnd->GetMenu();
305 RELEASE_WNDOBJ(win32wnd);
306 return hmenu;
307 }
308 return (HMENU)0;
309}
310
311VOID setMenu(HWND hwnd,HMENU hMenu)
312{
313 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
314
315 if (win32wnd) {
316 win32wnd->SetMenu(hMenu);
317 RELEASE_WNDOBJ(win32wnd);
318 }
319}
320
321
322/***********************************************************************
323 * MENU_GetMenu
324 *
325 * Validate the given menu handle and returns the menu structure pointer.
326 */
327POPUPMENU *MENU_GetMenu(HMENU hMenu)
328{
329#ifdef __WIN32OS2__
330 if(ObjGetHandleType(hMenu) == USEROBJ_MENU) {
331 POPUPMENU *menu;
332
333 menu = (POPUPMENU *)ObjGetHandleData(hMenu, USEROBJ_MENU);
334 return menu;
335 }
336 return NULL;
337#else
338 POPUPMENU *menu;
339 menu = (POPUPMENU*)hMenu;
340 if (!IS_A_MENU(menu))
341 {
342 //ERR("invalid menu handle=%x, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
343 menu = NULL;
344 }
345 return menu;
346#endif
347}
348
349/***********************************************************************
350 * MENU_CopySysPopup
351 *
352 * Return the default system menu.
353 */
354static HMENU MENU_CopySysPopup(void)
355{
356 HMENU hMenu = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU");
357
358 if( hMenu ) {
359#ifdef __WIN32OS2__
360 POPUPMENU* menu = MENU_GetMenu(hMenu);
361#else
362 POPUPMENU* menu = (POPUPMENU*)hMenu;
363#endif
364 menu->wFlags |= MF_SYSMENU | MF_POPUP;
365 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
366 }
367 else {
368 hMenu = 0;
369 //ERR("Unable to load default system menu\n" );
370 }
371
372 //TRACE("returning %x.\n", hMenu );
373
374 return hMenu;
375}
376
377/***********************************************************************
378 * MENU_GetTopPopupWnd()
379 *
380 * Return the locked pointer pTopPopupWnd.
381 */
382static HWND MENU_GetTopPopupWnd()
383{
384 return pTopPopupWnd;
385}
386/***********************************************************************
387 * MENU_ReleaseTopPopupWnd()
388 *
389 * Realease the locked pointer pTopPopupWnd.
390 */
391static void MENU_ReleaseTopPopupWnd()
392{
393}
394/***********************************************************************
395 * MENU_DestroyTopPopupWnd()
396 *
397 * Destroy the locked pointer pTopPopupWnd.
398 */
399static void MENU_DestroyTopPopupWnd()
400{
401 pTopPopupWnd = NULL;
402}
403
404
405
406/**********************************************************************
407 * MENU_GetSysMenu
408 *
409 * Create a copy of the system menu. System menu in Windows is
410 * a special menu-bar with the single entry - system menu popup.
411 * This popup is presented to the outside world as a "system menu".
412 * However, the real system menu handle is sometimes seen in the
413 * WM_MENUSELECT paramemters (and Word 6 likes it this way).
414 */
415HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
416{
417 HMENU hMenu;
418
419 hMenu = CreateMenu();
420 if (hMenu)
421 {
422#ifdef __WIN32OS2__
423 POPUPMENU *menu = MENU_GetMenu(hMenu);
424#else
425 POPUPMENU *menu = (POPUPMENU*)hMenu;
426#endif
427 menu->wFlags = MF_SYSMENU;
428 menu->hWnd = hWnd;
429
430 if (hPopupMenu == (HMENU)(-1))
431 hPopupMenu = MENU_CopySysPopup();
432 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
433
434 if (hPopupMenu)
435 {
436 InsertMenuA( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
437
438 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
439 menu->items[0].fState = 0;
440#ifdef __WIN32OS2__
441 menu = MENU_GetMenu(hPopupMenu);
442#else
443 menu = (POPUPMENU*)hPopupMenu;
444#endif
445 menu->wFlags |= MF_SYSMENU;
446
447 //TRACE("GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
448 return hMenu;
449 }
450 DestroyMenu( hMenu );
451 }
452 //ERR("failed to load system menu!\n");
453 return 0;
454}
455
456
457/***********************************************************************
458 * MENU_Init
459 *
460 * Menus initialisation.
461 */
462BOOL MENU_Init()
463{
464 HBITMAP hBitmap;
465 NONCLIENTMETRICSA ncm;
466
467 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
468 0x55, 0, 0xAA, 0,
469 0x55, 0, 0xAA, 0,
470 0x55, 0, 0xAA, 0 };
471
472 /* Load menu bitmaps */
473 hStdCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECK));
474 hStdRadioCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_RADIOCHECK));
475 hStdMnArrow = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_MNARROW));
476 /* Load system buttons bitmaps */
477 hBmpMinimize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCE));
478 hBmpMinimizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCED));
479 hBmpMaximize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORE));
480 hBmpMaximizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORED));
481 hBmpClose = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSE));
482 hBmpCloseD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSED));
483
484 if (hStdCheck)
485 {
486 BITMAP bm;
487 GetObjectA( hStdCheck, sizeof(bm), &bm );
488 check_bitmap_width = bm.bmWidth;
489 check_bitmap_height = bm.bmHeight;
490 } else
491 return FALSE;
492
493 /* Assume that radio checks have the same size as regular check. */
494 if (!hStdRadioCheck)
495 return FALSE;
496
497 if (hStdMnArrow)
498 {
499 BITMAP bm;
500 GetObjectA( hStdMnArrow, sizeof(bm), &bm );
501 arrow_bitmap_width = bm.bmWidth;
502 arrow_bitmap_height = bm.bmHeight;
503 } else
504 return FALSE;
505
506 if (! (hBitmap = CreateBitmap( 8, 8, 1, 1, shade_bits)))
507 return FALSE;
508
509 if(!(hShadeBrush = CreatePatternBrush( hBitmap )))
510 return FALSE;
511
512 DeleteObject( hBitmap );
513 if (!(MENU_DefSysPopup = MENU_CopySysPopup()))
514 return FALSE;
515
516 ncm.cbSize = sizeof (NONCLIENTMETRICSA);
517 if (!(SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSA), &ncm, 0)))
518 return FALSE;
519
520 if (!(hMenuFont = CreateFontIndirectA( &ncm.lfMenuFont )))
521 return FALSE;
522
523 ncm.lfMenuFont.lfWeight += 300;
524 if ( ncm.lfMenuFont.lfWeight > 1000)
525 ncm.lfMenuFont.lfWeight = 1000;
526
527 if (!(hMenuFontBold = CreateFontIndirectA( &ncm.lfMenuFont )))
528 return FALSE;
529
530 return TRUE;
531}
532
533/***********************************************************************
534 * MENU_InitSysMenuPopup
535 *
536 * Grey the appropriate items in System menu.
537 */
538static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
539{
540 BOOL gray;
541
542 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
543 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
544 gray = ((style & WS_MAXIMIZE) != 0);
545 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
546 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
547 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
548 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
549 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
550 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
551 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
552 gray = (clsStyle & CS_NOCLOSE) != 0;
553
554 /* The menu item must keep its state if it's disabled */
555 if(gray)
556 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
557}
558
559
560/******************************************************************************
561 *
562 * UINT32 MENU_GetStartOfNextColumn(
563 * HMENU32 hMenu )
564 *
565 *****************************************************************************/
566
567static UINT MENU_GetStartOfNextColumn(
568 HMENU hMenu )
569{
570#ifdef __WIN32OS2__
571 POPUPMENU *menu = MENU_GetMenu(hMenu);
572#else
573 POPUPMENU *menu = (POPUPMENU*)hMenu;
574#endif
575 UINT i = menu->FocusedItem + 1;
576
577 if(!menu)
578 return NO_SELECTED_ITEM;
579
580 if( i == NO_SELECTED_ITEM )
581 return i;
582
583 for( ; i < menu->nItems; ++i ) {
584 if (menu->items[i].fType & MF_MENUBARBREAK)
585 return i;
586 }
587
588 return NO_SELECTED_ITEM;
589}
590
591
592/******************************************************************************
593 *
594 * UINT32 MENU_GetStartOfPrevColumn(
595 * HMENU32 hMenu )
596 *
597 *****************************************************************************/
598
599static UINT MENU_GetStartOfPrevColumn(
600 HMENU hMenu )
601{
602#ifdef __WIN32OS2__
603 POPUPMENU const *menu = MENU_GetMenu(hMenu);
604#else
605 POPUPMENU const *menu = (POPUPMENU*)hMenu;
606#endif
607 UINT i;
608
609 if( !menu )
610 return NO_SELECTED_ITEM;
611
612 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
613 return NO_SELECTED_ITEM;
614
615 /* Find the start of the column */
616
617 for(i = menu->FocusedItem; i != 0 &&
618 !(menu->items[i].fType & MF_MENUBARBREAK);
619 --i); /* empty */
620
621 if(i == 0)
622 return NO_SELECTED_ITEM;
623
624 for(--i; i != 0; --i) {
625 if (menu->items[i].fType & MF_MENUBARBREAK)
626 break;
627 }
628
629 //TRACE("ret %d.\n", i );
630
631 return i;
632}
633
634
635
636/***********************************************************************
637 * MENU_FindItem
638 *
639 * Find a menu item. Return a pointer on the item, and modifies *hmenu
640 * in case the item was in a sub-menu.
641 */
642static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
643{
644 POPUPMENU *menu;
645 UINT i;
646
647 if (((*hmenu)==0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
648 if (!menu) return NULL;
649 if (wFlags & MF_BYPOSITION)
650 {
651 if (*nPos >= menu->nItems) return NULL;
652 return &menu->items[*nPos];
653 }
654 else
655 {
656 MENUITEM *item = menu->items;
657
658 for (i = 0; i < menu->nItems; i++, item++)
659 {
660 if (item->wID == *nPos)
661 {
662 *nPos = i;
663 return item;
664 }
665 else if (item->fType & MF_POPUP)
666 {
667 HMENU hsubmenu = item->hSubMenu;
668 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
669 if (subitem)
670 {
671 *hmenu = hsubmenu;
672 return subitem;
673 }
674 }
675 }
676 }
677 return NULL;
678}
679
680/***********************************************************************
681 * MENU_FindSubMenu
682 *
683 * Find a Sub menu. Return the position of the submenu, and modifies
684 * *hmenu in case it is found in another sub-menu.
685 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
686 */
687UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
688{
689 POPUPMENU *menu;
690 UINT i;
691 MENUITEM *item;
692 if (((*hmenu)==0xffff) ||
693 (!(menu = MENU_GetMenu(*hmenu))))
694 return NO_SELECTED_ITEM;
695 item = menu->items;
696 for (i = 0; i < menu->nItems; i++, item++) {
697 if(!(item->fType & MF_POPUP)) continue;
698 if (item->hSubMenu == hSubTarget) {
699 return i;
700 }
701 else {
702 HMENU hsubmenu = item->hSubMenu;
703 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
704 if (pos != NO_SELECTED_ITEM) {
705 *hmenu = hsubmenu;
706 return pos;
707 }
708 }
709 }
710 return NO_SELECTED_ITEM;
711}
712
713/***********************************************************************
714 * MENU_FreeItemData
715 */
716static void MENU_FreeItemData( MENUITEM* item )
717{
718 /* delete text */
719 if (IS_STRING_ITEM(item->fType) && item->text)
720 HeapFree(GetProcessHeap(), 0, item->text );
721}
722
723/***********************************************************************
724 * MENU_FindItemByCoords
725 *
726 * Find the item at the specified coordinates (screen coords). Does
727 * not work for child windows and therefore should not be called for
728 * an arbitrary system menu.
729 */
730static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu,
731 POINT pt, UINT *pos )
732{
733 MENUITEM *item;
734 UINT i;
735 RECT wrect;
736
737 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
738 pt.x -= wrect.left;pt.y -= wrect.top;
739 item = menu->items;
740 for (i = 0; i < menu->nItems; i++, item++)
741 {
742 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
743 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
744 {
745 if (pos) *pos = i;
746 return item;
747 }
748 }
749 return NULL;
750}
751
752
753/***********************************************************************
754 * MENU_FindItemByKey
755 *
756 * Find the menu item selected by a key press.
757 * Return item id, -1 if none, -2 if we should close the menu.
758 */
759static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
760 UINT key, BOOL forceMenuChar )
761{
762 //TRACE("\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
763
764 if (!IsMenu( hmenu ))
765 {
766 hmenu = GetSubMenu(getSysMenu(hwndOwner), 0);
767 }
768
769 if (hmenu)
770 {
771 POPUPMENU *menu = MENU_GetMenu(hmenu);
772 MENUITEM *item = menu->items;
773 LONG menuchar;
774
775 if( !forceMenuChar )
776 {
777 UINT i;
778
779 key = toupper(key);
780 for (i = 0; i < menu->nItems; i++, item++)
781 {
782 if (item->text && (IS_STRING_ITEM(item->fType)))
783 {
784 char *p = item->text - 2;
785 do
786 {
787 p = strchr (p + 2, '&');
788 }
789 while (p != NULL && p [1] == '&');
790 if (p && (toupper(p[1]) == key)) return i;
791 }
792 }
793 }
794 menuchar = SendMessageA( hwndOwner, WM_MENUCHAR,
795 MAKEWPARAM( key, menu->wFlags ), hmenu );
796 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
797 if (HIWORD(menuchar) == 1) return (UINT)(-2);
798 }
799 return (UINT)(-1);
800}
801/***********************************************************************
802 * MENU_LoadMagicItem
803 *
804 * Load the bitmap associated with the magic menu item and its style
805 */
806
807static HBITMAP MENU_LoadMagicItem(UINT id, BOOL hilite, DWORD dwItemData)
808{
809 /*
810 * Magic menu item id's section
811 * These magic id's are used by windows to insert "standard" mdi
812 * buttons (minimize,restore,close) on menu. Under windows,
813 * these magic id's make sure the right things appear when those
814 * bitmap buttons are pressed/selected/released.
815 */
816
817 switch(id & 0xffff)
818 { case HBMMENU_SYSTEM:
819 return (dwItemData) ?
820 (HBITMAP)dwItemData :
821 (hilite ? hBmpMinimizeD : hBmpMinimize);
822 case HBMMENU_MBAR_RESTORE:
823 return (hilite ? hBmpMaximizeD: hBmpMaximize);
824 case HBMMENU_MBAR_MINIMIZE:
825 return (hilite ? hBmpMinimizeD : hBmpMinimize);
826 case HBMMENU_MBAR_CLOSE:
827 return (hilite ? hBmpCloseD : hBmpClose);
828 case HBMMENU_CALLBACK:
829 case HBMMENU_MBAR_CLOSE_D:
830 case HBMMENU_MBAR_MINIMIZE_D:
831 case HBMMENU_POPUP_CLOSE:
832 case HBMMENU_POPUP_RESTORE:
833 case HBMMENU_POPUP_MAXIMIZE:
834 case HBMMENU_POPUP_MINIMIZE:
835 default:
836 //FIXME("Magic 0x%08x not implemented\n", id);
837 return 0;
838 }
839
840}
841
842/***********************************************************************
843 * MENU_CalcItemSize
844 *
845 * Calculate the size of the menu item and store it in lpitem->rect.
846 */
847static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
848 INT orgX, INT orgY, BOOL menuBar )
849{
850 char *p;
851
852 //TRACE("dc=0x%04x owner=0x%04x (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
853 //debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
854 // (menuBar ? " (MenuBar)" : ""));
855
856 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
857
858 if (lpitem->fType & MF_OWNERDRAW)
859 {
860 MEASUREITEMSTRUCT mis;
861 mis.CtlType = ODT_MENU;
862 mis.CtlID = 0;
863 mis.itemID = lpitem->wID;
864 mis.itemData = (DWORD)lpitem->dwItemData;
865 mis.itemHeight = 0;
866 mis.itemWidth = 0;
867 SendMessageA( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
868 lpitem->rect.bottom += mis.itemHeight;
869 lpitem->rect.right += mis.itemWidth;
870
871 //SvL: Add size of space between two menu items (fixes RealPlayer 7 menu)
872 if(menuBar)
873 {
874 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
875 }
876
877 //TRACE("id=%04x size=%dx%d\n",
878 // lpitem->wID, mis.itemWidth, mis.itemHeight);
879 return;
880 }
881
882 if (lpitem->fType & MF_SEPARATOR)
883 {
884 lpitem->rect.bottom += SEPARATOR_HEIGHT;
885 return;
886 }
887
888 if (!menuBar)
889 {
890 lpitem->rect.right += 2 * check_bitmap_width;
891 if (lpitem->fType & MF_POPUP)
892 lpitem->rect.right += arrow_bitmap_width;
893 }
894
895 if (IS_BITMAP_ITEM(lpitem->fType))
896 {
897 BITMAP bm;
898 HBITMAP resBmp = 0;
899
900 /* Check if there is a magic menu item associated with this item */
901 if((LOWORD((int)lpitem->text))<12)
902 {
903 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fType & MF_HILITE),
904 lpitem->dwItemData);
905 }
906 else
907 resBmp = (HBITMAP)lpitem->text;
908
909 if (GetObjectA(resBmp, sizeof(bm), &bm ))
910 {
911 lpitem->rect.right += bm.bmWidth;
912 lpitem->rect.bottom += bm.bmHeight;
913
914 }
915 }
916
917
918 /* it must be a text item - unless it's the system menu */
919 if (!(lpitem->fType & MF_SYSMENU) && IS_STRING_ITEM( lpitem->fType ))
920 { SIZE size;
921
922 GetTextExtentPoint32A(hdc, lpitem->text, strlen(lpitem->text), &size);
923
924 lpitem->rect.right += size.cx;
925 lpitem->rect.bottom += MAX (size.cy, GetSystemMetrics(SM_CYMENU)-1);
926 lpitem->xTab = 0;
927
928 if (menuBar)
929 {
930 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
931 }
932 else if ((p = strchr( lpitem->text, '\t' )) != NULL)
933 {
934 /* Item contains a tab (only meaningful in popup menus) */
935 GetTextExtentPoint32A(hdc, lpitem->text, (int)(p - lpitem->text) , &size);
936 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + size.cx;
937 lpitem->rect.right += MENU_TAB_SPACE;
938 }
939 else
940 {
941 if (strchr( lpitem->text, '\b' ))
942 lpitem->rect.right += MENU_TAB_SPACE;
943 lpitem->xTab = lpitem->rect.right - check_bitmap_width
944 - arrow_bitmap_width;
945 }
946 }
947 //dprintf(("MENU_CalcItemSize %x (%d,%d)-(%d,%d)", lpitem->wID, lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom));
948 //TRACE("(%d,%d)-(%d,%d)\n", lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom);
949}
950
951
952/***********************************************************************
953 * MENU_PopupMenuCalcSize
954 *
955 * Calculate the size of a popup menu.
956 */
957static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
958{
959 MENUITEM *lpitem;
960 HDC hdc;
961 int start, i;
962 int orgX, orgY, maxX, maxTab, maxTabWidth;
963
964 lppop->Width = lppop->Height = 0;
965 if (lppop->nItems == 0) return;
966#ifdef __WIN32OS2__
967 hdc = CreateCompatibleDC( 0 );
968#else
969 hdc = GetDC( 0 );
970#endif
971
972 SelectObject( hdc, hMenuFont);
973
974 start = 0;
975 maxX = 2 ;
976
977 while (start < lppop->nItems)
978 {
979 lpitem = &lppop->items[start];
980 orgX = maxX;
981 orgY = 2;
982
983 maxTab = maxTabWidth = 0;
984
985 /* Parse items until column break or end of menu */
986 for (i = start; i < lppop->nItems; i++, lpitem++)
987 {
988 if ((i != start) &&
989 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
990
991 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
992
993 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
994 maxX = MAX( maxX, lpitem->rect.right );
995 orgY = lpitem->rect.bottom;
996 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
997 {
998 maxTab = MAX( maxTab, lpitem->xTab );
999 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
1000 }
1001 }
1002
1003 /* Finish the column (set all items to the largest width found) */
1004 maxX = MAX( maxX, maxTab + maxTabWidth );
1005 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
1006 {
1007 lpitem->rect.right = maxX;
1008 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1009 lpitem->xTab = maxTab;
1010
1011 }
1012 lppop->Height = MAX( lppop->Height, orgY );
1013 }
1014
1015 lppop->Width = maxX;
1016
1017 /* space for 3d border */
1018 lppop->Height += 2;
1019 lppop->Width += 2;
1020
1021#ifdef __WIN32OS2__
1022 DeleteDC(hdc);
1023#else
1024 ReleaseDC( 0, hdc );
1025#endif
1026}
1027
1028
1029/***********************************************************************
1030 * MENU_MenuBarCalcSize
1031 *
1032 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
1033 * height is off by 1 pixel which causes lengthy window relocations when
1034 * active document window is maximized/restored.
1035 *
1036 * Calculate the size of the menu bar.
1037 */
1038static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
1039 LPPOPUPMENU lppop, HWND hwndOwner )
1040{
1041 MENUITEM *lpitem;
1042 int start, i, orgX, orgY, maxY, helpPos;
1043
1044 if ((lprect == NULL) || (lppop == NULL)) return;
1045 if (lppop->nItems == 0) return;
1046 //TRACE("left=%d top=%d right=%d bottom=%d\n",
1047 // lprect->left, lprect->top, lprect->right, lprect->bottom);
1048 lppop->Width = lprect->right - lprect->left;
1049 lppop->Height = 0;
1050 maxY = lprect->top;
1051 start = 0;
1052 helpPos = -1;
1053 while (start < lppop->nItems)
1054 {
1055 lpitem = &lppop->items[start];
1056 orgX = lprect->left;
1057 orgY = maxY;
1058
1059 /* Parse items until line break or end of menu */
1060 for (i = start; i < lppop->nItems; i++, lpitem++)
1061 {
1062 if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
1063 if ((i != start) &&
1064 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1065
1066 //TRACE("calling MENU_CalcItemSize org=(%d, %d)\n",
1067 // orgX, orgY );
1068 //debug_print_menuitem (" item: ", lpitem, "");
1069 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
1070
1071 if (lpitem->rect.right > lprect->right)
1072 {
1073 if (i != start) break;
1074 else lpitem->rect.right = lprect->right;
1075 }
1076 maxY = MAX( maxY, lpitem->rect.bottom );
1077 orgX = lpitem->rect.right;
1078 }
1079
1080 /* Finish the line (set all items to the largest height found) */
1081 while (start < i) lppop->items[start++].rect.bottom = maxY;
1082 }
1083
1084 lprect->bottom = maxY;
1085 lppop->Height = lprect->bottom - lprect->top;
1086
1087 /* Flush right all magic items and items between the MF_HELP and */
1088 /* the last item (if several lines, only move the last line) */
1089 lpitem = &lppop->items[lppop->nItems-1];
1090 orgY = lpitem->rect.top;
1091 orgX = lprect->right;
1092 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
1093 {
1094 if ( !IS_BITMAP_ITEM(lpitem->fType) && ((helpPos ==-1) ? TRUE : (helpPos>i) ))
1095 break; /* done */
1096 if (lpitem->rect.top != orgY) break; /* Other line */
1097 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1098 lpitem->rect.left += orgX - lpitem->rect.right;
1099 lpitem->rect.right = orgX;
1100 orgX = lpitem->rect.left;
1101 }
1102}
1103
1104/***********************************************************************
1105 * MENU_DrawMenuItem
1106 *
1107 * Draw a single menu item.
1108 */
1109static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1110 UINT height, BOOL menuBar, UINT odaction )
1111{
1112 RECT rect;
1113
1114 //debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1115
1116 if (lpitem->fType & MF_SYSMENU) return;
1117
1118 if (lpitem->fType & MF_OWNERDRAW)
1119 {
1120 DRAWITEMSTRUCT dis;
1121
1122 dis.CtlType = ODT_MENU;
1123 dis.CtlID = 0;
1124 dis.itemID = lpitem->wID;
1125 dis.itemData = (DWORD)lpitem->dwItemData;
1126 dis.itemState = 0;
1127 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1128 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED;
1129 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1130 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1131 dis.hwndItem = hmenu;
1132 dis.hDC = hdc;
1133 dis.rcItem = lpitem->rect;
1134 //TRACE("Ownerdraw: owner=%04x itemID=%d, itemState=%d, itemAction=%d, "
1135 // "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n", hwndOwner,
1136 // dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1137 // dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
1138 // dis.rcItem.bottom);
1139 SendMessageA( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1140 return;
1141 }
1142
1143 //TRACE("rect={%d,%d,%d,%d}\n", lpitem->rect.left, lpitem->rect.top,
1144 // lpitem->rect.right,lpitem->rect.bottom);
1145
1146 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1147
1148 rect = lpitem->rect;
1149
1150 //CB: todo: does Win98 use DrawEdge for menubars?
1151
1152 if ((lpitem->fState & MF_HILITE) && !(IS_BITMAP_ITEM(lpitem->fType)))
1153 FillRect( hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT) );
1154 else {
1155 //SvL: TODO: Bug in GDI32; draws black rectangle instead of menu color
1156 /// for everything except the 1st menu item
1157 RECT dummy = rect;
1158
1159 FillRect( hdc, &dummy, GetSysColorBrush(COLOR_HIGHLIGHT) );
1160 FillRect( hdc, &rect, GetSysColorBrush(COLOR_MENU) ); //CB: back color switching bug?
1161 }
1162
1163 SetBkMode( hdc, TRANSPARENT );
1164
1165 /* vertical separator */
1166 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1167 {
1168 RECT rc = rect;
1169 rc.top = 3;
1170 rc.bottom = height - 3;
1171 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1172 }
1173
1174 /* horizontal separator */
1175 if (lpitem->fType & MF_SEPARATOR)
1176 {
1177 RECT rc = rect;
1178 rc.left++;
1179 rc.right--;
1180 rc.top += SEPARATOR_HEIGHT / 2;
1181 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1182 return;
1183 }
1184
1185 /* Setup colors */
1186
1187 if ((lpitem->fState & MF_HILITE) && !(IS_BITMAP_ITEM(lpitem->fType)) )
1188 {
1189 if (lpitem->fState & MF_GRAYED)
1190 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1191#if 1 //CB: WINE's Win98 menubar -> to check
1192
1193 else
1194 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
1195#else
1196 else
1197 {
1198 if (menuBar)
1199 SetTextColor(hdc,GetSysColor(COLOR_MENUTEXT));
1200 else
1201 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
1202 }
1203#endif
1204 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
1205 }
1206 else
1207 {
1208 if (lpitem->fState & MF_GRAYED)
1209 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1210 else
1211 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1212 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
1213 }
1214
1215 /* helper lines for debugging */
1216/* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1217 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1218 MoveTo( hdc, rect.left, (rect.top + rect.bottom)/2,NULL);
1219 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1220*/
1221
1222 if (!menuBar)
1223 {
1224 INT y = rect.top + rect.bottom;
1225
1226 /* Draw the check mark
1227 *
1228 * FIXME:
1229 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1230 */
1231
1232 if (lpitem->fState & MF_CHECKED)
1233 {
1234 HBITMAP bm = lpitem->hCheckBit ? lpitem->hCheckBit :
1235 ((lpitem->fType & MFT_RADIOCHECK) ? hStdRadioCheck : hStdCheck);
1236 HDC hdcMem = CreateCompatibleDC( hdc );
1237
1238 SelectObject( hdcMem, bm );
1239 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1240 check_bitmap_width, check_bitmap_height,
1241 hdcMem, 0, 0, (lpitem->fState & MF_HILITE) ? MERGEPAINT : SRCAND );
1242 DeleteDC( hdcMem );
1243 }
1244 else if (lpitem->hUnCheckBit)
1245 {
1246 HDC hdcMem = CreateCompatibleDC( hdc );
1247
1248 SelectObject( hdcMem, lpitem->hUnCheckBit );
1249 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1250 check_bitmap_width, check_bitmap_height,
1251 hdcMem, 0, 0, (lpitem->fState & MF_HILITE) ? MERGEPAINT : SRCAND );
1252 DeleteDC( hdcMem );
1253 }
1254
1255 /* Draw the popup-menu arrow */
1256 if (lpitem->fType & MF_POPUP)
1257 {
1258 HDC hdcMem = CreateCompatibleDC( hdc );
1259
1260 SelectObject( hdcMem, hStdMnArrow );
1261 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1262 (y - arrow_bitmap_height) / 2,
1263 arrow_bitmap_width, arrow_bitmap_height,
1264 hdcMem, 0, 0, (lpitem->fState & MF_HILITE) ? MERGEPAINT : SRCAND );
1265 DeleteDC( hdcMem );
1266 }
1267
1268 rect.left += check_bitmap_width;
1269 rect.right -= arrow_bitmap_width;
1270 }
1271
1272 /* Draw the item text or bitmap */
1273 if (IS_BITMAP_ITEM(lpitem->fType))
1274 { int top;
1275
1276 HBITMAP resBmp = 0;
1277
1278 HDC hdcMem = CreateCompatibleDC( hdc );
1279
1280 /*
1281 * Check if there is a magic menu item associated with this item
1282 * and load the appropriate bitmap
1283 */
1284 if((LOWORD((int)lpitem->text)) < 12)
1285 {
1286 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fState & MF_HILITE),
1287 lpitem->dwItemData);
1288 }
1289 else
1290 resBmp = (HBITMAP)lpitem->text;
1291
1292 if (resBmp)
1293 {
1294 BITMAP bm;
1295 GetObjectA( resBmp, sizeof(bm), &bm );
1296
1297 SelectObject(hdcMem,resBmp );
1298
1299 /* handle fontsize > bitmap_height */
1300 top = ((rect.bottom-rect.top)>bm.bmHeight) ?
1301 rect.top+(rect.bottom-rect.top-bm.bmHeight)/2 : rect.top;
1302
1303 BitBlt( hdc, rect.left, top, rect.right - rect.left,
1304 rect.bottom - rect.top, hdcMem, 0, 0, SRCCOPY );
1305 }
1306 DeleteDC( hdcMem );
1307
1308 return;
1309
1310 }
1311 /* No bitmap - process text if present */
1312 else if (IS_STRING_ITEM(lpitem->fType))
1313 {
1314 register int i;
1315 HFONT hfontOld = 0;
1316
1317 UINT uFormat = (menuBar) ?
1318 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1319 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1320
1321 if ( lpitem->fState & MFS_DEFAULT )
1322 {
1323 hfontOld = SelectObject( hdc, hMenuFontBold);
1324 }
1325
1326 if (menuBar)
1327 {
1328 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1329 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1330 i = strlen( lpitem->text );
1331 }
1332 else
1333 {
1334 for (i = 0; lpitem->text[i]; i++)
1335 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1336 break;
1337 }
1338
1339 if(lpitem->fState & MF_GRAYED)
1340 {
1341 if (!(lpitem->fState & MF_HILITE) )
1342 {
1343 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1344 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1345 DrawTextA( hdc, lpitem->text, i, &rect, uFormat );
1346 --rect.left; --rect.top; --rect.right; --rect.bottom;
1347 }
1348 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1349 }
1350
1351 DrawTextA( hdc, lpitem->text, i, &rect, uFormat);
1352
1353 /* paint the shortcut text */
1354 if (lpitem->text[i]) /* There's a tab or flush-right char */
1355 {
1356 if (lpitem->text[i] == '\t')
1357 {
1358 rect.left = lpitem->xTab;
1359 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1360 }
1361 else
1362 {
1363 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1364 }
1365
1366 if(lpitem->fState & MF_GRAYED)
1367 {
1368 if (!(lpitem->fState & MF_HILITE) )
1369 {
1370 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1371 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1372 DrawTextA( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1373 --rect.left; --rect.top; --rect.right; --rect.bottom;
1374 }
1375 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1376 }
1377 DrawTextA( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1378 }
1379
1380 if (hfontOld)
1381 SelectObject (hdc, hfontOld);
1382 }
1383}
1384
1385
1386/***********************************************************************
1387 * MENU_DrawPopupMenu
1388 *
1389 * Paint a popup menu.
1390 */
1391static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1392{
1393 HBRUSH hPrevBrush = 0;
1394 RECT rect;
1395
1396 //TRACE("wnd=0x%04x dc=0x%04x menu=0x%04x\n", hwnd, hdc, hmenu);
1397
1398 GetClientRect( hwnd, &rect );
1399
1400 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1401 && (SelectObject( hdc, hMenuFont)))
1402 {
1403 HPEN hPrevPen;
1404
1405 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1406
1407 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1408 if( hPrevPen )
1409 {
1410 INT ropPrev, i;
1411 POPUPMENU *menu;
1412
1413 /* draw 3-d shade */
1414 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1415
1416 /* draw menu items */
1417
1418 menu = MENU_GetMenu(hmenu);
1419 if (menu && menu->nItems)
1420 {
1421 MENUITEM *item;
1422 UINT u;
1423
1424 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1425 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc, item,
1426 menu->Height, FALSE, ODA_DRAWENTIRE );
1427
1428 }
1429 } else
1430 {
1431 SelectObject( hdc, hPrevBrush );
1432 }
1433 }
1434}
1435
1436/***********************************************************************
1437 * MENU_DrawMenuBar
1438 *
1439 * Paint a menu bar. Returns the height of the menu bar.
1440 * called from [windows/nonclient.c]
1441 */
1442UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1443 BOOL suppress_draw)
1444{
1445 LPPOPUPMENU lppop;
1446 UINT i,retvalue;
1447 HFONT hfontOld = 0;
1448
1449 lppop = MENU_GetMenu(getMenu(hwnd));
1450 if (lppop == NULL || lprect == NULL)
1451 {
1452 retvalue = GetSystemMetrics(SM_CYMENU);
1453 goto END;
1454 }
1455
1456 //TRACE("(%04x, %p, %p)\n", hDC, lprect, lppop);
1457
1458 hfontOld = SelectObject( hDC, hMenuFont);
1459
1460 if (lppop->Height == 0)
1461 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1462
1463 lprect->bottom = lprect->top + lppop->Height;
1464
1465 if (suppress_draw)
1466 {
1467 retvalue = lppop->Height;
1468 goto END;
1469 }
1470
1471 HDC memDC;
1472 HBITMAP memBmp,oldBmp;
1473 RECT r;
1474 HFONT oldMemFont;
1475
1476 memDC = CreateCompatibleDC(hDC);
1477 r = *lprect;
1478 r.right -= r.left;
1479 r.bottom -= r.top;
1480 r.left = r.top = 0;
1481 memBmp = CreateCompatibleBitmap(hDC,r.right,r.bottom+1);
1482 oldBmp = SelectObject(memDC,memBmp);
1483 oldMemFont = SelectObject(memDC,hMenuFont);
1484
1485 FillRect(memDC,&r,GetSysColorBrush(COLOR_MENU));
1486
1487 SelectObject(memDC,GetSysColorPen(COLOR_3DFACE));
1488 MoveToEx(memDC,r.left,r.bottom,NULL);
1489 LineTo(memDC,r.right,r.bottom);
1490
1491 if (lppop->nItems == 0)
1492 {
1493 retvalue = GetSystemMetrics(SM_CYMENU);
1494 } else
1495 {
1496 for (i = 0; i < lppop->nItems; i++)
1497 {
1498 OffsetRect(&lppop->items[i].rect,-lprect->left,-lprect->top);
1499 MENU_DrawMenuItem( hwnd,getMenu(hwnd), GetWindow(hwnd,GW_OWNER),
1500 memDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
1501 OffsetRect(&lppop->items[i].rect,lprect->left,lprect->top);
1502 }
1503 retvalue = lppop->Height;
1504 }
1505
1506 BitBlt(hDC,lprect->left,lprect->top,lprect->right-lprect->left,lprect->bottom-lprect->top+1,memDC,0,0,SRCCOPY);
1507 SelectObject(memDC,oldBmp);
1508 if (oldMemFont) SelectObject(memDC,oldMemFont);
1509 DeleteObject(memBmp);
1510 DeleteDC(memDC);
1511
1512END:
1513 if (hfontOld)
1514 SelectObject (hDC, hfontOld);
1515
1516 return retvalue;
1517}
1518
1519/***********************************************************************
1520 * MENU_PatchResidentPopup
1521 */
1522BOOL MENU_PatchResidentPopup( HQUEUE checkQueue, HWND checkWnd )
1523{
1524 HWND pTPWnd = MENU_GetTopPopupWnd();
1525#if 0 //CB: todo
1526 if( pTPWnd )
1527 {
1528 HTASK hTask = 0;
1529
1530 //TRACE("patching resident popup: %04x %04x [%04x %04x]\n",
1531 // checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTPWnd->hmemTaskQ,
1532 // pTPWnd->owner ? pTPWnd->owner->hwndSelf : 0);
1533
1534 switch( checkQueue )
1535 {
1536 case 0: /* checkWnd is the new popup owner */
1537 if( checkWnd )
1538 {
1539 pTPWnd->owner = checkWnd;
1540 if( pTPWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1541 hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1542 }
1543 break;
1544
1545 case 0xFFFF: /* checkWnd is destroyed */
1546 if( pTPWnd->owner == checkWnd )
1547 pTPWnd->owner = NULL;
1548 MENU_ReleaseTopPopupWnd();
1549 return TRUE;
1550
1551 default: /* checkQueue is exiting */
1552 if( pTPWnd->hmemTaskQ == checkQueue )
1553 {
1554 hTask = QUEUE_GetQueueTask( pTPWnd->hmemTaskQ );
1555 hTask = TASK_GetNextTask( hTask );
1556 }
1557 break;
1558 }
1559
1560 if( hTask )
1561 {
1562 TDB* task = (TDB*)GlobalLock( hTask );
1563 if( task )
1564 {
1565 pTPWnd->hInstance = task->hInstance;
1566 pTPWnd->hmemTaskQ = task->hQueue;
1567 MENU_ReleaseTopPopupWnd();
1568 return TRUE;
1569 }
1570 //else WARN("failed to patch resident popup.\n");
1571 }
1572 }
1573#endif
1574 MENU_ReleaseTopPopupWnd();
1575 return FALSE;
1576}
1577
1578/***********************************************************************
1579 * MENU_ShowPopup
1580 *
1581 * Display a popup menu.
1582 */
1583static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1584 INT x, INT y, INT xanchor, INT yanchor )
1585{
1586 POPUPMENU *menu;
1587
1588 //TRACE("owner=0x%04x hmenu=0x%04x id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1589 //hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1590
1591 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
1592 if (menu->FocusedItem != NO_SELECTED_ITEM)
1593 {
1594 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1595 menu->FocusedItem = NO_SELECTED_ITEM;
1596 }
1597
1598 /* store the owner for DrawItem*/
1599 menu->hwndOwner = hwndOwner;
1600
1601 if(IsWindow(hwndOwner))
1602 {
1603 UINT width, height;
1604
1605 MENU_PopupMenuCalcSize( menu, hwndOwner );
1606
1607 /* adjust popup menu pos so that it fits within the desktop */
1608
1609 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1610 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1611
1612 if( x + width > GetSystemMetrics(SM_CXSCREEN ))
1613 {
1614 if( xanchor )
1615 x -= width - xanchor;
1616 if( x + width > GetSystemMetrics(SM_CXSCREEN))
1617 x = GetSystemMetrics(SM_CXSCREEN) - width;
1618 }
1619 if( x < 0 ) x = 0;
1620
1621 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1622 {
1623 if( yanchor )
1624 y -= height + yanchor;
1625 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1626 y = GetSystemMetrics(SM_CYSCREEN) - height;
1627 }
1628 if( y < 0 ) y = 0;
1629
1630 /* NOTE: In Windows, top menu popup is not owned. */
1631 if (!pTopPopupWnd) /* create top level popup menu window */
1632 {
1633 assert( uSubPWndLevel == 0 );
1634
1635 pTopPopupWnd = CreateWindowA( POPUPMENUCLASSNAME, NULL,
1636 WS_POPUP, x, y, width, height,
1637 hwndOwner, 0, GetWindowLongA(hwndOwner,GWL_HINSTANCE),
1638 (LPVOID)hmenu );
1639 if (!pTopPopupWnd)
1640 {
1641 DebugInt3();
1642 return FALSE;
1643 }
1644 menu->hWnd = pTopPopupWnd;
1645 MENU_ReleaseTopPopupWnd();
1646 }
1647 else
1648 if( uSubPWndLevel )
1649 {
1650 /* create a new window for the submenu */
1651
1652 menu->hWnd = CreateWindowA( POPUPMENUCLASSNAME, NULL,
1653 WS_POPUP, x, y, width, height,
1654 hwndOwner, 0, GetWindowLongA(hwndOwner,GWL_HINSTANCE),
1655 (LPVOID)hmenu );
1656 if( !menu->hWnd )
1657 {
1658 DebugInt3();
1659 return FALSE;
1660 }
1661 }
1662 else /* top level popup menu window already exists */
1663 {
1664 HWND pTPWnd = MENU_GetTopPopupWnd();
1665 menu->hWnd = pTPWnd;
1666
1667 MENU_PatchResidentPopup( 0, hwndOwner );
1668 SendMessageA( pTPWnd, MM_SETMENUHANDLE, (WPARAM)hmenu, 0L);
1669
1670 /* adjust its size */
1671
1672 SetWindowPos( menu->hWnd, 0, x, y, width, height,
1673 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1674 MENU_ReleaseTopPopupWnd();
1675 }
1676
1677 uSubPWndLevel++; /* menu level counter */
1678
1679 /* Display the window */
1680
1681 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1682 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1683 EnableWindow(menu->hWnd,TRUE);
1684 UpdateWindow( menu->hWnd );
1685 return TRUE;
1686 }
1687 return FALSE;
1688}
1689
1690
1691/***********************************************************************
1692 * MENU_SelectItem
1693 */
1694static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1695 BOOL sendMenuSelect, HMENU topmenu )
1696{
1697 LPPOPUPMENU lppop;
1698 HDC hdc;
1699
1700 //TRACE("owner=0x%04x menu=0x%04x index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1701
1702 lppop = MENU_GetMenu(hmenu);
1703 if ((!lppop) || (!lppop->nItems)) return;
1704
1705 if (lppop->FocusedItem == wIndex) return;
1706 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1707 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1708
1709 SelectObject( hdc, hMenuFont);
1710
1711 /* Clear previous highlighted item */
1712 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1713 {
1714 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1715 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1716 lppop->Height, !(lppop->wFlags & MF_POPUP),
1717 ODA_SELECT );
1718 }
1719
1720 /* Highlight new item (if any) */
1721 lppop->FocusedItem = wIndex;
1722 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1723 {
1724 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1725 lppop->items[wIndex].fState |= MF_HILITE;
1726 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1727 &lppop->items[wIndex], lppop->Height,
1728 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1729 }
1730 if (sendMenuSelect)
1731 {
1732 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1733 SendMessageA( hwndOwner, WM_MENUSELECT,
1734 MAKELONG(ip->fType & MF_POPUP ? wIndex: ip->wID,
1735 ip->fType | ip->fState | MF_MOUSESELECT |
1736 (lppop->wFlags & MF_SYSMENU)), hmenu);
1737 }
1738 }
1739 else if (sendMenuSelect) {
1740 if(topmenu){
1741 int pos;
1742 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1743#ifdef __WIN32OS2__
1744 POPUPMENU *ptm = MENU_GetMenu(topmenu);
1745#else
1746 POPUPMENU *ptm = (POPUPMENU*)topmenu;
1747#endif
1748 MENUITEM *ip = &ptm->items[pos];
1749 SendMessageA( hwndOwner, WM_MENUSELECT, MAKELONG(pos,
1750 ip->fType | ip->fState | MF_MOUSESELECT |
1751 (ptm->wFlags & MF_SYSMENU)), topmenu);
1752 }
1753 }
1754 }
1755 ReleaseDC( lppop->hWnd, hdc );
1756}
1757
1758
1759/***********************************************************************
1760 * MENU_MoveSelection
1761 *
1762 * Moves currently selected item according to the offset parameter.
1763 * If there is no selection then it should select the last item if
1764 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1765 */
1766static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1767{
1768 INT i;
1769 POPUPMENU *menu;
1770
1771 //TRACE("hwnd=0x%04x hmenu=0x%04x off=0x%04x\n", hwndOwner, hmenu, offset);
1772
1773 menu = MENU_GetMenu(hmenu);
1774 if ((!menu) || (!menu->items)) return;
1775
1776 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1777 {
1778 if( menu->nItems == 1 ) return; else
1779 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1780 ; i += offset)
1781 if (!(menu->items[i].fType & MF_SEPARATOR))
1782 {
1783 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1784 return;
1785 }
1786 }
1787
1788 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1789 i >= 0 && i < menu->nItems ; i += offset)
1790 if (!(menu->items[i].fType & MF_SEPARATOR))
1791 {
1792 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1793 return;
1794 }
1795}
1796
1797
1798/**********************************************************************
1799 * MENU_SetItemData
1800 *
1801 * Set an item flags, id and text ptr. Called by InsertMenu() and
1802 * ModifyMenu().
1803 */
1804static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT id,
1805 LPCSTR str )
1806{
1807 LPSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1808
1809 //debug_print_menuitem("MENU_SetItemData from: ", item, "");
1810
1811
1812 if (IS_STRING_ITEM(flags))
1813 {
1814 if (!str || !*str)
1815 {
1816 flags |= MF_SEPARATOR;
1817 item->text = NULL;
1818 }
1819 else
1820 {
1821 LPSTR text;
1822 /* Item beginning with a backspace is a help item */
1823 if (*str == '\b')
1824 {
1825 flags |= MF_HELP;
1826 str++;
1827 }
1828 if (!(text = HEAP_strdupA(GetProcessHeap(), 0, str ))) return FALSE;
1829 item->text = text;
1830 }
1831 }
1832 else if (IS_BITMAP_ITEM(flags))
1833#ifdef __WIN32OS2__
1834 //SvL: Our bitmap handles are 32 bits!
1835 item->text = (LPSTR)str;
1836#else
1837 item->text = (LPSTR)(HBITMAP)LOWORD(str);
1838#endif
1839 else item->text = NULL;
1840
1841 if (flags & MF_OWNERDRAW)
1842 item->dwItemData = (DWORD)str;
1843 else
1844 item->dwItemData = 0;
1845
1846 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1847 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1848
1849 if (flags & MF_POPUP)
1850 {
1851 POPUPMENU *menu = MENU_GetMenu((UINT)id);
1852 if (IS_A_MENU(menu)) menu->wFlags |= MF_POPUP;
1853 else
1854 {
1855 item->wID = 0;
1856 item->hSubMenu = 0;
1857 item->fType = 0;
1858 item->fState = 0;
1859 return FALSE;
1860 }
1861 }
1862
1863 item->wID = id;
1864 if (flags & MF_POPUP)
1865 item->hSubMenu = id;
1866
1867 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1868 flags |= MF_POPUP; /* keep popup */
1869
1870 item->fType = flags & TYPE_MASK;
1871 item->fState = (flags & STATE_MASK) &
1872 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1873
1874
1875 /* Don't call SetRectEmpty here! */
1876
1877
1878 if (prevText) HeapFree(GetProcessHeap(), 0, prevText );
1879
1880 //debug_print_menuitem("MENU_SetItemData to : ", item, "");
1881 return TRUE;
1882}
1883
1884
1885/**********************************************************************
1886 * MENU_InsertItem
1887 *
1888 * Insert a new item into a menu.
1889 */
1890static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1891{
1892 MENUITEM *newItems;
1893 POPUPMENU *menu;
1894
1895 if (!(menu = MENU_GetMenu(hMenu)))
1896 return NULL;
1897
1898 /* Find where to insert new item */
1899 if (flags & MF_BYPOSITION) {
1900 if (pos > menu->nItems)
1901 pos = menu->nItems;
1902 } else {
1903 if (!MENU_FindItem( &hMenu, &pos, flags ))
1904 pos = menu->nItems;
1905 else {
1906 if (!(menu = MENU_GetMenu( hMenu )))
1907 return NULL;
1908 }
1909 }
1910 /* Create new items array */
1911
1912 newItems = (MENUITEM*)HeapAlloc(GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
1913 if (!newItems)
1914 {
1915 //WARN("allocation failed\n" );
1916 return NULL;
1917 }
1918 if (menu->nItems > 0)
1919 {
1920 /* Copy the old array into the new */
1921 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1922 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1923 (menu->nItems-pos)*sizeof(MENUITEM) );
1924 HeapFree(GetProcessHeap(), 0, menu->items );
1925 }
1926 menu->items = newItems;
1927 menu->nItems++;
1928 memset( &newItems[pos], 0, sizeof(*newItems) );
1929 menu->Height = 0; /* force size recalculate */
1930 return &newItems[pos];
1931}
1932
1933
1934/**********************************************************************
1935 * MENU_ParseResource
1936 *
1937 * Parse a standard menu resource and add items to the menu.
1938 * Return a pointer to the end of the resource.
1939 */
1940static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1941{
1942 WORD flags, id = 0;
1943 LPCSTR str;
1944
1945 do
1946 {
1947 flags = GET_WORD(res);
1948 res += sizeof(WORD);
1949 if (!(flags & MF_POPUP))
1950 {
1951 id = GET_WORD(res);
1952 res += sizeof(WORD);
1953 }
1954 //if (!IS_STRING_ITEM(flags))
1955 // ERR("not a string item %04x\n", flags );
1956 str = res;
1957 if (!unicode) res += strlen(str) + 1;
1958 else res += (lstrlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1959 if (flags & MF_POPUP)
1960 {
1961 HMENU hSubMenu = CreatePopupMenu();
1962 if (!hSubMenu) return NULL;
1963 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1964 return NULL;
1965 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1966 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1967 }
1968 else /* Not a popup */
1969 {
1970 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1971 else AppendMenuW( hMenu, flags, id,
1972 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1973 }
1974 } while (!(flags & MF_END));
1975 return res;
1976}
1977
1978
1979/**********************************************************************
1980 * MENUEX_ParseResource
1981 *
1982 * Parse an extended menu resource and add items to the menu.
1983 * Return a pointer to the end of the resource.
1984 */
1985static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1986{
1987 WORD resinfo;
1988 do {
1989 MENUITEMINFOW mii;
1990
1991 mii.cbSize = sizeof(mii);
1992 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1993 mii.fType = GET_DWORD(res);
1994 res += sizeof(DWORD);
1995 mii.fState = GET_DWORD(res);
1996 res += sizeof(DWORD);
1997 mii.wID = GET_DWORD(res);
1998 res += sizeof(DWORD);
1999 resinfo = GET_WORD(res);
2000 res += sizeof(WORD);
2001 /* Align the text on a word boundary. */
2002 res += (~((int)res - 1)) & 1;
2003 mii.dwTypeData = (LPWSTR) res;
2004 res += (1 + lstrlenW(mii.dwTypeData)) * sizeof(WCHAR);
2005 /* Align the following fields on a dword boundary. */
2006 res += (~((int)res - 1)) & 3;
2007
2008 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
2009 {
2010 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
2011 0, mii.dwTypeData);
2012 //TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
2013 // mii.fType, mii.fState, mii.wID, resinfo, newstr);
2014 HeapFree( GetProcessHeap(), 0, newstr );
2015 }
2016
2017 if (resinfo & 1) { /* Pop-up? */
2018 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
2019 res += sizeof(DWORD);
2020 mii.hSubMenu = CreatePopupMenu();
2021 if (!mii.hSubMenu)
2022 return NULL;
2023 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
2024 DestroyMenu(mii.hSubMenu);
2025 return NULL;
2026 }
2027 mii.fMask |= MIIM_SUBMENU;
2028 mii.fType |= MF_POPUP;
2029 }
2030 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2031 } while (!(resinfo & MF_END));
2032 return res;
2033}
2034
2035
2036/***********************************************************************
2037 * MENU_GetSubPopup
2038 *
2039 * Return the handle of the selected sub-popup menu (if any).
2040 */
2041static HMENU MENU_GetSubPopup( HMENU hmenu )
2042{
2043 POPUPMENU *menu;
2044 MENUITEM *item;
2045
2046 menu = MENU_GetMenu(hmenu);
2047
2048 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2049
2050 item = &menu->items[menu->FocusedItem];
2051 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2052 return item->hSubMenu;
2053 return 0;
2054}
2055
2056
2057/***********************************************************************
2058 * MENU_HideSubPopups
2059 *
2060 * Hide the sub-popup menus of this menu.
2061 */
2062static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2063 BOOL sendMenuSelect )
2064{
2065 POPUPMENU *menu = MENU_GetMenu(hmenu);
2066
2067 //TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2068
2069 if (menu && uSubPWndLevel)
2070 {
2071 HMENU hsubmenu;
2072 POPUPMENU *submenu;
2073 MENUITEM *item;
2074
2075 if (menu->FocusedItem != NO_SELECTED_ITEM)
2076 {
2077 item = &menu->items[menu->FocusedItem];
2078 if (!(item->fType & MF_POPUP) ||
2079 !(item->fState & MF_MOUSESELECT)) return;
2080 item->fState &= ~MF_MOUSESELECT;
2081 hsubmenu = item->hSubMenu;
2082 } else return;
2083
2084 submenu = MENU_GetMenu(hsubmenu);
2085 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2086 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2087
2088 if (submenu->hWnd == MENU_GetTopPopupWnd() )
2089 {
2090 DestroyWindow( submenu->hWnd );
2091 submenu->hWnd = 0;
2092// ShowWindow( submenu->hWnd, SW_HIDE );
2093 uSubPWndLevel = 0;
2094 }
2095 else
2096 {
2097 DestroyWindow( submenu->hWnd );
2098 submenu->hWnd = 0;
2099 }
2100 MENU_ReleaseTopPopupWnd();
2101 }
2102}
2103
2104
2105/***********************************************************************
2106 * MENU_ShowSubPopup
2107 *
2108 * Display the sub-menu of the selected item of this menu.
2109 * Return the handle of the submenu, or hmenu if no submenu to display.
2110 */
2111static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2112 BOOL selectFirst, UINT wFlags,POINT *pt)
2113{
2114 RECT rect;
2115 POPUPMENU *menu;
2116 MENUITEM *item;
2117 HDC hdc;
2118
2119 //TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, selectFirst);
2120
2121 if (!(menu = MENU_GetMenu(hmenu))) return hmenu;
2122
2123 if (menu->FocusedItem == NO_SELECTED_ITEM)
2124 {
2125 return hmenu;
2126 }
2127
2128 item = &menu->items[menu->FocusedItem];
2129 if (!(item->fType & MF_POPUP) ||
2130 (item->fState & (MF_GRAYED | MF_DISABLED)))
2131 {
2132 return hmenu;
2133 }
2134
2135 /* message must be send before using item,
2136 because nearly everything may by changed by the application ! */
2137
2138 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2139 if (!(wFlags & TPM_NONOTIFY))
2140 SendMessageA( hwndOwner, WM_INITMENUPOPUP, item->hSubMenu,
2141 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2142
2143 item = &menu->items[menu->FocusedItem];
2144 rect = item->rect;
2145
2146 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
2147 if (!(item->fState & MF_HILITE))
2148 {
2149 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2150 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2151
2152 SelectObject( hdc, hMenuFont);
2153
2154 item->fState |= MF_HILITE;
2155 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2156 ReleaseDC( menu->hWnd, hdc );
2157 }
2158 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2159 item->rect = rect;
2160
2161 item->fState |= MF_MOUSESELECT;
2162
2163 if (IS_SYSTEM_MENU(menu))
2164 {
2165 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(menu->hWnd);
2166
2167 MENU_InitSysMenuPopup(item->hSubMenu,GetWindowLongA(menu->hWnd,GWL_STYLE), GetClassLongA(menu->hWnd, GCL_STYLE));
2168
2169 if ((wFlags & TPM_CAPTIONSYSMENU) && pt)
2170 {
2171 rect.top = pt->y;
2172 rect.left = pt->x;
2173 rect.bottom = rect.right = 0;
2174 } else
2175 {
2176 if (win32wnd) win32wnd->GetSysPopupPos(&rect);
2177 rect.top = rect.bottom;
2178 rect.right = GetSystemMetrics(SM_CXSIZE);
2179 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2180 }
2181 if(win32wnd) RELEASE_WNDOBJ(win32wnd);
2182 }
2183 else
2184 {
2185 if (menu->wFlags & MF_POPUP)
2186 {
2187 RECT rectWindow;
2188
2189 GetWindowRect(menu->hWnd,&rectWindow);
2190 rect.left = rectWindow.left + item->rect.right;
2191 rect.top = rectWindow.top + item->rect.top;
2192 rect.right = item->rect.left - item->rect.right;
2193 rect.bottom = item->rect.top - item->rect.bottom;
2194 }
2195 else
2196 {
2197 RECT rectWindow;
2198
2199 GetWindowRect(menu->hWnd,&rectWindow);
2200 rect.left = rectWindow.left + item->rect.left;
2201 rect.top = rectWindow.top + item->rect.bottom;
2202 rect.right = item->rect.right - item->rect.left;
2203 rect.bottom = item->rect.bottom - item->rect.top;
2204 }
2205 }
2206
2207 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2208 rect.left, rect.top, rect.right, rect.bottom );
2209 if (selectFirst)
2210 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2211 return item->hSubMenu;
2212}
2213
2214/***********************************************************************
2215 * MENU_PtMenu
2216 *
2217 * Walks menu chain trying to find a menu pt maps to.
2218 */
2219static HMENU MENU_PtMenu(HMENU hMenu,POINT pt,BOOL inMenuBar)
2220{
2221 POPUPMENU *menu = MENU_GetMenu(hMenu);
2222 register UINT ht = menu->FocusedItem;
2223
2224 /* try subpopup first (if any) */
2225 ht = (ht != NO_SELECTED_ITEM &&
2226 (menu->items[ht].fType & MF_POPUP) &&
2227 (menu->items[ht].fState & MF_MOUSESELECT))
2228 ? (UINT) MENU_PtMenu(menu->items[ht].hSubMenu,pt,inMenuBar) : 0;
2229
2230 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
2231 {
2232 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(menu->hWnd);
2233 if(win32wnd==NULL) {
2234 //SvL: This happens in Moraff's YourJongg 2.0, return here
2235 //TODO: Check if this is supposed to happen at all...
2236 return (HMENU)0;
2237 }
2238
2239 ht = win32wnd->HandleNCHitTest(pt);
2240 RELEASE_WNDOBJ(win32wnd);
2241 if( menu->wFlags & MF_POPUP )
2242 ht = (ht != (UINT)HTNOWHERE &&
2243 ht != (UINT)HTERROR) ? (UINT)hMenu : 0;
2244 else
2245 {
2246 ht = ((ht == HTSYSMENU) && !inMenuBar) ? (UINT)(getSysMenu(menu->hWnd))
2247 : ((ht == HTMENU) && inMenuBar) ? (UINT)(getMenu(menu->hWnd)) : 0;
2248 }
2249 }
2250 return (HMENU)ht;
2251}
2252
2253/***********************************************************************
2254 * MENU_ExecFocusedItem
2255 *
2256 * Execute a menu item (for instance when user pressed Enter).
2257 * Return the wID of the executed item. Otherwise, -1 indicating
2258 * that no menu item wase executed;
2259 * Have to receive the flags for the TrackPopupMenu options to avoid
2260 * sending unwanted message.
2261 *
2262 */
2263static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2264{
2265 MENUITEM *item;
2266 POPUPMENU *menu = MENU_GetMenu(hMenu);
2267
2268 //TRACE("%p hmenu=0x%04x\n", pmt, hMenu);
2269
2270 if (!menu || !menu->nItems ||
2271 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2272
2273 item = &menu->items[menu->FocusedItem];
2274
2275 //TRACE("%08x %08x %08x\n",
2276 // hMenu, item->wID, item->hSubMenu);
2277
2278 if (!(item->fType & MF_POPUP))
2279 {
2280 if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
2281 {
2282 /* If TPM_RETURNCMD is set you return the id, but
2283 do not send a message to the owner */
2284 if(!(wFlags & TPM_RETURNCMD))
2285 {
2286 if( menu->wFlags & MF_SYSMENU )
2287 PostMessageA( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2288 MAKELPARAM(pmt->pt.x, pmt->pt.y) );
2289 else
2290 PostMessageA( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2291 }
2292 return item->wID;
2293 }
2294 }
2295 else
2296 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags,&pmt->pt);
2297
2298 return -1;
2299}
2300
2301/***********************************************************************
2302 * MENU_SwitchTracking
2303 *
2304 * Helper function for menu navigation routines.
2305 */
2306static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2307{
2308 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2309 POPUPMENU *topmenu = MENU_GetMenu(pmt->hTopMenu);
2310
2311 //TRACE("%p hmenu=0x%04x 0x%04x\n", pmt, hPtMenu, id);
2312
2313 if( pmt->hTopMenu != hPtMenu &&
2314 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2315 {
2316 /* both are top level menus (system and menu-bar) */
2317 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2318 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2319 pmt->hTopMenu = hPtMenu;
2320 }
2321 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2322 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2323}
2324
2325
2326/***********************************************************************
2327 * MENU_ButtonDown
2328 *
2329 * Return TRUE if we can go on with menu tracking.
2330 */
2331static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2332{
2333 //TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2334
2335 if (hPtMenu)
2336 {
2337 UINT id = 0;
2338 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2339 MENUITEM *item;
2340
2341 if( IS_SYSTEM_MENU(ptmenu) )
2342 item = ptmenu->items;
2343 else
2344 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2345
2346 if( item )
2347 {
2348 if( ptmenu->FocusedItem != id )
2349 MENU_SwitchTracking( pmt, hPtMenu, id );
2350
2351 /* If the popup menu is not already "popped" */
2352 if(!(item->fState & MF_MOUSESELECT ))
2353 {
2354 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,hPtMenu,FALSE,wFlags,&pmt->pt);
2355
2356#ifdef __WIN32OS2__
2357 if(fOS2Look)
2358 ptmenu->bTimeToHide = FALSE;
2359#else
2360 /* In win31, a newly popped menu always remains opened for the next buttonup */
2361 if(TWEAK_WineLook == WIN31_LOOK)
2362 ptmenu->bTimeToHide = FALSE;
2363#endif
2364 }
2365
2366 return TRUE;
2367 }
2368 /* Else the click was on the menu bar, finish the tracking */
2369 }
2370 return FALSE;
2371}
2372
2373/***********************************************************************
2374 * MENU_ButtonUp
2375 *
2376 * Return the value of MENU_ExecFocusedItem if
2377 * the selected item was not a popup. Else open the popup.
2378 * A -1 return value indicates that we go on with menu tracking.
2379 *
2380 */
2381static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2382{
2383 //TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2384
2385 if (hPtMenu)
2386 {
2387 UINT id = 0;
2388 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2389 MENUITEM *item;
2390
2391 if( IS_SYSTEM_MENU(ptmenu) )
2392 item = ptmenu->items;
2393 else
2394 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2395
2396 if( item && (ptmenu->FocusedItem == id ))
2397 {
2398 if( !(item->fType & MF_POPUP) )
2399 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2400
2401 /* If we are dealing with the top-level menu and that this */
2402 /* is a click on an already "poppped" item */
2403 /* Stop the menu tracking and close the opened submenus */
2404 if((pmt->hTopMenu == hPtMenu) && (ptmenu->bTimeToHide == TRUE))
2405 return 0;
2406 }
2407 ptmenu->bTimeToHide = TRUE;
2408 }
2409 return -1;
2410}
2411
2412
2413/***********************************************************************
2414 * MENU_MouseMove
2415 *
2416 * Return TRUE if we can go on with menu tracking.
2417 */
2418static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2419{
2420 UINT id = NO_SELECTED_ITEM;
2421 POPUPMENU *ptmenu = NULL;
2422
2423 if( hPtMenu )
2424 {
2425 ptmenu = MENU_GetMenu(hPtMenu);
2426 if( IS_SYSTEM_MENU(ptmenu) )
2427 id = 0;
2428 else
2429 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2430 }
2431
2432 if( id == NO_SELECTED_ITEM )
2433 {
2434 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2435 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2436
2437 }
2438 else if( ptmenu->FocusedItem != id )
2439 {
2440 POPUPMENU *menu;
2441 MENUITEM *item;
2442
2443 MENU_SwitchTracking( pmt, hPtMenu, id );
2444
2445
2446 /*
2447 Test to see if we are trying to popup a submenu or not.
2448 If we aren't, don't change the current menu pointer
2449 and return.
2450 */
2451 if (!(menu = (POPUPMENU *)MENU_GetMenu( hPtMenu )))
2452 {
2453 pmt->hCurrentMenu = hPtMenu;
2454 return TRUE;
2455 }
2456
2457 if (!IsWindow( menu->hWnd ) ||
2458 (menu->FocusedItem == NO_SELECTED_ITEM))
2459 {
2460 pmt->hCurrentMenu = hPtMenu;
2461 return TRUE;
2462 }
2463
2464 item = &menu->items[menu->FocusedItem];
2465 if (!(item->fType & MF_POPUP) ||
2466 (item->fState & (MF_GRAYED | MF_DISABLED)))
2467 {
2468 pmt->hCurrentMenu = hPtMenu;
2469 return TRUE;
2470 }
2471
2472 /* Check to see if we are trying to popup a toplevel menu or a
2473 submenu. Only the submenu has a delay.
2474 */
2475 if (uSubPWndLevel)
2476 {
2477 /*
2478 If we made it here, we want to pop up a submenu. Before we pop it up,
2479 we want a slight delay. This is implemented by remembering the ID of the menu
2480 where the mouse is currently positioned, and setting up a timer. When the
2481 timer fires (handled in MENU_TrackMenu() ), if the mouse is over the same
2482 submenu item, we popup it up. Otherwise, we do nothing.
2483 */
2484 KillTimer (pmt->hOwnerWnd, SUBMENU_POPUP_TIMERID); /* Just in case another timer was set up and not fired yet... */
2485 if ( (SetTimer (pmt->hOwnerWnd, SUBMENU_POPUP_TIMERID, POPUP_MENU_DELAY, NULL)) != SUBMENU_POPUP_TIMERID)
2486 {
2487 /*
2488 For some reason the timer wasn't set up properly... Revert to old
2489 functionality.
2490 */
2491 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,hPtMenu,FALSE,wFlags,&pmt->pt);
2492 return TRUE;
2493 }
2494 } else
2495 {
2496 /* We are trying to popup a top level menu... so no delay */
2497
2498 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags,&pmt->pt);
2499 return TRUE;
2500 }
2501
2502 mouseOverMenuID = id;
2503 isTimerSet = TRUE;
2504 }
2505 return TRUE;
2506}
2507
2508
2509/***********************************************************************
2510 * MENU_DoNextMenu
2511 *
2512 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2513 */
2514static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2515{
2516 POPUPMENU *menu = MENU_GetMenu(pmt->hTopMenu);
2517
2518 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2519 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2520 {
2521 MDINEXTMENU next_menu;
2522 HMENU hNewMenu;
2523 HWND hNewWnd;
2524 UINT id = 0;
2525
2526 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2527 next_menu.hmenuNext = 0;
2528 next_menu.hwndNext = 0;
2529
2530 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2531
2532 dprintf(("%04x [%04x] -> %04x [%04x]\n",
2533 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext ));
2534
2535 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2536 {
2537 hNewWnd = pmt->hOwnerWnd;
2538 if( IS_SYSTEM_MENU(menu) )
2539 {
2540 /* switch to the menu bar */
2541
2542 if( (GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_CHILD) || !getMenu(pmt->hOwnerWnd) )
2543 {
2544 return FALSE;
2545 }
2546
2547 hNewMenu = getMenu(pmt->hOwnerWnd);
2548 if( vk == VK_LEFT )
2549 {
2550 menu = MENU_GetMenu(hNewMenu);
2551 id = menu->nItems - 1;
2552 }
2553 }
2554 else if( GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_SYSMENU )
2555 {
2556 /* switch to the system menu */
2557 hNewMenu = getSysMenu(pmt->hOwnerWnd);
2558 }
2559 else
2560 {
2561 return FALSE;
2562 }
2563 }
2564 else /* application returned a new menu to switch to */
2565 {
2566 hNewMenu = next_menu.hmenuNext;
2567 hNewWnd = next_menu.hwndNext;
2568
2569 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2570 {
2571 if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_SYSMENU) &&
2572 GetSubMenu(getSysMenu(hNewWnd), 0) == hNewMenu )
2573 {
2574 /* get the real system menu */
2575 hNewMenu = getSysMenu(hNewWnd);
2576 }
2577 else if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_CHILD) || (getMenu(hNewWnd) != hNewMenu) )
2578 {
2579 /* FIXME: Not sure what to do here, perhaps,
2580 * try to track hNewMenu as a popup? */
2581
2582 //TRACE(" -- got confused.\n");
2583 return FALSE;
2584 }
2585 }
2586 else return FALSE;
2587 }
2588
2589 if( hNewMenu != pmt->hTopMenu )
2590 {
2591 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2592 FALSE, 0 );
2593 if( pmt->hCurrentMenu != pmt->hTopMenu )
2594 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2595 }
2596
2597 if( hNewWnd != pmt->hOwnerWnd )
2598 {
2599 ReleaseCapture();
2600 pmt->hOwnerWnd = hNewWnd;
2601 SetCapture(pmt->hOwnerWnd); //SvL: Don't know if this is good enough
2602 //EVENT_Capture( pmt->hOwnerWnd, HTMENU ); //CB: todo
2603 }
2604
2605 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2606 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2607
2608 return TRUE;
2609 }
2610 return FALSE;
2611}
2612
2613/***********************************************************************
2614 * MENU_SuspendPopup
2615 *
2616 * The idea is not to show the popup if the next input message is
2617 * going to hide it anyway.
2618 */
2619static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT uMsg )
2620{
2621 MSG msg;
2622
2623 msg.hwnd = pmt->hOwnerWnd;
2624
2625 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2626 pmt->trackFlags |= TF_SKIPREMOVE;
2627
2628 switch( uMsg )
2629 {
2630 case WM_KEYDOWN:
2631 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2632 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2633 {
2634 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2635 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2636 if( msg.message == WM_KEYDOWN &&
2637 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2638 {
2639 pmt->trackFlags |= TF_SUSPENDPOPUP;
2640 return TRUE;
2641 }
2642 }
2643 break;
2644 }
2645
2646 /* failures go through this */
2647 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2648 return FALSE;
2649}
2650
2651/***********************************************************************
2652 * MENU_KeyLeft
2653 *
2654 * Handle a VK_LEFT key event in a menu.
2655 */
2656static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2657{
2658 POPUPMENU *menu;
2659 HMENU hmenutmp, hmenuprev;
2660 UINT prevcol;
2661
2662 hmenuprev = hmenutmp = pmt->hTopMenu;
2663 menu = MENU_GetMenu(hmenutmp);
2664
2665 /* Try to move 1 column left (if possible) */
2666 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2667 NO_SELECTED_ITEM ) {
2668
2669 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2670 prevcol, TRUE, 0 );
2671 return;
2672 }
2673
2674 /* close topmost popup */
2675 while (hmenutmp != pmt->hCurrentMenu)
2676 {
2677 hmenuprev = hmenutmp;
2678 hmenutmp = MENU_GetSubPopup( hmenuprev );
2679 }
2680
2681 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2682 pmt->hCurrentMenu = hmenuprev;
2683
2684 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2685 {
2686 /* move menu bar selection if no more popups are left */
2687
2688 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2689 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2690
2691 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2692 {
2693 /* A sublevel menu was displayed - display the next one
2694 * unless there is another displacement coming up */
2695
2696 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2697 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2698 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2699 }
2700 }
2701}
2702
2703
2704/***********************************************************************
2705 * MENU_KeyRight
2706 *
2707 * Handle a VK_RIGHT key event in a menu.
2708 */
2709static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2710{
2711 HMENU hmenutmp;
2712 POPUPMENU *menu = MENU_GetMenu(pmt->hTopMenu);
2713 UINT nextcol;
2714
2715 //TRACE("MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2716 // pmt->hCurrentMenu,
2717 // ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2718 // items[0].text,
2719 // pmt->hTopMenu, menu->items[0].text );
2720
2721 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2722 {
2723 /* If already displaying a popup, try to display sub-popup */
2724
2725 hmenutmp = pmt->hCurrentMenu;
2726 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags,&pmt->pt);
2727
2728 /* if subpopup was displayed then we are done */
2729 if (hmenutmp != pmt->hCurrentMenu) return;
2730 }
2731
2732 /* Check to see if there's another column */
2733 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2734 NO_SELECTED_ITEM ) {
2735 //TRACE("Going to %d.\n", nextcol );
2736 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2737 nextcol, TRUE, 0 );
2738 return;
2739 }
2740
2741 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2742 {
2743 if( pmt->hCurrentMenu != pmt->hTopMenu )
2744 {
2745 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2746 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2747 } else hmenutmp = 0;
2748
2749 /* try to move to the next item */
2750 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2751 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2752
2753 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2754 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2755 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2756 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2757 }
2758}
2759
2760VOID MENU_DispatchMouseMsg(MSG *msg)
2761{
2762 LONG hittest;
2763
2764 hittest = SendMessageA(msg->hwnd,WM_NCHITTEST,0,MAKELONG(msg->pt.x,msg->pt.y));
2765 if (hittest != HTCLIENT)
2766 SendMessageA(msg->hwnd,msg->message+WM_NCMOUSEMOVE-WM_MOUSEMOVE,hittest,MAKELONG(msg->pt.x,msg->pt.y));
2767 else
2768 DispatchMessageA(msg);
2769}
2770
2771/***********************************************************************
2772 * MENU_TrackMenu
2773 *
2774 * Menu tracking code.
2775 */
2776static INT MENU_TrackMenu(HMENU hmenu,UINT wFlags,INT x,INT y,HWND hwnd,BOOL inMenuBar,const RECT *lprect)
2777{
2778 MSG msg;
2779 POPUPMENU *menu;
2780 BOOL fRemove;
2781 INT executedMenuId = -1;
2782 MTRACKER mt;
2783 BOOL enterIdleSent = FALSE;
2784 BOOL bSysMenu;
2785
2786 mt.trackFlags = 0;
2787 mt.hCurrentMenu = hmenu;
2788 mt.hTopMenu = hmenu;
2789 mt.hOwnerWnd = hwnd;
2790 mt.pt.x = x;
2791 mt.pt.y = y;
2792
2793 //TRACE("hmenu=0x%04x flags=0x%08x (%d,%d) hwnd=0x%04x (%d,%d)-(%d,%d)\n",
2794 // hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2795 // (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2796
2797 fEndMenu = FALSE;
2798 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
2799
2800 bSysMenu = IS_SYSTEM_MENU(menu);
2801
2802 if (wFlags & TPM_BUTTONDOWN)
2803 {
2804 /* Get the result in order to start the tracking or not */
2805 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2806 fEndMenu = !fRemove;
2807 }
2808
2809 //EVENT_Capture( mt.hOwnerWnd, HTMENU ); //CB: todo
2810 //SvL: Set keyboard & mouse event capture
2811 SetCapture(mt.hOwnerWnd);
2812
2813 while (!fEndMenu)
2814 {
2815 menu = MENU_GetMenu(mt.hCurrentMenu);
2816 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2817
2818 /* we have to keep the message in the queue until it's
2819 * clear that menu loop is not over yet. */
2820// if (!GetMessageA(&msg,msg.hwnd,0,0)) break;
2821 //SvL: Getting messages for only the menu delays background paints (i.e. VPBuddy logo)
2822 if (!GetMessageA(&msg,0,0,0)) break;
2823 TranslateMessage( &msg );
2824 mt.pt = msg.pt;
2825
2826 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2827 enterIdleSent=FALSE;
2828
2829 fRemove = FALSE;
2830 if((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2831 {
2832 /* Find a menu for this mouse event */
2833 POINT pt = msg.pt;
2834
2835 hmenu = MENU_PtMenu(mt.hTopMenu,pt,inMenuBar);
2836
2837 //CB: todo: solve conflicts with OS/2's single message queue architecture
2838
2839 switch(msg.message)
2840 {
2841 /* no WM_NC... messages in captured state */
2842
2843 case WM_RBUTTONDBLCLK:
2844 case WM_RBUTTONDOWN:
2845 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2846 goto buttondown;
2847 case WM_LBUTTONDBLCLK:
2848 if (bSysMenu && (hmenu == mt.hTopMenu))
2849 {
2850 fEndMenu = TRUE;
2851 break;
2852 }
2853 /* fall through */
2854 case WM_LBUTTONDOWN:
2855 /* If the message belongs to the menu, removes it from the queue */
2856 /* Else, end menu tracking */
2857
2858 buttondown:
2859 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2860 mouseOverMenuID = -1;
2861 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2862 fEndMenu = !fRemove;
2863 break;
2864
2865 case WM_RBUTTONUP:
2866 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2867 /* fall through */
2868 case WM_LBUTTONUP:
2869 /* Check if a menu was selected by the mouse */
2870 if (hmenu)
2871 {
2872 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2873 mouseOverMenuID = -1;
2874 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2875
2876 /* End the loop if executedMenuId is an item ID */
2877 /* or if the job was done (executedMenuId = 0). */
2878 fEndMenu = fRemove = (executedMenuId != -1);
2879 }
2880 /* No menu was selected by the mouse */
2881 /* if the function was called by TrackPopupMenu, continue
2882 with the menu tracking. If not, stop it */
2883 else
2884 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2885
2886 break;
2887
2888 case WM_MOUSEMOVE:
2889 /* In win95 winelook, the selected menu item must be changed every time the
2890 mouse moves. In Win31 winelook, the mouse button has to be held down */
2891#ifdef __WIN32OS2__
2892 if ( !fOS2Look ||
2893 ( (msg.wParam & MK_LBUTTON) ||
2894 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2895 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2896#else
2897 if ( (TWEAK_WineLook > WIN31_LOOK) ||
2898 ( (msg.wParam & MK_LBUTTON) ||
2899 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2900 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2901#endif
2902
2903 } /* switch(msg.message) - mouse */
2904 }
2905 else if (msg.message == WM_TIMER)
2906 {
2907 UINT id = -1;
2908 POPUPMENU *ptmenu = NULL;
2909
2910 if (isTimerSet)
2911 {
2912 /*
2913 If we get here, an attempt was made to pop up a submenu.
2914 (See MENU_MouseMove() )
2915 */
2916
2917 /* Get the ID of the menu item the mouse is over now. */
2918 if( hmenu )
2919 {
2920 ptmenu = (POPUPMENU *)MENU_GetMenu( hmenu );
2921 if( IS_SYSTEM_MENU(ptmenu) )
2922 id = 0;
2923 else
2924 MENU_FindItemByCoords( ptmenu, mt.pt, &id );
2925
2926 /* If it is over the same item that set up the timer originally .... */
2927 if (mouseOverMenuID != -1 && mouseOverMenuID == id)
2928 {
2929 /* .... Pop up the menu */
2930 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, hmenu, FALSE, wFlags,&mt.pt);
2931 }
2932 }
2933
2934 /* Reset the timer so it doesn't fire again. (So we are ready for the next
2935 attempt to popup a submenu... ) */
2936 KillTimer(mt.hOwnerWnd,SUBMENU_POPUP_TIMERID);
2937 isTimerSet = FALSE;
2938 }
2939 }
2940 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2941 {
2942 fRemove = TRUE; /* Keyboard messages are always removed */
2943 switch(msg.message)
2944 {
2945 case WM_KEYDOWN:
2946 switch(msg.wParam)
2947 {
2948 case VK_HOME:
2949 case VK_END:
2950 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2951 NO_SELECTED_ITEM, FALSE, 0 );
2952 /* fall through */
2953 case VK_UP:
2954 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2955 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2956 break;
2957
2958 case VK_DOWN: /* If on menu bar, pull-down the menu */
2959
2960 menu = MENU_GetMenu(mt.hCurrentMenu);
2961 if (!(menu->wFlags & MF_POPUP))
2962 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags,&mt.pt);
2963 else /* otherwise try to move selection */
2964 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2965 break;
2966
2967 case VK_LEFT:
2968 MENU_KeyLeft( &mt, wFlags );
2969 break;
2970
2971 case VK_RIGHT:
2972 MENU_KeyRight( &mt, wFlags );
2973 break;
2974
2975 case VK_ESCAPE:
2976 fEndMenu = TRUE;
2977 break;
2978
2979 case VK_F1:
2980 {
2981 HELPINFO hi;
2982 hi.cbSize = sizeof(HELPINFO);
2983 hi.iContextType = HELPINFO_MENUITEM;
2984 if (menu->FocusedItem == NO_SELECTED_ITEM)
2985 hi.iCtrlId = 0;
2986 else
2987 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2988 hi.hItemHandle = hmenu;
2989 hi.dwContextId = menu->dwContextHelpID;
2990 hi.MousePos = msg.pt;
2991 //TRACE_(winhelp)("Sending HELPINFO_MENUITEM to 0x%08x\n", hwnd);
2992 SendMessageA(hwnd, WM_HELP, 0, (LPARAM)&hi);
2993 break;
2994 }
2995
2996 default:
2997 break;
2998 }
2999 break; /* WM_KEYDOWN */
3000
3001 case WM_SYSKEYDOWN:
3002 switch(msg.wParam)
3003 {
3004 case VK_MENU:
3005 fEndMenu = TRUE;
3006 break;
3007
3008 }
3009 break; /* WM_SYSKEYDOWN */
3010
3011 case WM_CHAR:
3012 {
3013 UINT pos;
3014
3015 if (msg.wParam == '\r' || msg.wParam == ' ')
3016 {
3017 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3018 fEndMenu = (executedMenuId != -1);
3019
3020 break;
3021 }
3022
3023 /* Hack to avoid control chars. */
3024 /* We will find a better way real soon... */
3025 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
3026
3027 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3028 LOWORD(msg.wParam), FALSE );
3029 if (pos == (UINT)-2) fEndMenu = TRUE;
3030 else if (pos == (UINT)-1) MessageBeep(0);
3031 else
3032 {
3033 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3034 TRUE, 0 );
3035 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3036 fEndMenu = (executedMenuId != -1);
3037 }
3038 }
3039 break;
3040 } /* switch(msg.message) - kbd */
3041 }
3042 else if (msg.message == WM_SYSCOMMAND)
3043 {
3044 /* The user clicked on the system menu/button */
3045 fEndMenu = TRUE;
3046 break;
3047 }
3048 else
3049 {
3050 DispatchMessageA( &msg );
3051 }
3052
3053 if (!fEndMenu) fRemove = TRUE;
3054
3055 /* finally remove message from the queue */
3056
3057 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3058 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
3059 else mt.trackFlags &= ~TF_SKIPREMOVE;
3060 }
3061
3062 ReleaseCapture();
3063
3064 menu = MENU_GetMenu(mt.hTopMenu);
3065
3066 if( IsWindow( mt.hOwnerWnd ) )
3067 {
3068 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
3069
3070 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3071#ifdef __WIN32OS2__
3072 if (menu && menu->wFlags & MF_POPUP)
3073 {
3074 DestroyWindow( menu->hWnd );
3075 menu->hWnd = 0;
3076// ShowWindow( menu->hWnd, SW_HIDE );
3077 uSubPWndLevel = 0;
3078 }
3079#endif
3080 SendMessageA( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
3081 }
3082
3083 /* Reset the variable for hiding menu */
3084 menu->bTimeToHide = FALSE;
3085
3086 /* The return value is only used by TrackPopupMenu */
3087 return ((executedMenuId != -1) ? executedMenuId : 0);
3088}
3089
3090/***********************************************************************
3091 * MENU_InitTracking
3092 */
3093static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3094{
3095 //TRACE("hwnd=0x%04x hmenu=0x%04x\n", hWnd, hMenu);
3096
3097 HideCaret(0);
3098
3099 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3100 if (!(wFlags & TPM_NONOTIFY))
3101 SendMessageA( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3102
3103 SendMessageA( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
3104
3105 if (!(wFlags & TPM_NONOTIFY))
3106 SendMessageA( hWnd, WM_INITMENU, hMenu, 0 );
3107
3108 return TRUE;
3109}
3110/***********************************************************************
3111 * MENU_ExitTracking
3112 */
3113static BOOL MENU_ExitTracking(HWND hWnd)
3114{
3115 //TRACE("hwnd=0x%04x\n", hWnd);
3116
3117 SendMessageA( hWnd, WM_EXITMENULOOP, 0, 0 );
3118 ShowCaret(0);
3119 return TRUE;
3120}
3121
3122/***********************************************************************
3123 * MENU_TrackMouseMenuBar
3124 *
3125 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3126 */
3127void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3128{
3129 HMENU hMenu = (ht == 0) ? getMenu(hWnd):getSysMenu(hWnd);
3130 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3131
3132 //TRACE("pwnd=%p ht=0x%04x (%ld,%ld)\n", wndPtr, ht, pt.x, pt.y);
3133
3134 if (IsMenu(hMenu))
3135 {
3136 if (ht == HTCAPTION) wFlags |= TPM_CAPTIONSYSMENU | TPM_RIGHTBUTTON;
3137 if (IsIconic(hWnd)) wFlags |= TPM_BOTTOMALIGN; //CB: todo: for minimized windows
3138
3139 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3140 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd,ht == 0, NULL );
3141 MENU_ExitTracking(hWnd);
3142 }
3143}
3144
3145MENUITEM *MENU_HighlightedItem=NULL;
3146UINT MENU_HighlightedItemID=NO_SELECTED_ITEM;
3147POPUPMENU *MENU_HighlightedMenu=NULL;
3148
3149void MENU_TrackMouseMenuBar_MouseMove(HWND hwnd,POINT pt,BOOL OnMenu)
3150{
3151 HMENU hMenu = getMenu(hwnd);
3152 MENUITEM *item = NULL;
3153 RECT rect;
3154 HDC hdc;
3155 BOOL UnHighlight = (OnMenu==TRUE)?FALSE:TRUE;
3156 POPUPMENU *ptmenu = NULL;
3157 UINT id = NO_SELECTED_ITEM;
3158
3159 if (OnMenu == TRUE && IsMenu(hMenu)) {
3160
3161 ptmenu=(POPUPMENU *)MENU_GetMenu( hMenu );
3162
3163 item=MENU_FindItemByCoords( ptmenu, pt, &id );
3164
3165 if(!item) {
3166 /* No item selected, perhaps we are on the blank spot? */
3167 UnHighlight = TRUE;
3168 /* Paranoid setting */
3169 item=NULL;
3170 } else if(id == MENU_HighlightedItemID) {
3171 /* If it's already highlighted, don't do it again */
3172 return;
3173 } else if(item->fState & MF_MOUSESELECT) {
3174 /* If it's dropped, we let the DrawMenuItem draw the sunken border */
3175 return;
3176 } else if(item->fType & MF_BITMAP) {
3177 UnHighlight = TRUE;
3178 /* (Required) Paranoid setting */
3179 item=NULL;
3180 } else {
3181 hdc = GetDCEx( ptmenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3182 rect = item->rect;
3183 DrawEdge(hdc, &rect, BDR_RAISEDINNER, BF_RECT);
3184 ReleaseDC( ptmenu->hWnd, hdc );
3185
3186 UnHighlight = TRUE;
3187 }
3188 }
3189
3190 if(UnHighlight == TRUE) {
3191 if(MENU_HighlightedItem) {
3192 if(!(MENU_HighlightedItem->fState & MF_MOUSESELECT)) {
3193 hdc = GetDCEx( MENU_HighlightedMenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3194 rect = MENU_HighlightedItem->rect;
3195
3196 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_MENU));
3197 ReleaseDC( MENU_HighlightedMenu->hWnd, hdc );
3198 }
3199 }
3200
3201 /* Sets to NULL, NO_SELECTED_ITEM, NULL, unless it found a new
3202 item, then it will be filled in with the proper values */
3203 MENU_HighlightedItem = item;
3204 MENU_HighlightedItemID = id;
3205 MENU_HighlightedMenu= ptmenu;
3206 }
3207}
3208
3209/***********************************************************************
3210 * MENU_TrackKbdMenuBar
3211 *
3212 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3213 */
3214void MENU_TrackKbdMenuBar( HWND hWnd, UINT wParam, INT vkey)
3215{
3216 UINT uItem = NO_SELECTED_ITEM;
3217 HMENU hTrackMenu;
3218 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3219
3220 /* find window that has a menu */
3221
3222 while(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD)
3223 if( !(hWnd = GetParent(hWnd)) ) return;
3224
3225 /* check if we have to track a system menu */
3226
3227 if( (GetWindowLongA(hWnd,GWL_STYLE) & (WS_CHILD | WS_MINIMIZE)) ||
3228 !getMenu(hWnd) || vkey == VK_SPACE )
3229 {
3230 if( !(GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) ) return;
3231 hTrackMenu = getSysMenu(hWnd);
3232 uItem = 0;
3233 wParam |= HTSYSMENU; /* prevent item lookup */
3234 }
3235 else
3236 hTrackMenu = getMenu(hWnd);
3237
3238 if (IsMenu( hTrackMenu ))
3239 {
3240 MENU_InitTracking( hWnd, hTrackMenu, FALSE, wFlags );
3241
3242 if( vkey && vkey != VK_SPACE )
3243 {
3244 uItem = MENU_FindItemByKey( hWnd, hTrackMenu,
3245 vkey, (wParam & HTSYSMENU) );
3246 if( uItem >= (UINT)(-2) )
3247 {
3248 if( uItem == (UINT)(-1) ) MessageBeep(0);
3249 hTrackMenu = 0;
3250 }
3251 }
3252
3253 if( hTrackMenu )
3254 {
3255 MENU_SelectItem( hWnd, hTrackMenu, uItem, TRUE, 0 );
3256
3257 if( uItem == NO_SELECTED_ITEM )
3258 MENU_MoveSelection( hWnd, hTrackMenu, ITEM_NEXT );
3259 else if( vkey )
3260 PostMessageA( hWnd, WM_KEYDOWN, VK_DOWN, 0L );
3261
3262 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hWnd,TRUE, NULL );
3263 }
3264
3265 MENU_ExitTracking (hWnd);
3266 }
3267}
3268
3269
3270/**********************************************************************
3271 * TrackPopupMenu (USER32.549)
3272 *
3273 * Like the win32 API, the function return the command ID only if the
3274 * flag TPM_RETURNCMD is on.
3275 *
3276 */
3277BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3278 INT nReserved, HWND hWnd, const RECT *lpRect )
3279{
3280 BOOL ret = FALSE;
3281
3282 if(lpRect) {
3283 dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x (%d,%d)(%d,%d)", hMenu, wFlags, x, y, nReserved, hWnd, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom));
3284 }
3285 else dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x lpRect=NULL", hMenu, wFlags, x, y, nReserved, hWnd));
3286
3287 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3288
3289 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3290 if (!(wFlags & TPM_NONOTIFY))
3291 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3292
3293 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3294 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,FALSE, lpRect );
3295 MENU_ExitTracking(hWnd);
3296
3297 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3298 ret = 1;
3299
3300 return ret;
3301}
3302
3303/**********************************************************************
3304 * TrackPopupMenuEx (USER32.550)
3305 */
3306BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3307 HWND hWnd, LPTPMPARAMS lpTpm )
3308{
3309 dprintf(("USER32: TrackPopupMenuEx"));
3310 //FIXME("not fully implemented\n" );
3311 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3312 lpTpm ? &lpTpm->rcExclude : NULL );
3313}
3314
3315/***********************************************************************
3316 * PopupMenuWndProc
3317 *
3318 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3319 */
3320LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3321 LPARAM lParam )
3322{
3323 LRESULT retvalue;
3324
3325 //TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3326 //hwnd, message, wParam, lParam);
3327
3328 switch(message)
3329 {
3330 case WM_CREATE:
3331 {
3332 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3333 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3334 retvalue = 0;
3335 goto END;
3336 }
3337
3338 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3339 retvalue = MA_NOACTIVATE;
3340 goto END;
3341
3342 case WM_PAINT:
3343 {
3344 PAINTSTRUCT ps;
3345 BeginPaint( hwnd, &ps );
3346 MENU_DrawPopupMenu( hwnd, ps.hdc,
3347 (HMENU)GetWindowLongA( hwnd, 0 ) );
3348 EndPaint( hwnd, &ps );
3349 retvalue = 0;
3350 goto END;
3351 }
3352 case WM_ERASEBKGND:
3353 retvalue = 1;
3354 goto END;
3355
3356 case WM_DESTROY:
3357
3358 /* zero out global pointer in case resident popup window
3359 * was somehow destroyed. */
3360
3361 if(MENU_GetTopPopupWnd() )
3362 {
3363 if( hwnd == pTopPopupWnd )
3364 {
3365 //ERR("resident popup destroyed!\n");
3366
3367 MENU_DestroyTopPopupWnd();
3368 uSubPWndLevel = 0;
3369 }
3370 else
3371 uSubPWndLevel--;
3372 MENU_ReleaseTopPopupWnd();
3373 }
3374 break;
3375
3376 case WM_SHOWWINDOW:
3377
3378 if( wParam )
3379 {
3380 //if( !(*(HMENU*)wndPtr->wExtra) )
3381 // ERR("no menu to display\n");
3382 }
3383 else
3384 SetWindowLongA(hwnd,0,0);
3385 break;
3386
3387 case MM_SETMENUHANDLE:
3388
3389 SetWindowLongA(hwnd,0,wParam);
3390 break;
3391
3392 case MM_GETMENUHANDLE:
3393
3394 retvalue = GetWindowLongA(hwnd,0);
3395 goto END;
3396
3397 default:
3398 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3399 goto END;
3400 }
3401 retvalue = 0;
3402END:
3403 return retvalue;
3404}
3405
3406
3407/***********************************************************************
3408 * MENU_GetMenuBarHeight
3409 *
3410 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3411 */
3412UINT MENU_GetMenuBarHeight(HWND hwnd,UINT menubarWidth,INT orgX,INT orgY)
3413{
3414 HDC hdc;
3415 RECT rectBar;
3416 LPPOPUPMENU lppop;
3417 UINT retvalue;
3418
3419 //TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3420 // hwnd, menubarWidth, orgX, orgY );
3421
3422 if (!(lppop = MENU_GetMenu(getMenu(hwnd))))
3423 {
3424 return 0;
3425 }
3426
3427 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3428 SelectObject( hdc, hMenuFont);
3429 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3430 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3431 ReleaseDC( hwnd, hdc );
3432 retvalue = lppop->Height;
3433 return retvalue;
3434}
3435
3436
3437/*******************************************************************
3438 * ChangeMenu32A (USER32.23)
3439 */
3440BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3441 UINT id, UINT flags )
3442{
3443 dprintf(("USER32: ChangeMenuA"));
3444
3445 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3446 // hMenu, pos, (DWORD)data, id, flags );
3447 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3448 id, data );
3449 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3450 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3451 id, data );
3452 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3453 flags & MF_BYPOSITION ? pos : id,
3454 flags & ~MF_REMOVE );
3455 /* Default: MF_INSERT */
3456 return InsertMenuA( hMenu, pos, flags, id, data );
3457}
3458
3459
3460/*******************************************************************
3461 * ChangeMenu32W (USER32.24)
3462 */
3463BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3464 UINT id, UINT flags )
3465{
3466 dprintf(("USER32: ChangeMenuW"));
3467
3468 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3469 // hMenu, pos, (DWORD)data, id, flags );
3470 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3471 id, data );
3472 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3473 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3474 id, data );
3475 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3476 flags & MF_BYPOSITION ? pos : id,
3477 flags & ~MF_REMOVE );
3478 /* Default: MF_INSERT */
3479 return InsertMenuW( hMenu, pos, flags, id, data );
3480}
3481
3482
3483/*******************************************************************
3484 * CheckMenuItem (USER32.46)
3485 */
3486DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3487{
3488 MENUITEM *item;
3489 DWORD ret;
3490
3491 dprintf(("USER32: CheckMenuItem %x %x %x", hMenu, id, flags));
3492
3493 //TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3494 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3495 ret = item->fState & MF_CHECKED;
3496 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3497 else item->fState &= ~MF_CHECKED;
3498 return ret;
3499}
3500
3501
3502/**********************************************************************
3503 * EnableMenuItem32 (USER32.170)
3504 */
3505UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3506{
3507 UINT oldflags;
3508 MENUITEM *item;
3509 POPUPMENU *menu;
3510
3511 dprintf(("USER32: EnableMenuItem %x %x %x", hMenu, wItemID, wFlags));
3512
3513 //TRACE("(%04x, %04X, %04X) !\n",
3514 // hMenu, wItemID, wFlags);
3515
3516 /* Get the Popupmenu to access the owner menu */
3517 if (!(menu = MENU_GetMenu(hMenu)))
3518 return (UINT)-1;
3519
3520 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3521 return (UINT)-1;
3522
3523 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3524 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3525
3526 /* In win95 if the close item in the system menu change update the close button */
3527 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3528 {
3529 if (menu->hSysMenuOwner != 0)
3530 {
3531 POPUPMENU* parentMenu;
3532
3533 /* Get the parent menu to access*/
3534 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3535 return (UINT)-1;
3536
3537 /* Refresh the frame to reflect the change*/
3538 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3539 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3540 }
3541 }
3542
3543 return oldflags;
3544}
3545
3546
3547/*******************************************************************
3548 * GetMenuString32A (USER32.268)
3549 */
3550INT WINAPI GetMenuStringA(
3551 HMENU hMenu, /* [in] menuhandle */
3552 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3553 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3554 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3555 UINT wFlags /* [in] MF_ flags */
3556) {
3557 MENUITEM *item;
3558
3559 dprintf(("USER32: GetMenuStringA %x %d %d %x", hMenu, wItemID, nMaxSiz, wFlags));
3560
3561 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3562 // hMenu, wItemID, str, nMaxSiz, wFlags );
3563
3564 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3565
3566 if (!str || !nMaxSiz)
3567 {
3568 if (item && IS_STRING_ITEM(item->fType))
3569 return strlen(item->text);
3570 else
3571 return 0;
3572 }
3573
3574 str[0] = '\0';
3575
3576 if (item)
3577 {
3578 if (!IS_STRING_ITEM(item->fType)) return 0;
3579 lstrcpynA( str, item->text, nMaxSiz );
3580 }
3581
3582 return strlen(str);
3583}
3584
3585
3586/*******************************************************************
3587 * GetMenuString32W (USER32.269)
3588 */
3589INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3590 LPWSTR str, INT nMaxSiz, UINT wFlags )
3591{
3592 MENUITEM *item;
3593
3594 dprintf(("USER32: GetMenuStringW %x %d %x %d %d", hMenu, wItemID, str, nMaxSiz, wFlags));
3595
3596 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3597 // hMenu, wItemID, str, nMaxSiz, wFlags );
3598
3599 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3600
3601 if (!str || !nMaxSiz)
3602 {
3603 if (item && IS_STRING_ITEM(item->fType))
3604 return strlen(item->text);
3605 else
3606 return 0;
3607 }
3608
3609 str[0] = '\0';
3610
3611 if (item)
3612 {
3613 if (!IS_STRING_ITEM(item->fType)) return 0;
3614 lstrcpynAtoW( str, item->text, nMaxSiz );
3615 }
3616
3617 return lstrlenW(str);
3618}
3619
3620
3621/**********************************************************************
3622 * HiliteMenuItem32 (USER32.318)
3623 */
3624BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3625 UINT wHilite )
3626{
3627 LPPOPUPMENU menu;
3628
3629 dprintf(("USER32: HiliteMenuItem"));
3630
3631 //TRACE("(%04x, %04x, %04x, %04x);\n",
3632 // hWnd, hMenu, wItemID, wHilite);
3633 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3634 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3635 if (menu->FocusedItem == wItemID) return TRUE;
3636 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3637 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3638 return TRUE;
3639}
3640
3641
3642/**********************************************************************
3643 * GetMenuState (USER32.267)
3644 */
3645UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3646{
3647 MENUITEM *item;
3648
3649 dprintf(("USER32: GetMenuState %d %d",wItemID,wFlags));
3650
3651 //TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3652 // hMenu, wItemID, wFlags);
3653
3654 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3655
3656 //debug_print_menuitem (" item: ", item, "");
3657 if (item->fType & MF_POPUP)
3658 {
3659 POPUPMENU *menu = MENU_GetMenu(item->hSubMenu);
3660 if (!menu) return -1;
3661 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3662 }
3663 else
3664 {
3665 /* We used to (from way back then) mask the result to 0xff. */
3666 /* I don't know why and it seems wrong as the documented */
3667 /* return flag MF_SEPARATOR is outside that mask. */
3668 return (item->fType | item->fState);
3669 }
3670}
3671
3672
3673/**********************************************************************
3674 * GetMenuItemCount32 (USER32.262)
3675 */
3676INT WINAPI GetMenuItemCount( HMENU hMenu )
3677{
3678 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3679
3680 dprintf(("USER32: GetMenuItemCount %x", hMenu));
3681
3682 if (!IS_A_MENU(menu)) return -1;
3683 //TRACE("(%04x) returning %d\n",
3684 // hMenu, menu->nItems );
3685 return menu->nItems;
3686}
3687
3688/**********************************************************************
3689 * GetMenuItemID32 (USER32.263)
3690 */
3691UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3692{
3693 MENUITEM * lpmi;
3694
3695 dprintf(("USER32: GetMenuItemID %x %d", hMenu, nPos));
3696
3697 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0xFFFFFFFF;
3698 if (lpmi->fType & MF_POPUP) return 0xFFFFFFFF;
3699 return lpmi->wID;
3700
3701}
3702
3703/*******************************************************************
3704 * InsertMenu32A (USER32.322)
3705 */
3706BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3707 UINT id, LPCSTR str )
3708{
3709 MENUITEM *item;
3710
3711 if (IS_STRING_ITEM(flags) && str)
3712 dprintf(("USER32: InsertMenuA %x %d %x %x %s", hMenu, pos, flags, id, str));
3713 // TRACE("hMenu %04x, pos %d, flags %08x, "
3714 // "id %04x, str '%s'\n",
3715 // hMenu, pos, flags, id, str );
3716 else // TRACE("hMenu %04x, pos %d, flags %08x, "
3717 dprintf(("USER32: InsertMenuA %x %d %x %d %x", hMenu, pos, flags, id, str));
3718 // "id %04x, str %08lx (not a string)\n",
3719 // hMenu, pos, flags, id, (DWORD)str );
3720
3721 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3722
3723 if (!(MENU_SetItemData( item, flags, id, str )))
3724 {
3725 RemoveMenu( hMenu, pos, flags );
3726 return FALSE;
3727 }
3728
3729 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3730 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3731
3732 item->hCheckBit = item->hUnCheckBit = 0;
3733 return TRUE;
3734}
3735
3736
3737/*******************************************************************
3738 * InsertMenu32W (USER32.325)
3739 */
3740BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3741 UINT id, LPCWSTR str )
3742{
3743 BOOL ret;
3744
3745 if (IS_STRING_ITEM(flags) && str)
3746 {
3747 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3748 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3749 HeapFree( GetProcessHeap(), 0, newstr );
3750 return ret;
3751 }
3752 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3753}
3754
3755
3756/*******************************************************************
3757 * AppendMenu32A (USER32.5)
3758 */
3759BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3760 UINT id, LPCSTR data )
3761{
3762 dprintf(("USER32: AppendMenuA %x %x %x %x", hMenu, flags, id, data));
3763
3764 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3765}
3766
3767
3768/*******************************************************************
3769 * AppendMenu32W (USER32.6)
3770 */
3771BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3772 UINT id, LPCWSTR data )
3773{
3774 dprintf(("USER32: AppendMenuW %x %x %x %x", hMenu, flags, id, data));
3775
3776 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3777}
3778
3779
3780/**********************************************************************
3781 * RemoveMenu (USER32.441)
3782 */
3783BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3784{
3785 LPPOPUPMENU menu;
3786 MENUITEM *item;
3787
3788 dprintf(("USER32: RemoveMenu %x %d %x", hMenu, nPos, wFlags));
3789
3790 //TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3791 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3792 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3793
3794 /* Remove item */
3795
3796 MENU_FreeItemData( item );
3797
3798 if (--menu->nItems == 0)
3799 {
3800 HeapFree(GetProcessHeap(), 0, menu->items );
3801 menu->items = NULL;
3802 }
3803 else
3804 {
3805 while(nPos < menu->nItems)
3806 {
3807 *item = *(item+1);
3808 item++;
3809 nPos++;
3810 }
3811 menu->items = (MENUITEM*)HeapReAlloc(GetProcessHeap(), 0, menu->items,
3812 menu->nItems * sizeof(MENUITEM) );
3813 }
3814 return TRUE;
3815}
3816
3817
3818/**********************************************************************
3819 * DeleteMenu32 (USER32.129)
3820 */
3821BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3822{
3823 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3824
3825 dprintf(("USER32: DeleteMenu %x %d %x", hMenu, nPos, wFlags));
3826
3827 if (!item) return FALSE;
3828 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3829 /* nPos is now the position of the item */
3830 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3831 return TRUE;
3832}
3833
3834
3835/*******************************************************************
3836 * ModifyMenu32A (USER32.397)
3837 */
3838BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3839 UINT id, LPCSTR str )
3840{
3841 MENUITEM *item;
3842
3843
3844 if (IS_STRING_ITEM(flags))
3845 {
3846 dprintf(("USER32: ModifyMenuA, %x %d %x %d %s", hMenu, pos, flags, id, str));
3847 //TRACE("%04x %d %04x %04x '%s'\n",
3848 // hMenu, pos, flags, id, str ? str : "#NULL#" );
3849 if (!str) return FALSE;
3850 }
3851 else
3852 {
3853 dprintf(("USER32: ModifyMenuA, %x %d %x %d %x", hMenu, pos, flags, id, str));
3854 //TRACE("%04x %d %04x %04x %08lx\n",
3855 // hMenu, pos, flags, id, (DWORD)str );
3856 }
3857
3858 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3859 return MENU_SetItemData( item, flags, id, str );
3860}
3861
3862
3863/*******************************************************************
3864 * ModifyMenu32W (USER32.398)
3865 */
3866BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3867 UINT id, LPCWSTR str )
3868{
3869 BOOL ret;
3870
3871 if (IS_STRING_ITEM(flags) && str)
3872 {
3873 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3874 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3875 HeapFree( GetProcessHeap(), 0, newstr );
3876 return ret;
3877 }
3878 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3879}
3880
3881
3882/**********************************************************************
3883 * CreatePopupMenu32 (USER32.82)
3884 */
3885HMENU WINAPI CreatePopupMenu(void)
3886{
3887 HMENU hmenu;
3888 POPUPMENU *menu;
3889
3890 dprintf(("USER32: CreatePopupMenu"));
3891
3892 if (!(hmenu = CreateMenu())) return 0;
3893 menu = MENU_GetMenu(hmenu);
3894 menu->wFlags |= MF_POPUP;
3895 menu->bTimeToHide = FALSE;
3896 return hmenu;
3897}
3898
3899
3900/**********************************************************************
3901 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3902 */
3903DWORD WINAPI GetMenuCheckMarkDimensions(void)
3904{
3905 dprintf(("USER32: GetMenuCheckMarkDimensions"));
3906
3907 return MAKELONG( check_bitmap_width, check_bitmap_height );
3908}
3909
3910
3911/**********************************************************************
3912 * SetMenuItemBitmaps32 (USER32.490)
3913 */
3914BOOL WINAPI SetMenuItemBitmaps(HMENU hMenu, UINT nPos, UINT wFlags,
3915 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3916{
3917 MENUITEM *item;
3918
3919 dprintf(("USER32: SetMenuItemBitmaps %x %d %x %x %x", hMenu, nPos, wFlags, hNewCheck, hNewUnCheck));
3920
3921 //TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3922 // hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3923 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3924
3925 if (!hNewCheck && !hNewUnCheck)
3926 {
3927 item->fState &= ~MF_USECHECKBITMAPS;
3928 }
3929 else /* Install new bitmaps */
3930 {
3931 item->hCheckBit = hNewCheck;
3932 item->hUnCheckBit = hNewUnCheck;
3933 item->fState |= MF_USECHECKBITMAPS;
3934 }
3935 return TRUE;
3936}
3937
3938
3939/**********************************************************************
3940 * CreateMenu (USER32.81)
3941 */
3942HMENU WINAPI CreateMenu(void)
3943{
3944 HMENU hMenu;
3945 LPPOPUPMENU menu;
3946
3947 dprintf(("USER32: CreateMenu"));
3948
3949#ifdef __WIN32OS2__
3950 if (!(menu = (LPPOPUPMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3951 if(ObjAllocateHandle(&hMenu, (DWORD)menu, USEROBJ_MENU) == FALSE) return 0;
3952#else
3953 if (!(hMenu = (HMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3954 menu = (LPPOPUPMENU)hMenu;
3955#endif
3956
3957 ZeroMemory(menu, sizeof(POPUPMENU));
3958 menu->wMagic = MENU_MAGIC;
3959 menu->FocusedItem = NO_SELECTED_ITEM;
3960 menu->bTimeToHide = FALSE;
3961
3962 //TRACE("return %04x\n", hMenu );
3963
3964 return hMenu;
3965}
3966
3967
3968/**********************************************************************
3969 * DestroyMenu32 (USER32.134)
3970 */
3971BOOL WINAPI DestroyMenu( HMENU hMenu )
3972{
3973 //TRACE("(%04x)\n", hMenu);
3974
3975 dprintf(("USER32: DestroyMenu %x", hMenu));
3976
3977 /* Silently ignore attempts to destroy default system popup */
3978
3979 if (hMenu && hMenu != MENU_DefSysPopup)
3980 {
3981 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3982 HWND pTPWnd = MENU_GetTopPopupWnd();
3983
3984 if( pTPWnd && (hMenu == GetWindowLongA(pTPWnd,0)) )
3985 SetWindowLongA(pTPWnd,0,0);
3986
3987 if (IS_A_MENU(lppop))
3988 {
3989 lppop->wMagic = 0; /* Mark it as destroyed */
3990
3991 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3992 (!pTPWnd || (lppop->hWnd != pTPWnd)))
3993 DestroyWindow( lppop->hWnd );
3994
3995 if (lppop->items) /* recursively destroy submenus */
3996 {
3997 int i;
3998 MENUITEM *item = lppop->items;
3999 for (i = lppop->nItems; i > 0; i--, item++)
4000 {
4001 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
4002 MENU_FreeItemData( item );
4003 }
4004 HeapFree(GetProcessHeap(), 0, lppop->items );
4005 }
4006#ifdef __WIN32OS2__
4007 HeapFree(GetProcessHeap(),0,(LPVOID)lppop);
4008 ObjFreeHandle(hMenu);
4009#else
4010 HeapFree(GetProcessHeap(),0,(LPVOID)hMenu);
4011#endif
4012 MENU_ReleaseTopPopupWnd();
4013 }
4014 else
4015 {
4016 MENU_ReleaseTopPopupWnd();
4017 return FALSE;
4018 }
4019 }
4020 return (hMenu != MENU_DefSysPopup);
4021}
4022
4023/**********************************************************************
4024 * GetSystemMenu32 (USER32.291)
4025 */
4026HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4027{
4028 HMENU retvalue = 0;
4029
4030 dprintf(("USER32: GetSystemMenu %x %d", hWnd, bRevert));
4031
4032 if (IsWindow(hWnd))
4033 {
4034 HMENU hSysMenu = getSysMenu(hWnd);
4035 if(hSysMenu)
4036 {
4037 if( bRevert )
4038 {
4039 DestroyMenu(hSysMenu);
4040 hSysMenu = 0;
4041 setSysMenu(hWnd,hSysMenu);
4042 }
4043 else
4044 {
4045 POPUPMENU *menu = MENU_GetMenu(hSysMenu);
4046 if(menu)
4047 {
4048 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
4049 menu->items[0].hSubMenu = MENU_CopySysPopup();
4050 }
4051 else
4052 {
4053 //WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
4054 // wndPtr->hSysMenu, hWnd);
4055 hSysMenu = 0;
4056 setSysMenu(hWnd,hSysMenu);
4057 }
4058 }
4059 }
4060
4061 if(!hSysMenu && (GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) )
4062 {
4063 hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
4064 setSysMenu(hWnd,hSysMenu);
4065 }
4066
4067 if( hSysMenu )
4068 {
4069 POPUPMENU *menu;
4070 retvalue = GetSubMenu(hSysMenu, 0);
4071
4072 /* Store the dummy sysmenu handle to facilitate the refresh */
4073 /* of the close button if the SC_CLOSE item change */
4074 menu = MENU_GetMenu(retvalue);
4075 if (menu)
4076 menu->hSysMenuOwner = hSysMenu;
4077 }
4078 }
4079 return retvalue;
4080}
4081
4082
4083/*******************************************************************
4084 * SetSystemMenu32 (USER32.508)
4085 */
4086BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4087{
4088 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
4089
4090 dprintf(("USER32: SetSystemMenu"));
4091
4092 if (win32wnd)
4093 {
4094 if (win32wnd->GetSysMenu()) DestroyMenu(win32wnd->GetSysMenu());
4095 win32wnd->SetSysMenu(MENU_GetSysMenu( hwnd, hMenu ));
4096 RELEASE_WNDOBJ(win32wnd);
4097 return TRUE;
4098 }
4099 return FALSE;
4100}
4101
4102/**********************************************************************
4103 * GetMenu32 (USER32.257)
4104 */
4105HMENU WINAPI GetMenu( HWND hWnd )
4106{
4107 HMENU retvalue;
4108
4109 dprintf(("USER32: GetMenu %x", hWnd));
4110
4111 if (GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) return 0;
4112 else return getMenu(hWnd);
4113}
4114
4115/**********************************************************************
4116 * SetMenu32 (USER32.487)
4117 */
4118BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4119{
4120 //TRACE("(%04x, %04x);\n", hWnd, hMenu);
4121
4122 dprintf(("USER32: SetMenu %x %x", hWnd, hMenu));
4123
4124 if (hMenu && !IsMenu(hMenu))
4125 {
4126 //WARN("hMenu is not a menu handle\n");
4127 return FALSE;
4128 }
4129
4130
4131 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD))
4132 {
4133 if (GetCapture() == hWnd) ReleaseCapture();
4134
4135 setMenu(hWnd,hMenu);
4136 if (hMenu != 0)
4137 {
4138 LPPOPUPMENU lpmenu;
4139
4140 if (!(lpmenu = MENU_GetMenu(hMenu)))
4141 {
4142 return FALSE;
4143 }
4144 lpmenu->hWnd = hWnd;
4145 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
4146 lpmenu->Height = 0; /* Make sure we recalculate the size */
4147 }
4148//SvL: This fixes the menu in standard mine sweeper (window isn't visible
4149// when SetMenu is called)
4150// if (IsWindowVisible(hWnd))
4151 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4152 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4153 return TRUE;
4154 }
4155 return FALSE;
4156}
4157
4158
4159/**********************************************************************
4160 * GetSubMenu32 (USER32.288)
4161 */
4162HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4163{
4164 MENUITEM * lpmi;
4165
4166 dprintf(("USER32: GetSubMenu %x %d", hMenu, nPos));
4167
4168 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4169 if (!(lpmi->fType & MF_POPUP)) return 0;
4170 return lpmi->hSubMenu;
4171}
4172
4173
4174/**********************************************************************
4175 * DrawMenuBar (USER32.161)
4176 */
4177BOOL WINAPI DrawMenuBar( HWND hWnd )
4178{
4179 LPPOPUPMENU lppop;
4180
4181 dprintf(("USER32: DrawMenuBar %x", hWnd));
4182
4183 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) && getMenu(hWnd))
4184 {
4185 lppop = MENU_GetMenu(getMenu(hWnd));
4186 if (lppop == NULL)
4187 {
4188 return FALSE;
4189 }
4190
4191 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4192 lppop->hwndOwner = hWnd;
4193 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4194 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4195 return TRUE;
4196 }
4197 return FALSE;
4198}
4199
4200
4201/***********************************************************************
4202 * EndMenu (USER.187) (USER32.175)
4203 */
4204void WINAPI EndMenu(void)
4205{
4206 dprintf(("USER32: EndMenu not implemented!"));
4207 /*
4208 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
4209 */
4210
4211 fEndMenu = TRUE;
4212}
4213
4214
4215/*****************************************************************
4216 * LoadMenu32A (USER32.370)
4217 */
4218HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4219{
4220 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4221
4222 dprintf(("USER32: LoadMenuA %x %x", instance, name));
4223
4224 if (!hrsrc) return 0;
4225 return LoadMenuIndirectA( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4226}
4227
4228
4229/*****************************************************************
4230 * LoadMenu32W (USER32.373)
4231 */
4232HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4233{
4234 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4235
4236 dprintf(("USER32: LoadMenuW %x %x", instance, name));
4237
4238 if (!hrsrc) return 0;
4239 return LoadMenuIndirectW( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4240}
4241
4242
4243/**********************************************************************
4244 * LoadMenuIndirect32A (USER32.371)
4245 */
4246HMENU WINAPI LoadMenuIndirectA(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate)
4247{
4248 HMENU hMenu;
4249 WORD version, offset;
4250 LPCSTR p = (LPCSTR)lpMenuTemplate;
4251
4252 dprintf(("USER32: LoadMenuIndirectA"));
4253
4254 //TRACE("%p\n", template );
4255 version = GET_WORD(p);
4256 p += sizeof(WORD);
4257 switch (version)
4258 {
4259 case 0:
4260 offset = GET_WORD(p);
4261 p += sizeof(WORD) + offset;
4262 if (!(hMenu = CreateMenu())) return 0;
4263 if (!MENU_ParseResource( p, hMenu, TRUE ))
4264 {
4265 DestroyMenu( hMenu );
4266 return 0;
4267 }
4268 return hMenu;
4269 case 1:
4270 offset = GET_WORD(p);
4271 p += sizeof(WORD) + offset;
4272 if (!(hMenu = CreateMenu())) return 0;
4273 if (!MENUEX_ParseResource( p, hMenu))
4274 {
4275 DestroyMenu( hMenu );
4276 return 0;
4277 }
4278 return hMenu;
4279 default:
4280 //ERR("version %d not supported.\n", version);
4281 return 0;
4282 }
4283}
4284
4285
4286/**********************************************************************
4287 * LoadMenuIndirect32W (USER32.372)
4288 */
4289HMENU WINAPI LoadMenuIndirectW(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate )
4290{
4291 dprintf(("USER32: LoadMenuIndirectW"));
4292
4293 /* FIXME: is there anything different between A and W? */
4294 return LoadMenuIndirectA(lpMenuTemplate);
4295}
4296
4297
4298/**********************************************************************
4299 * IsMenu32 (USER32.346)
4300 */
4301BOOL WINAPI IsMenu(HMENU hmenu)
4302{
4303 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4304
4305 dprintf(("USER32: IsMenu"));
4306
4307 return IS_A_MENU(menu);
4308}
4309
4310/**********************************************************************
4311 * GetMenuItemInfo32_common
4312 */
4313
4314static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4315 LPMENUITEMINFOA lpmii, BOOL unicode)
4316{
4317 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4318
4319 //debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
4320
4321 if (!menu)
4322 return FALSE;
4323
4324 if (lpmii->fMask & MIIM_TYPE) {
4325 lpmii->fType = menu->fType;
4326 switch (MENU_ITEM_TYPE(menu->fType)) {
4327 case MF_STRING:
4328 if (menu->text) {
4329 if (unicode) {
4330 if(lpmii->dwTypeData && lpmii->cch)
4331 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4332 lpmii->cch = lstrlenA(menu->text);
4333 } else {
4334 if(lpmii->dwTypeData && lpmii->cch)
4335 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4336 lpmii->cch = lstrlenA(menu->text);
4337 }
4338 }
4339 break;
4340 case MF_OWNERDRAW:
4341 case MF_BITMAP:
4342 lpmii->dwTypeData = menu->text;
4343 /* fall through */
4344 default:
4345 lpmii->cch = 0;
4346 }
4347 }
4348
4349 if (lpmii->fMask & MIIM_STRING) {
4350 if (unicode) {
4351 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4352 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4353 } else {
4354 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4355 lpmii->cch = lstrlenA(menu->text);
4356 }
4357 }
4358
4359 if (lpmii->fMask & MIIM_FTYPE)
4360 lpmii->fType = menu->fType;
4361
4362 if (lpmii->fMask & MIIM_BITMAP)
4363 lpmii->hbmpItem = menu->hbmpItem;
4364
4365 if (lpmii->fMask & MIIM_STATE)
4366 lpmii->fState = menu->fState;
4367
4368 if (lpmii->fMask & MIIM_ID)
4369 lpmii->wID = menu->wID;
4370
4371 if (lpmii->fMask & MIIM_SUBMENU)
4372 lpmii->hSubMenu = menu->hSubMenu;
4373
4374 if (lpmii->fMask & MIIM_CHECKMARKS) {
4375 lpmii->hbmpChecked = menu->hCheckBit;
4376 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4377 }
4378 if (lpmii->fMask & MIIM_DATA)
4379 lpmii->dwItemData = menu->dwItemData;
4380
4381 return TRUE;
4382}
4383
4384/**********************************************************************
4385 * GetMenuItemInfoA (USER32.264)
4386 */
4387BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4388 LPMENUITEMINFOA lpmii)
4389{
4390 dprintf(("USER32: GetMenuItemInfoA"));
4391
4392 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4393}
4394
4395/**********************************************************************
4396 * GetMenuItemInfoW (USER32.265)
4397 */
4398BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4399 LPMENUITEMINFOW lpmii)
4400{
4401 dprintf(("USER32: GetMenuItemInfoW"));
4402
4403 return GetMenuItemInfo_common (hmenu, item, bypos,
4404 (LPMENUITEMINFOA)lpmii, TRUE);
4405}
4406
4407/**********************************************************************
4408 * SetMenuItemInfo32_common
4409 */
4410
4411static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4412 const MENUITEMINFOA *lpmii,
4413 BOOL unicode)
4414{
4415 if (!menu) return FALSE;
4416
4417 if (lpmii->fMask & MIIM_TYPE ) {
4418 /* Get rid of old string. */
4419 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4420 HeapFree(GetProcessHeap(), 0, menu->text);
4421 menu->text = NULL;
4422 }
4423
4424 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4425 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4426 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4427
4428 menu->text = lpmii->dwTypeData;
4429
4430 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4431 if (unicode)
4432 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4433 else
4434 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4435 }
4436 }
4437
4438 if (lpmii->fMask & MIIM_FTYPE ) {
4439 /* free the string when the type is changing */
4440 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4441 HeapFree(GetProcessHeap(), 0, menu->text);
4442 menu->text = NULL;
4443 }
4444 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4445 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4446 }
4447
4448 if (lpmii->fMask & MIIM_STRING ) {
4449 /* free the string when used */
4450 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4451 HeapFree(GetProcessHeap(), 0, menu->text);
4452 if (unicode)
4453 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4454 else
4455 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4456 }
4457 }
4458
4459 if (lpmii->fMask & MIIM_STATE)
4460 {
4461 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4462 menu->fState = lpmii->fState;
4463 }
4464
4465 if (lpmii->fMask & MIIM_ID)
4466 menu->wID = lpmii->wID;
4467
4468 if (lpmii->fMask & MIIM_SUBMENU) {
4469 menu->hSubMenu = lpmii->hSubMenu;
4470 if (menu->hSubMenu) {
4471 POPUPMENU *subMenu = MENU_GetMenu((UINT)menu->hSubMenu);
4472 if (IS_A_MENU(subMenu)) {
4473 subMenu->wFlags |= MF_POPUP;
4474 menu->fType |= MF_POPUP;
4475 }
4476 else
4477 /* FIXME: Return an error ? */
4478 menu->fType &= ~MF_POPUP;
4479 }
4480 else
4481 menu->fType &= ~MF_POPUP;
4482 }
4483
4484 if (lpmii->fMask & MIIM_CHECKMARKS)
4485 {
4486 menu->hCheckBit = lpmii->hbmpChecked;
4487 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4488 }
4489 if (lpmii->fMask & MIIM_DATA)
4490 menu->dwItemData = lpmii->dwItemData;
4491
4492 //debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4493 return TRUE;
4494}
4495
4496/**********************************************************************
4497 * SetMenuItemInfo32A (USER32.491)
4498 */
4499BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4500 const MENUITEMINFOA *lpmii)
4501{
4502 dprintf(("USER32: SetMenuItemInfoA %x %d %d %x", hmenu, item, bypos, lpmii));
4503
4504 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4505 lpmii, FALSE);
4506}
4507
4508/**********************************************************************
4509 * SetMenuItemInfo32W (USER32.492)
4510 */
4511BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4512 const MENUITEMINFOW *lpmii)
4513{
4514 dprintf(("USER32: SetMenuItemInfoW"));
4515
4516 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4517 (const MENUITEMINFOA*)lpmii, TRUE);
4518}
4519
4520/**********************************************************************
4521 * SetMenuDefaultItem (USER32.489)
4522 *
4523 */
4524BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4525{
4526 UINT i;
4527 POPUPMENU *menu;
4528 MENUITEM *item;
4529
4530
4531 dprintf(("USER32: SetMenuDefaultItem"));
4532 //TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4533
4534 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4535
4536 /* reset all default-item flags */
4537 item = menu->items;
4538 for (i = 0; i < menu->nItems; i++, item++)
4539 {
4540 item->fState &= ~MFS_DEFAULT;
4541 }
4542
4543 /* no default item */
4544 if ( -1 == uItem)
4545 {
4546 return TRUE;
4547 }
4548
4549 item = menu->items;
4550 if ( bypos )
4551 {
4552 if ( uItem >= menu->nItems ) return FALSE;
4553 item[uItem].fState |= MFS_DEFAULT;
4554 return TRUE;
4555 }
4556 else
4557 {
4558 for (i = 0; i < menu->nItems; i++, item++)
4559 {
4560 if (item->wID == uItem)
4561 {
4562 item->fState |= MFS_DEFAULT;
4563 return TRUE;
4564 }
4565 }
4566
4567 }
4568 return FALSE;
4569}
4570
4571/**********************************************************************
4572 * GetMenuDefaultItem (USER32.260)
4573 */
4574UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4575{
4576 POPUPMENU *menu;
4577 MENUITEM * item;
4578 UINT i = 0;
4579
4580 dprintf(("USER32: GetMenuDefaultItem"));
4581 //TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4582
4583 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4584
4585 /* find default item */
4586 item = menu->items;
4587
4588 /* empty menu */
4589 if (! item) return -1;
4590
4591 while ( !( item->fState & MFS_DEFAULT ) )
4592 {
4593 i++; item++;
4594 if (i >= menu->nItems ) return -1;
4595 }
4596
4597 /* default: don't return disabled items */
4598 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4599
4600 /* search rekursiv when needed */
4601 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4602 {
4603 UINT ret;
4604 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4605 if ( -1 != ret ) return ret;
4606
4607 /* when item not found in submenu, return the popup item */
4608 }
4609 return ( bypos ) ? i : item->wID;
4610
4611}
4612
4613/**********************************************************************
4614 * InsertMenuItem32A (USER32.323)
4615 */
4616BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4617 const MENUITEMINFOA *lpmii)
4618{
4619 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4620
4621 dprintf(("USER32: InsertMenuItemA %x %d %d %x", hMenu, uItem, bypos, lpmii->wID));
4622
4623 return SetMenuItemInfo_common(item, lpmii, FALSE);
4624}
4625
4626
4627/**********************************************************************
4628 * InsertMenuItem32W (USER32.324)
4629 */
4630BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4631 const MENUITEMINFOW *lpmii)
4632{
4633 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4634
4635 dprintf(("USER32: InsertMenuItemW"));
4636
4637 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4638}
4639
4640/**********************************************************************
4641 * CheckMenuRadioItem32 (USER32.47)
4642 */
4643
4644BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4645 UINT first, UINT last, UINT check,
4646 UINT bypos)
4647{
4648 MENUITEM *mifirst, *milast, *micheck;
4649 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4650
4651 dprintf(("USER32: CheckMenuRadioItem"));
4652 //TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4653 // hMenu, first, last, check, bypos);
4654
4655 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4656 milast = MENU_FindItem (&mlast, &last, bypos);
4657 micheck = MENU_FindItem (&mcheck, &check, bypos);
4658
4659 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4660 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4661 micheck > milast || micheck < mifirst)
4662 return FALSE;
4663
4664 while (mifirst <= milast)
4665 {
4666 if (mifirst == micheck)
4667 {
4668 mifirst->fType |= MFT_RADIOCHECK;
4669 mifirst->fState |= MFS_CHECKED;
4670 } else {
4671 mifirst->fType &= ~MFT_RADIOCHECK;
4672 mifirst->fState &= ~MFS_CHECKED;
4673 }
4674 mifirst++;
4675 }
4676
4677 return TRUE;
4678}
4679
4680/**********************************************************************
4681 * GetMenuItemRect32 (USER32.266)
4682 *
4683 * ATTENTION: Here, the returned values in rect are the screen
4684 * coordinates of the item just like if the menu was
4685 * always on the upper left side of the application.
4686 *
4687 */
4688BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4689 LPRECT rect)
4690{
4691 POPUPMENU *itemMenu;
4692 MENUITEM *item;
4693 HWND referenceHwnd;
4694
4695 //TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4696
4697 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4698 referenceHwnd = hwnd;
4699
4700 if(!hwnd)
4701 {
4702 itemMenu = MENU_GetMenu(hMenu);
4703 if (itemMenu == NULL) {
4704 SetLastError(ERROR_INVALID_PARAMETER);
4705 return FALSE;
4706 }
4707
4708 if(itemMenu->hWnd == 0)
4709 return FALSE;
4710 referenceHwnd = itemMenu->hWnd;
4711 }
4712
4713 if ((rect == NULL) || (item == NULL)) {
4714 SetLastError(ERROR_INVALID_PARAMETER);
4715 return FALSE;
4716 }
4717 *rect = item->rect;
4718
4719 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4720 dprintf(("USER32: GetMenuItemRect %x %x %d (%d,%d)(%d,%d)", hwnd, hMenu, uItem, rect->left, rect->top, rect->right, rect->bottom));
4721
4722 return TRUE;
4723}
4724
4725/**********************************************************************
4726 * SetMenuInfo
4727 *
4728 * FIXME
4729 * MIM_APPLYTOSUBMENUS
4730 * actually use the items to draw the menu
4731 */
4732BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4733{
4734 POPUPMENU *menu;
4735
4736 dprintf(("USER32: SetMenuInfo"));
4737 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4738
4739 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu=MENU_GetMenu(hMenu)))
4740 {
4741
4742 if (lpmi->fMask & MIM_BACKGROUND)
4743 menu->hbrBack = lpmi->hbrBack;
4744
4745 if (lpmi->fMask & MIM_HELPID)
4746 menu->dwContextHelpID = lpmi->dwContextHelpID;
4747
4748 if (lpmi->fMask & MIM_MAXHEIGHT)
4749 menu->cyMax = lpmi->cyMax;
4750
4751 if (lpmi->fMask & MIM_MENUDATA)
4752 menu->dwMenuData = lpmi->dwMenuData;
4753
4754 if (lpmi->fMask & MIM_STYLE)
4755 menu->dwStyle = lpmi->dwStyle;
4756
4757 return TRUE;
4758 }
4759 return FALSE;
4760}
4761
4762/**********************************************************************
4763 * GetMenuInfo
4764 *
4765 * NOTES
4766 * win98/NT5.0
4767 *
4768 */
4769BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4770{ POPUPMENU *menu;
4771
4772 dprintf(("USER32: GetMenuInfo"));
4773 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4774
4775 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4776 {
4777
4778 if (lpmi->fMask & MIM_BACKGROUND)
4779 lpmi->hbrBack = menu->hbrBack;
4780
4781 if (lpmi->fMask & MIM_HELPID)
4782 lpmi->dwContextHelpID = menu->dwContextHelpID;
4783
4784 if (lpmi->fMask & MIM_MAXHEIGHT)
4785 lpmi->cyMax = menu->cyMax;
4786
4787 if (lpmi->fMask & MIM_MENUDATA)
4788 lpmi->dwMenuData = menu->dwMenuData;
4789
4790 if (lpmi->fMask & MIM_STYLE)
4791 lpmi->dwStyle = menu->dwStyle;
4792
4793 return TRUE;
4794 }
4795 return FALSE;
4796}
4797
4798/**********************************************************************
4799 * SetMenuContextHelpId (USER32.488)
4800 */
4801BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4802{
4803 LPPOPUPMENU menu;
4804
4805 dprintf(("USER32: SetMenuContextHelpId"));
4806 //TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4807
4808 menu = MENU_GetMenu(hMenu);
4809 if (menu)
4810 {
4811 menu->dwContextHelpID = dwContextHelpID;
4812 return TRUE;
4813 }
4814 return FALSE;
4815}
4816
4817/**********************************************************************
4818 * GetMenuContextHelpId (USER32.488)
4819 */
4820DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4821{
4822 LPPOPUPMENU menu;
4823
4824 dprintf(("USER32: GetMenuContextHelpId"));
4825
4826 menu = MENU_GetMenu(hMenu);
4827 if (menu)
4828 {
4829 return menu->dwContextHelpID;
4830 }
4831 return 0;
4832}
4833
4834/**********************************************************************
4835 * MenuItemFromPoint (USER32.387)
4836 */
4837UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4838{
4839 dprintf(("USER32: MenuItemFromPoint not implemented!"));
4840
4841 return 0;
4842}
4843
4844/**********************************************************************
4845 * IsMenuActive (Internal)
4846 */
4847BOOL IsMenuActive(void)
4848{
4849 return pTopPopupWnd && (GetWindowLongA(pTopPopupWnd,GWL_STYLE) & WS_VISIBLE);
4850}
4851
4852BOOL POPUPMENU_Register()
4853{
4854 WNDCLASSA wndClass;
4855 BOOL rc;
4856
4857//SvL: Don't check this now
4858// if (GlobalFindAtomA(POPUPMENUCLASSNAME)) return FALSE;
4859
4860 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
4861 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
4862 wndClass.lpfnWndProc = (WNDPROC)PopupMenuWndProc;
4863 wndClass.cbClsExtra = 0;
4864 wndClass.cbWndExtra = sizeof(HMENU);
4865 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
4866 wndClass.hbrBackground = NULL_BRUSH;
4867 wndClass.lpszClassName = POPUPMENUCLASSNAME;
4868
4869 rc = RegisterClassA(&wndClass);
4870 MENU_Init();
4871
4872 return rc;
4873}
4874//******************************************************************************
4875//******************************************************************************
4876BOOL POPUPMENU_Unregister()
4877{
4878 if (GlobalFindAtomA(POPUPMENUCLASSNAME))
4879 return UnregisterClassA(POPUPMENUCLASSNAME,(HINSTANCE)NULL);
4880 else return FALSE;
4881}
4882//******************************************************************************
4883//******************************************************************************
4884
Note: See TracBrowser for help on using the repository browser.