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

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

ported the Wine MDI control + some menu fixes

File size: 147.5 KB
Line 
1/* $Id: menu.cpp,v 1.36 2001-09-19 15:39:48 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 HMENU hNewMenu;
2522 HWND hNewWnd;
2523 UINT id = 0;
2524 LRESULT l = SendMessageA( pmt->hOwnerWnd, WM_NEXTMENU, vk,
2525 (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu );
2526
2527 //TRACE("%04x [%04x] -> %04x [%04x]\n",
2528 // (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2529
2530 if( l == 0 )
2531 {
2532 hNewWnd = pmt->hOwnerWnd;
2533 if( IS_SYSTEM_MENU(menu) )
2534 {
2535 /* switch to the menu bar */
2536
2537 if( (GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_CHILD) || !getMenu(pmt->hOwnerWnd) )
2538 {
2539 return FALSE;
2540 }
2541
2542 hNewMenu = getMenu(pmt->hOwnerWnd);
2543 if( vk == VK_LEFT )
2544 {
2545 menu = MENU_GetMenu(hNewMenu);
2546 id = menu->nItems - 1;
2547 }
2548 }
2549 else if( GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_SYSMENU )
2550 {
2551 /* switch to the system menu */
2552 hNewMenu = getSysMenu(pmt->hOwnerWnd);
2553 }
2554 else
2555 {
2556 return FALSE;
2557 }
2558 }
2559 else /* application returned a new menu to switch to */
2560 {
2561 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2562
2563 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2564 {
2565 if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_SYSMENU) &&
2566 GetSubMenu(getSysMenu(hNewWnd), 0) == hNewMenu )
2567 {
2568 /* get the real system menu */
2569 hNewMenu = getSysMenu(hNewWnd);
2570 }
2571 else if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_CHILD) || (getMenu(hNewWnd) != hNewMenu) )
2572 {
2573 /* FIXME: Not sure what to do here, perhaps,
2574 * try to track hNewMenu as a popup? */
2575
2576 //TRACE(" -- got confused.\n");
2577 return FALSE;
2578 }
2579 }
2580 else return FALSE;
2581 }
2582
2583 if( hNewMenu != pmt->hTopMenu )
2584 {
2585 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2586 FALSE, 0 );
2587 if( pmt->hCurrentMenu != pmt->hTopMenu )
2588 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2589 }
2590
2591 if( hNewWnd != pmt->hOwnerWnd )
2592 {
2593 ReleaseCapture();
2594 pmt->hOwnerWnd = hNewWnd;
2595 SetCapture(pmt->hOwnerWnd); //SvL: Don't know if this is good enough
2596 //EVENT_Capture( pmt->hOwnerWnd, HTMENU ); //CB: todo
2597 }
2598
2599 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2600 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2601
2602 return TRUE;
2603 }
2604 return FALSE;
2605}
2606
2607/***********************************************************************
2608 * MENU_SuspendPopup
2609 *
2610 * The idea is not to show the popup if the next input message is
2611 * going to hide it anyway.
2612 */
2613static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT uMsg )
2614{
2615 MSG msg;
2616
2617 msg.hwnd = pmt->hOwnerWnd;
2618
2619 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2620 pmt->trackFlags |= TF_SKIPREMOVE;
2621
2622 switch( uMsg )
2623 {
2624 case WM_KEYDOWN:
2625 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2626 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2627 {
2628 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2629 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2630 if( msg.message == WM_KEYDOWN &&
2631 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2632 {
2633 pmt->trackFlags |= TF_SUSPENDPOPUP;
2634 return TRUE;
2635 }
2636 }
2637 break;
2638 }
2639
2640 /* failures go through this */
2641 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2642 return FALSE;
2643}
2644
2645/***********************************************************************
2646 * MENU_KeyLeft
2647 *
2648 * Handle a VK_LEFT key event in a menu.
2649 */
2650static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2651{
2652 POPUPMENU *menu;
2653 HMENU hmenutmp, hmenuprev;
2654 UINT prevcol;
2655
2656 hmenuprev = hmenutmp = pmt->hTopMenu;
2657 menu = MENU_GetMenu(hmenutmp);
2658
2659 /* Try to move 1 column left (if possible) */
2660 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2661 NO_SELECTED_ITEM ) {
2662
2663 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2664 prevcol, TRUE, 0 );
2665 return;
2666 }
2667
2668 /* close topmost popup */
2669 while (hmenutmp != pmt->hCurrentMenu)
2670 {
2671 hmenuprev = hmenutmp;
2672 hmenutmp = MENU_GetSubPopup( hmenuprev );
2673 }
2674
2675 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2676 pmt->hCurrentMenu = hmenuprev;
2677
2678 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2679 {
2680 /* move menu bar selection if no more popups are left */
2681
2682 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2683 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2684
2685 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2686 {
2687 /* A sublevel menu was displayed - display the next one
2688 * unless there is another displacement coming up */
2689
2690 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2691 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2692 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2693 }
2694 }
2695}
2696
2697
2698/***********************************************************************
2699 * MENU_KeyRight
2700 *
2701 * Handle a VK_RIGHT key event in a menu.
2702 */
2703static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2704{
2705 HMENU hmenutmp;
2706 POPUPMENU *menu = MENU_GetMenu(pmt->hTopMenu);
2707 UINT nextcol;
2708
2709 //TRACE("MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2710 // pmt->hCurrentMenu,
2711 // ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2712 // items[0].text,
2713 // pmt->hTopMenu, menu->items[0].text );
2714
2715 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2716 {
2717 /* If already displaying a popup, try to display sub-popup */
2718
2719 hmenutmp = pmt->hCurrentMenu;
2720 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags,&pmt->pt);
2721
2722 /* if subpopup was displayed then we are done */
2723 if (hmenutmp != pmt->hCurrentMenu) return;
2724 }
2725
2726 /* Check to see if there's another column */
2727 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2728 NO_SELECTED_ITEM ) {
2729 //TRACE("Going to %d.\n", nextcol );
2730 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2731 nextcol, TRUE, 0 );
2732 return;
2733 }
2734
2735 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2736 {
2737 if( pmt->hCurrentMenu != pmt->hTopMenu )
2738 {
2739 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2740 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2741 } else hmenutmp = 0;
2742
2743 /* try to move to the next item */
2744 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2745 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2746
2747 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2748 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2749 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2750 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2751 }
2752}
2753
2754VOID MENU_DispatchMouseMsg(MSG *msg)
2755{
2756 LONG hittest;
2757
2758 hittest = SendMessageA(msg->hwnd,WM_NCHITTEST,0,MAKELONG(msg->pt.x,msg->pt.y));
2759 if (hittest != HTCLIENT)
2760 SendMessageA(msg->hwnd,msg->message+WM_NCMOUSEMOVE-WM_MOUSEMOVE,hittest,MAKELONG(msg->pt.x,msg->pt.y));
2761 else
2762 DispatchMessageA(msg);
2763}
2764
2765/***********************************************************************
2766 * MENU_TrackMenu
2767 *
2768 * Menu tracking code.
2769 */
2770static INT MENU_TrackMenu(HMENU hmenu,UINT wFlags,INT x,INT y,HWND hwnd,BOOL inMenuBar,const RECT *lprect)
2771{
2772 MSG msg;
2773 POPUPMENU *menu;
2774 BOOL fRemove;
2775 INT executedMenuId = -1;
2776 MTRACKER mt;
2777 BOOL enterIdleSent = FALSE;
2778 BOOL bSysMenu;
2779
2780 mt.trackFlags = 0;
2781 mt.hCurrentMenu = hmenu;
2782 mt.hTopMenu = hmenu;
2783 mt.hOwnerWnd = hwnd;
2784 mt.pt.x = x;
2785 mt.pt.y = y;
2786
2787 //TRACE("hmenu=0x%04x flags=0x%08x (%d,%d) hwnd=0x%04x (%d,%d)-(%d,%d)\n",
2788 // hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2789 // (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2790
2791 fEndMenu = FALSE;
2792 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
2793
2794 bSysMenu = IS_SYSTEM_MENU(menu);
2795
2796 if (wFlags & TPM_BUTTONDOWN)
2797 {
2798 /* Get the result in order to start the tracking or not */
2799 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2800 fEndMenu = !fRemove;
2801 }
2802
2803 //EVENT_Capture( mt.hOwnerWnd, HTMENU ); //CB: todo
2804 //SvL: Set keyboard & mouse event capture
2805 SetCapture(mt.hOwnerWnd);
2806
2807 while (!fEndMenu)
2808 {
2809 menu = MENU_GetMenu(mt.hCurrentMenu);
2810 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2811
2812 /* we have to keep the message in the queue until it's
2813 * clear that menu loop is not over yet. */
2814// if (!GetMessageA(&msg,msg.hwnd,0,0)) break;
2815 //SvL: Getting messages for only the menu delays background paints (i.e. VPBuddy logo)
2816 if (!GetMessageA(&msg,0,0,0)) break;
2817 TranslateMessage( &msg );
2818 mt.pt = msg.pt;
2819
2820 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2821 enterIdleSent=FALSE;
2822
2823 fRemove = FALSE;
2824 if((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2825 {
2826 /* Find a menu for this mouse event */
2827 POINT pt = msg.pt;
2828
2829 hmenu = MENU_PtMenu(mt.hTopMenu,pt,inMenuBar);
2830
2831 //CB: todo: solve conflicts with OS/2's single message queue architecture
2832
2833 switch(msg.message)
2834 {
2835 /* no WM_NC... messages in captured state */
2836
2837 case WM_RBUTTONDBLCLK:
2838 case WM_RBUTTONDOWN:
2839 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2840 goto buttondown;
2841 case WM_LBUTTONDBLCLK:
2842 if (bSysMenu && (hmenu == mt.hTopMenu))
2843 {
2844 fEndMenu = TRUE;
2845 break;
2846 }
2847 /* fall through */
2848 case WM_LBUTTONDOWN:
2849 /* If the message belongs to the menu, removes it from the queue */
2850 /* Else, end menu tracking */
2851
2852 buttondown:
2853 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2854 mouseOverMenuID = -1;
2855 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2856 fEndMenu = !fRemove;
2857 break;
2858
2859 case WM_RBUTTONUP:
2860 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2861 /* fall through */
2862 case WM_LBUTTONUP:
2863 /* Check if a menu was selected by the mouse */
2864 if (hmenu)
2865 {
2866 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2867 mouseOverMenuID = -1;
2868 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2869
2870 /* End the loop if executedMenuId is an item ID */
2871 /* or if the job was done (executedMenuId = 0). */
2872 fEndMenu = fRemove = (executedMenuId != -1);
2873 }
2874 /* No menu was selected by the mouse */
2875 /* if the function was called by TrackPopupMenu, continue
2876 with the menu tracking. If not, stop it */
2877 else
2878 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2879
2880 break;
2881
2882 case WM_MOUSEMOVE:
2883 /* In win95 winelook, the selected menu item must be changed every time the
2884 mouse moves. In Win31 winelook, the mouse button has to be held down */
2885#ifdef __WIN32OS2__
2886 if ( !fOS2Look ||
2887 ( (msg.wParam & MK_LBUTTON) ||
2888 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2889 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2890#else
2891 if ( (TWEAK_WineLook > WIN31_LOOK) ||
2892 ( (msg.wParam & MK_LBUTTON) ||
2893 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2894 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2895#endif
2896
2897 } /* switch(msg.message) - mouse */
2898 }
2899 else if (msg.message == WM_TIMER)
2900 {
2901 UINT id = -1;
2902 POPUPMENU *ptmenu = NULL;
2903
2904 if (isTimerSet)
2905 {
2906 /*
2907 If we get here, an attempt was made to pop up a submenu.
2908 (See MENU_MouseMove() )
2909 */
2910
2911 /* Get the ID of the menu item the mouse is over now. */
2912 if( hmenu )
2913 {
2914 ptmenu = (POPUPMENU *)MENU_GetMenu( hmenu );
2915 if( IS_SYSTEM_MENU(ptmenu) )
2916 id = 0;
2917 else
2918 MENU_FindItemByCoords( ptmenu, mt.pt, &id );
2919
2920 /* If it is over the same item that set up the timer originally .... */
2921 if (mouseOverMenuID != -1 && mouseOverMenuID == id)
2922 {
2923 /* .... Pop up the menu */
2924 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, hmenu, FALSE, wFlags,&mt.pt);
2925 }
2926 }
2927
2928 /* Reset the timer so it doesn't fire again. (So we are ready for the next
2929 attempt to popup a submenu... ) */
2930 KillTimer(mt.hOwnerWnd,SUBMENU_POPUP_TIMERID);
2931 isTimerSet = FALSE;
2932 }
2933 }
2934 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2935 {
2936 fRemove = TRUE; /* Keyboard messages are always removed */
2937 switch(msg.message)
2938 {
2939 case WM_KEYDOWN:
2940 switch(msg.wParam)
2941 {
2942 case VK_HOME:
2943 case VK_END:
2944 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2945 NO_SELECTED_ITEM, FALSE, 0 );
2946 /* fall through */
2947 case VK_UP:
2948 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2949 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2950 break;
2951
2952 case VK_DOWN: /* If on menu bar, pull-down the menu */
2953
2954 menu = MENU_GetMenu(mt.hCurrentMenu);
2955 if (!(menu->wFlags & MF_POPUP))
2956 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags,&mt.pt);
2957 else /* otherwise try to move selection */
2958 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2959 break;
2960
2961 case VK_LEFT:
2962 MENU_KeyLeft( &mt, wFlags );
2963 break;
2964
2965 case VK_RIGHT:
2966 MENU_KeyRight( &mt, wFlags );
2967 break;
2968
2969 case VK_ESCAPE:
2970 fEndMenu = TRUE;
2971 break;
2972
2973 case VK_F1:
2974 {
2975 HELPINFO hi;
2976 hi.cbSize = sizeof(HELPINFO);
2977 hi.iContextType = HELPINFO_MENUITEM;
2978 if (menu->FocusedItem == NO_SELECTED_ITEM)
2979 hi.iCtrlId = 0;
2980 else
2981 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2982 hi.hItemHandle = hmenu;
2983 hi.dwContextId = menu->dwContextHelpID;
2984 hi.MousePos = msg.pt;
2985 //TRACE_(winhelp)("Sending HELPINFO_MENUITEM to 0x%08x\n", hwnd);
2986 SendMessageA(hwnd, WM_HELP, 0, (LPARAM)&hi);
2987 break;
2988 }
2989
2990 default:
2991 break;
2992 }
2993 break; /* WM_KEYDOWN */
2994
2995 case WM_SYSKEYDOWN:
2996 switch(msg.wParam)
2997 {
2998 case VK_MENU:
2999 fEndMenu = TRUE;
3000 break;
3001
3002 }
3003 break; /* WM_SYSKEYDOWN */
3004
3005 case WM_CHAR:
3006 {
3007 UINT pos;
3008
3009 if (msg.wParam == '\r' || msg.wParam == ' ')
3010 {
3011 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3012 fEndMenu = (executedMenuId != -1);
3013
3014 break;
3015 }
3016
3017 /* Hack to avoid control chars. */
3018 /* We will find a better way real soon... */
3019 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
3020
3021 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3022 LOWORD(msg.wParam), FALSE );
3023 if (pos == (UINT)-2) fEndMenu = TRUE;
3024 else if (pos == (UINT)-1) MessageBeep(0);
3025 else
3026 {
3027 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3028 TRUE, 0 );
3029 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3030 fEndMenu = (executedMenuId != -1);
3031 }
3032 }
3033 break;
3034 } /* switch(msg.message) - kbd */
3035 }
3036 else if (msg.message == WM_SYSCOMMAND)
3037 {
3038 /* The user clicked on the system menu/button */
3039 fEndMenu = TRUE;
3040 break;
3041 }
3042 else
3043 {
3044 DispatchMessageA( &msg );
3045 }
3046
3047 if (!fEndMenu) fRemove = TRUE;
3048
3049 /* finally remove message from the queue */
3050
3051 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3052 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
3053 else mt.trackFlags &= ~TF_SKIPREMOVE;
3054 }
3055
3056 ReleaseCapture();
3057
3058 menu = MENU_GetMenu(mt.hTopMenu);
3059
3060 if( IsWindow( mt.hOwnerWnd ) )
3061 {
3062 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
3063
3064 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3065#ifdef __WIN32OS2__
3066 if (menu && menu->wFlags & MF_POPUP)
3067 {
3068 DestroyWindow( menu->hWnd );
3069 menu->hWnd = 0;
3070// ShowWindow( menu->hWnd, SW_HIDE );
3071 uSubPWndLevel = 0;
3072 }
3073#endif
3074 SendMessageA( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
3075 }
3076
3077 /* Reset the variable for hiding menu */
3078 menu->bTimeToHide = FALSE;
3079
3080 /* The return value is only used by TrackPopupMenu */
3081 return ((executedMenuId != -1) ? executedMenuId : 0);
3082}
3083
3084/***********************************************************************
3085 * MENU_InitTracking
3086 */
3087static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3088{
3089 //TRACE("hwnd=0x%04x hmenu=0x%04x\n", hWnd, hMenu);
3090
3091 HideCaret(0);
3092
3093 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3094 if (!(wFlags & TPM_NONOTIFY))
3095 SendMessageA( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3096
3097 SendMessageA( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
3098
3099 if (!(wFlags & TPM_NONOTIFY))
3100 SendMessageA( hWnd, WM_INITMENU, hMenu, 0 );
3101
3102 return TRUE;
3103}
3104/***********************************************************************
3105 * MENU_ExitTracking
3106 */
3107static BOOL MENU_ExitTracking(HWND hWnd)
3108{
3109 //TRACE("hwnd=0x%04x\n", hWnd);
3110
3111 SendMessageA( hWnd, WM_EXITMENULOOP, 0, 0 );
3112 ShowCaret(0);
3113 return TRUE;
3114}
3115
3116/***********************************************************************
3117 * MENU_TrackMouseMenuBar
3118 *
3119 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3120 */
3121void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3122{
3123 HMENU hMenu = (ht == 0) ? getMenu(hWnd):getSysMenu(hWnd);
3124 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3125
3126 //TRACE("pwnd=%p ht=0x%04x (%ld,%ld)\n", wndPtr, ht, pt.x, pt.y);
3127
3128 if (IsMenu(hMenu))
3129 {
3130 if (ht == HTCAPTION) wFlags |= TPM_CAPTIONSYSMENU | TPM_RIGHTBUTTON;
3131 if (IsIconic(hWnd)) wFlags |= TPM_BOTTOMALIGN; //CB: todo: for minimized windows
3132
3133 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3134 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd,ht == 0, NULL );
3135 MENU_ExitTracking(hWnd);
3136 }
3137}
3138
3139MENUITEM *MENU_HighlightedItem=NULL;
3140UINT MENU_HighlightedItemID=NO_SELECTED_ITEM;
3141POPUPMENU *MENU_HighlightedMenu=NULL;
3142
3143void MENU_TrackMouseMenuBar_MouseMove(HWND hwnd,POINT pt,BOOL OnMenu)
3144{
3145 HMENU hMenu = getMenu(hwnd);
3146 MENUITEM *item = NULL;
3147 RECT rect;
3148 HDC hdc;
3149 BOOL UnHighlight = (OnMenu==TRUE)?FALSE:TRUE;
3150 POPUPMENU *ptmenu = NULL;
3151 UINT id = NO_SELECTED_ITEM;
3152
3153 if (OnMenu == TRUE && IsMenu(hMenu)) {
3154
3155 ptmenu=(POPUPMENU *)MENU_GetMenu( hMenu );
3156
3157 item=MENU_FindItemByCoords( ptmenu, pt, &id );
3158
3159 if(!item) {
3160 /* No item selected, perhaps we are on the blank spot? */
3161 UnHighlight = TRUE;
3162 /* Paranoid setting */
3163 item=NULL;
3164 } else if(id == MENU_HighlightedItemID) {
3165 /* If it's already highlighted, don't do it again */
3166 return;
3167 } else if(item->fState & MF_MOUSESELECT) {
3168 /* If it's dropped, we let the DrawMenuItem draw the sunken border */
3169 return;
3170 } else if(item->fType & MF_BITMAP) {
3171 UnHighlight = TRUE;
3172 /* (Required) Paranoid setting */
3173 item=NULL;
3174 } else {
3175 hdc = GetDCEx( ptmenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3176 rect = item->rect;
3177 DrawEdge(hdc, &rect, BDR_RAISEDINNER, BF_RECT);
3178 ReleaseDC( ptmenu->hWnd, hdc );
3179
3180 UnHighlight = TRUE;
3181 }
3182 }
3183
3184 if(UnHighlight == TRUE) {
3185 if(MENU_HighlightedItem) {
3186 if(!(MENU_HighlightedItem->fState & MF_MOUSESELECT)) {
3187 hdc = GetDCEx( MENU_HighlightedMenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3188 rect = MENU_HighlightedItem->rect;
3189
3190 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_MENU));
3191 ReleaseDC( MENU_HighlightedMenu->hWnd, hdc );
3192 }
3193 }
3194
3195 /* Sets to NULL, NO_SELECTED_ITEM, NULL, unless it found a new
3196 item, then it will be filled in with the proper values */
3197 MENU_HighlightedItem = item;
3198 MENU_HighlightedItemID = id;
3199 MENU_HighlightedMenu= ptmenu;
3200 }
3201}
3202
3203/***********************************************************************
3204 * MENU_TrackKbdMenuBar
3205 *
3206 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3207 */
3208void MENU_TrackKbdMenuBar( HWND hWnd, UINT wParam, INT vkey)
3209{
3210 UINT uItem = NO_SELECTED_ITEM;
3211 HMENU hTrackMenu;
3212 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3213
3214 /* find window that has a menu */
3215
3216 while(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD)
3217 if( !(hWnd = GetParent(hWnd)) ) return;
3218
3219 /* check if we have to track a system menu */
3220
3221 if( (GetWindowLongA(hWnd,GWL_STYLE) & (WS_CHILD | WS_MINIMIZE)) ||
3222 !getMenu(hWnd) || vkey == VK_SPACE )
3223 {
3224 if( !(GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) ) return;
3225 hTrackMenu = getSysMenu(hWnd);
3226 uItem = 0;
3227 wParam |= HTSYSMENU; /* prevent item lookup */
3228 }
3229 else
3230 hTrackMenu = getMenu(hWnd);
3231
3232 if (IsMenu( hTrackMenu ))
3233 {
3234 MENU_InitTracking( hWnd, hTrackMenu, FALSE, wFlags );
3235
3236 if( vkey && vkey != VK_SPACE )
3237 {
3238 uItem = MENU_FindItemByKey( hWnd, hTrackMenu,
3239 vkey, (wParam & HTSYSMENU) );
3240 if( uItem >= (UINT)(-2) )
3241 {
3242 if( uItem == (UINT)(-1) ) MessageBeep(0);
3243 hTrackMenu = 0;
3244 }
3245 }
3246
3247 if( hTrackMenu )
3248 {
3249 MENU_SelectItem( hWnd, hTrackMenu, uItem, TRUE, 0 );
3250
3251 if( uItem == NO_SELECTED_ITEM )
3252 MENU_MoveSelection( hWnd, hTrackMenu, ITEM_NEXT );
3253 else if( vkey )
3254 PostMessageA( hWnd, WM_KEYDOWN, VK_DOWN, 0L );
3255
3256 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hWnd,TRUE, NULL );
3257 }
3258
3259 MENU_ExitTracking (hWnd);
3260 }
3261}
3262
3263
3264/**********************************************************************
3265 * TrackPopupMenu (USER32.549)
3266 *
3267 * Like the win32 API, the function return the command ID only if the
3268 * flag TPM_RETURNCMD is on.
3269 *
3270 */
3271BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3272 INT nReserved, HWND hWnd, const RECT *lpRect )
3273{
3274 BOOL ret = FALSE;
3275
3276 if(lpRect) {
3277 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));
3278 }
3279 else dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x lpRect=NULL", hMenu, wFlags, x, y, nReserved, hWnd));
3280
3281 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3282
3283 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3284 if (!(wFlags & TPM_NONOTIFY))
3285 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3286
3287 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3288 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,FALSE, lpRect );
3289 MENU_ExitTracking(hWnd);
3290
3291 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3292 ret = 1;
3293
3294 return ret;
3295}
3296
3297/**********************************************************************
3298 * TrackPopupMenuEx (USER32.550)
3299 */
3300BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3301 HWND hWnd, LPTPMPARAMS lpTpm )
3302{
3303 dprintf(("USER32: TrackPopupMenuEx"));
3304 //FIXME("not fully implemented\n" );
3305 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3306 lpTpm ? &lpTpm->rcExclude : NULL );
3307}
3308
3309/***********************************************************************
3310 * PopupMenuWndProc
3311 *
3312 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3313 */
3314LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3315 LPARAM lParam )
3316{
3317 LRESULT retvalue;
3318
3319 //TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3320 //hwnd, message, wParam, lParam);
3321
3322 switch(message)
3323 {
3324 case WM_CREATE:
3325 {
3326 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3327 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3328 retvalue = 0;
3329 goto END;
3330 }
3331
3332 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3333 retvalue = MA_NOACTIVATE;
3334 goto END;
3335
3336 case WM_PAINT:
3337 {
3338 PAINTSTRUCT ps;
3339 BeginPaint( hwnd, &ps );
3340 MENU_DrawPopupMenu( hwnd, ps.hdc,
3341 (HMENU)GetWindowLongA( hwnd, 0 ) );
3342 EndPaint( hwnd, &ps );
3343 retvalue = 0;
3344 goto END;
3345 }
3346 case WM_ERASEBKGND:
3347 retvalue = 1;
3348 goto END;
3349
3350 case WM_DESTROY:
3351
3352 /* zero out global pointer in case resident popup window
3353 * was somehow destroyed. */
3354
3355 if(MENU_GetTopPopupWnd() )
3356 {
3357 if( hwnd == pTopPopupWnd )
3358 {
3359 //ERR("resident popup destroyed!\n");
3360
3361 MENU_DestroyTopPopupWnd();
3362 uSubPWndLevel = 0;
3363 }
3364 else
3365 uSubPWndLevel--;
3366 MENU_ReleaseTopPopupWnd();
3367 }
3368 break;
3369
3370 case WM_SHOWWINDOW:
3371
3372 if( wParam )
3373 {
3374 //if( !(*(HMENU*)wndPtr->wExtra) )
3375 // ERR("no menu to display\n");
3376 }
3377 else
3378 SetWindowLongA(hwnd,0,0);
3379 break;
3380
3381 case MM_SETMENUHANDLE:
3382
3383 SetWindowLongA(hwnd,0,wParam);
3384 break;
3385
3386 case MM_GETMENUHANDLE:
3387
3388 retvalue = GetWindowLongA(hwnd,0);
3389 goto END;
3390
3391 default:
3392 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3393 goto END;
3394 }
3395 retvalue = 0;
3396END:
3397 return retvalue;
3398}
3399
3400
3401/***********************************************************************
3402 * MENU_GetMenuBarHeight
3403 *
3404 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3405 */
3406UINT MENU_GetMenuBarHeight(HWND hwnd,UINT menubarWidth,INT orgX,INT orgY)
3407{
3408 HDC hdc;
3409 RECT rectBar;
3410 LPPOPUPMENU lppop;
3411 UINT retvalue;
3412
3413 //TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3414 // hwnd, menubarWidth, orgX, orgY );
3415
3416 if (!(lppop = MENU_GetMenu(getMenu(hwnd))))
3417 {
3418 return 0;
3419 }
3420
3421 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3422 SelectObject( hdc, hMenuFont);
3423 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3424 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3425 ReleaseDC( hwnd, hdc );
3426 retvalue = lppop->Height;
3427 return retvalue;
3428}
3429
3430
3431/*******************************************************************
3432 * ChangeMenu32A (USER32.23)
3433 */
3434BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3435 UINT id, UINT flags )
3436{
3437 dprintf(("USER32: ChangeMenuA"));
3438
3439 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3440 // hMenu, pos, (DWORD)data, id, flags );
3441 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3442 id, data );
3443 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3444 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3445 id, data );
3446 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3447 flags & MF_BYPOSITION ? pos : id,
3448 flags & ~MF_REMOVE );
3449 /* Default: MF_INSERT */
3450 return InsertMenuA( hMenu, pos, flags, id, data );
3451}
3452
3453
3454/*******************************************************************
3455 * ChangeMenu32W (USER32.24)
3456 */
3457BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3458 UINT id, UINT flags )
3459{
3460 dprintf(("USER32: ChangeMenuW"));
3461
3462 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3463 // hMenu, pos, (DWORD)data, id, flags );
3464 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3465 id, data );
3466 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3467 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3468 id, data );
3469 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3470 flags & MF_BYPOSITION ? pos : id,
3471 flags & ~MF_REMOVE );
3472 /* Default: MF_INSERT */
3473 return InsertMenuW( hMenu, pos, flags, id, data );
3474}
3475
3476
3477/*******************************************************************
3478 * CheckMenuItem (USER32.46)
3479 */
3480DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3481{
3482 MENUITEM *item;
3483 DWORD ret;
3484
3485 dprintf(("USER32: CheckMenuItem %x %x %x", hMenu, id, flags));
3486
3487 //TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3488 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3489 ret = item->fState & MF_CHECKED;
3490 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3491 else item->fState &= ~MF_CHECKED;
3492 return ret;
3493}
3494
3495
3496/**********************************************************************
3497 * EnableMenuItem32 (USER32.170)
3498 */
3499UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3500{
3501 UINT oldflags;
3502 MENUITEM *item;
3503 POPUPMENU *menu;
3504
3505 dprintf(("USER32: EnableMenuItem %x %x %x", hMenu, wItemID, wFlags));
3506
3507 //TRACE("(%04x, %04X, %04X) !\n",
3508 // hMenu, wItemID, wFlags);
3509
3510 /* Get the Popupmenu to access the owner menu */
3511 if (!(menu = MENU_GetMenu(hMenu)))
3512 return (UINT)-1;
3513
3514 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3515 return (UINT)-1;
3516
3517 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3518 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3519
3520 /* In win95 if the close item in the system menu change update the close button */
3521 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3522 {
3523 if (menu->hSysMenuOwner != 0)
3524 {
3525 POPUPMENU* parentMenu;
3526
3527 /* Get the parent menu to access*/
3528 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3529 return (UINT)-1;
3530
3531 /* Refresh the frame to reflect the change*/
3532 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3533 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3534 }
3535 }
3536
3537 return oldflags;
3538}
3539
3540
3541/*******************************************************************
3542 * GetMenuString32A (USER32.268)
3543 */
3544INT WINAPI GetMenuStringA(
3545 HMENU hMenu, /* [in] menuhandle */
3546 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3547 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3548 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3549 UINT wFlags /* [in] MF_ flags */
3550) {
3551 MENUITEM *item;
3552
3553 dprintf(("USER32: GetMenuStringA %x %d %d %x", hMenu, wItemID, nMaxSiz, wFlags));
3554
3555 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3556 // hMenu, wItemID, str, nMaxSiz, wFlags );
3557
3558 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3559
3560 if (!str || !nMaxSiz)
3561 {
3562 if (item && IS_STRING_ITEM(item->fType))
3563 return strlen(item->text);
3564 else
3565 return 0;
3566 }
3567
3568 str[0] = '\0';
3569
3570 if (item)
3571 {
3572 if (!IS_STRING_ITEM(item->fType)) return 0;
3573 lstrcpynA( str, item->text, nMaxSiz );
3574 }
3575
3576 return strlen(str);
3577}
3578
3579
3580/*******************************************************************
3581 * GetMenuString32W (USER32.269)
3582 */
3583INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3584 LPWSTR str, INT nMaxSiz, UINT wFlags )
3585{
3586 MENUITEM *item;
3587
3588 dprintf(("USER32: GetMenuStringW %x %d %x %d %d", hMenu, wItemID, str, nMaxSiz, wFlags));
3589
3590 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3591 // hMenu, wItemID, str, nMaxSiz, wFlags );
3592
3593 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3594
3595 if (!str || !nMaxSiz)
3596 {
3597 if (item && IS_STRING_ITEM(item->fType))
3598 return strlen(item->text);
3599 else
3600 return 0;
3601 }
3602
3603 str[0] = '\0';
3604
3605 if (item)
3606 {
3607 if (!IS_STRING_ITEM(item->fType)) return 0;
3608 lstrcpynAtoW( str, item->text, nMaxSiz );
3609 }
3610
3611 return lstrlenW(str);
3612}
3613
3614
3615/**********************************************************************
3616 * HiliteMenuItem32 (USER32.318)
3617 */
3618BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3619 UINT wHilite )
3620{
3621 LPPOPUPMENU menu;
3622
3623 dprintf(("USER32: HiliteMenuItem"));
3624
3625 //TRACE("(%04x, %04x, %04x, %04x);\n",
3626 // hWnd, hMenu, wItemID, wHilite);
3627 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3628 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3629 if (menu->FocusedItem == wItemID) return TRUE;
3630 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3631 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3632 return TRUE;
3633}
3634
3635
3636/**********************************************************************
3637 * GetMenuState (USER32.267)
3638 */
3639UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3640{
3641 MENUITEM *item;
3642
3643 dprintf(("USER32: GetMenuState %d %d",wItemID,wFlags));
3644
3645 //TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3646 // hMenu, wItemID, wFlags);
3647
3648 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3649
3650 //debug_print_menuitem (" item: ", item, "");
3651 if (item->fType & MF_POPUP)
3652 {
3653 POPUPMENU *menu = MENU_GetMenu(item->hSubMenu);
3654 if (!menu) return -1;
3655 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3656 }
3657 else
3658 {
3659 /* We used to (from way back then) mask the result to 0xff. */
3660 /* I don't know why and it seems wrong as the documented */
3661 /* return flag MF_SEPARATOR is outside that mask. */
3662 return (item->fType | item->fState);
3663 }
3664}
3665
3666
3667/**********************************************************************
3668 * GetMenuItemCount32 (USER32.262)
3669 */
3670INT WINAPI GetMenuItemCount( HMENU hMenu )
3671{
3672 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3673
3674 dprintf(("USER32: GetMenuItemCount %x", hMenu));
3675
3676 if (!IS_A_MENU(menu)) return -1;
3677 //TRACE("(%04x) returning %d\n",
3678 // hMenu, menu->nItems );
3679 return menu->nItems;
3680}
3681
3682/**********************************************************************
3683 * GetMenuItemID32 (USER32.263)
3684 */
3685UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3686{
3687 MENUITEM * lpmi;
3688
3689 dprintf(("USER32: GetMenuItemID %x %d", hMenu, nPos));
3690
3691 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0xFFFFFFFF;
3692 if (lpmi->fType & MF_POPUP) return 0xFFFFFFFF;
3693 return lpmi->wID;
3694
3695}
3696
3697/*******************************************************************
3698 * InsertMenu32A (USER32.322)
3699 */
3700BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3701 UINT id, LPCSTR str )
3702{
3703 MENUITEM *item;
3704
3705 if (IS_STRING_ITEM(flags) && str)
3706 dprintf(("USER32: InsertMenuA %x %d %x %x %s", hMenu, pos, flags, id, str));
3707 // TRACE("hMenu %04x, pos %d, flags %08x, "
3708 // "id %04x, str '%s'\n",
3709 // hMenu, pos, flags, id, str );
3710 else // TRACE("hMenu %04x, pos %d, flags %08x, "
3711 dprintf(("USER32: InsertMenuA %x %d %x %d %x", hMenu, pos, flags, id, str));
3712 // "id %04x, str %08lx (not a string)\n",
3713 // hMenu, pos, flags, id, (DWORD)str );
3714
3715 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3716
3717 if (!(MENU_SetItemData( item, flags, id, str )))
3718 {
3719 RemoveMenu( hMenu, pos, flags );
3720 return FALSE;
3721 }
3722
3723 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3724 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3725
3726 item->hCheckBit = item->hUnCheckBit = 0;
3727 return TRUE;
3728}
3729
3730
3731/*******************************************************************
3732 * InsertMenu32W (USER32.325)
3733 */
3734BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3735 UINT id, LPCWSTR str )
3736{
3737 BOOL ret;
3738
3739 if (IS_STRING_ITEM(flags) && str)
3740 {
3741 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3742 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3743 HeapFree( GetProcessHeap(), 0, newstr );
3744 return ret;
3745 }
3746 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3747}
3748
3749
3750/*******************************************************************
3751 * AppendMenu32A (USER32.5)
3752 */
3753BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3754 UINT id, LPCSTR data )
3755{
3756 dprintf(("USER32: AppendMenuA %x %x %x %x", hMenu, flags, id, data));
3757
3758 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3759}
3760
3761
3762/*******************************************************************
3763 * AppendMenu32W (USER32.6)
3764 */
3765BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3766 UINT id, LPCWSTR data )
3767{
3768 dprintf(("USER32: AppendMenuW %x %x %x %x", hMenu, flags, id, data));
3769
3770 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3771}
3772
3773
3774/**********************************************************************
3775 * RemoveMenu (USER32.441)
3776 */
3777BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3778{
3779 LPPOPUPMENU menu;
3780 MENUITEM *item;
3781
3782 dprintf(("USER32: RemoveMenu %x %d %x", hMenu, nPos, wFlags));
3783
3784 //TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3785 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3786 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3787
3788 /* Remove item */
3789
3790 MENU_FreeItemData( item );
3791
3792 if (--menu->nItems == 0)
3793 {
3794 HeapFree(GetProcessHeap(), 0, menu->items );
3795 menu->items = NULL;
3796 }
3797 else
3798 {
3799 while(nPos < menu->nItems)
3800 {
3801 *item = *(item+1);
3802 item++;
3803 nPos++;
3804 }
3805 menu->items = (MENUITEM*)HeapReAlloc(GetProcessHeap(), 0, menu->items,
3806 menu->nItems * sizeof(MENUITEM) );
3807 }
3808 return TRUE;
3809}
3810
3811
3812/**********************************************************************
3813 * DeleteMenu32 (USER32.129)
3814 */
3815BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3816{
3817 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3818
3819 dprintf(("USER32: DeleteMenu %x %d %x", hMenu, nPos, wFlags));
3820
3821 if (!item) return FALSE;
3822 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3823 /* nPos is now the position of the item */
3824 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3825 return TRUE;
3826}
3827
3828
3829/*******************************************************************
3830 * ModifyMenu32A (USER32.397)
3831 */
3832BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3833 UINT id, LPCSTR str )
3834{
3835 MENUITEM *item;
3836
3837
3838 if (IS_STRING_ITEM(flags))
3839 {
3840 dprintf(("USER32: ModifyMenuA, %x %d %x %d %s", hMenu, pos, flags, id, str));
3841 //TRACE("%04x %d %04x %04x '%s'\n",
3842 // hMenu, pos, flags, id, str ? str : "#NULL#" );
3843 if (!str) return FALSE;
3844 }
3845 else
3846 {
3847 dprintf(("USER32: ModifyMenuA, %x %d %x %d %x", hMenu, pos, flags, id, str));
3848 //TRACE("%04x %d %04x %04x %08lx\n",
3849 // hMenu, pos, flags, id, (DWORD)str );
3850 }
3851
3852 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3853 return MENU_SetItemData( item, flags, id, str );
3854}
3855
3856
3857/*******************************************************************
3858 * ModifyMenu32W (USER32.398)
3859 */
3860BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3861 UINT id, LPCWSTR str )
3862{
3863 BOOL ret;
3864
3865 if (IS_STRING_ITEM(flags) && str)
3866 {
3867 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3868 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3869 HeapFree( GetProcessHeap(), 0, newstr );
3870 return ret;
3871 }
3872 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3873}
3874
3875
3876/**********************************************************************
3877 * CreatePopupMenu32 (USER32.82)
3878 */
3879HMENU WINAPI CreatePopupMenu(void)
3880{
3881 HMENU hmenu;
3882 POPUPMENU *menu;
3883
3884 dprintf(("USER32: CreatePopupMenu"));
3885
3886 if (!(hmenu = CreateMenu())) return 0;
3887 menu = MENU_GetMenu(hmenu);
3888 menu->wFlags |= MF_POPUP;
3889 menu->bTimeToHide = FALSE;
3890 return hmenu;
3891}
3892
3893
3894/**********************************************************************
3895 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3896 */
3897DWORD WINAPI GetMenuCheckMarkDimensions(void)
3898{
3899 dprintf(("USER32: GetMenuCheckMarkDimensions"));
3900
3901 return MAKELONG( check_bitmap_width, check_bitmap_height );
3902}
3903
3904
3905/**********************************************************************
3906 * SetMenuItemBitmaps32 (USER32.490)
3907 */
3908BOOL WINAPI SetMenuItemBitmaps(HMENU hMenu, UINT nPos, UINT wFlags,
3909 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3910{
3911 MENUITEM *item;
3912
3913 dprintf(("USER32: SetMenuItemBitmaps %x %d %x %x %x", hMenu, nPos, wFlags, hNewCheck, hNewUnCheck));
3914
3915 //TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3916 // hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3917 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3918
3919 if (!hNewCheck && !hNewUnCheck)
3920 {
3921 item->fState &= ~MF_USECHECKBITMAPS;
3922 }
3923 else /* Install new bitmaps */
3924 {
3925 item->hCheckBit = hNewCheck;
3926 item->hUnCheckBit = hNewUnCheck;
3927 item->fState |= MF_USECHECKBITMAPS;
3928 }
3929 return TRUE;
3930}
3931
3932
3933/**********************************************************************
3934 * CreateMenu (USER32.81)
3935 */
3936HMENU WINAPI CreateMenu(void)
3937{
3938 HMENU hMenu;
3939 LPPOPUPMENU menu;
3940
3941 dprintf(("USER32: CreateMenu"));
3942
3943#ifdef __WIN32OS2__
3944 if (!(menu = (LPPOPUPMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3945 if(ObjAllocateHandle(&hMenu, (DWORD)menu, USEROBJ_MENU) == FALSE) return 0;
3946#else
3947 if (!(hMenu = (HMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3948 menu = (LPPOPUPMENU)hMenu;
3949#endif
3950
3951 ZeroMemory(menu, sizeof(POPUPMENU));
3952 menu->wMagic = MENU_MAGIC;
3953 menu->FocusedItem = NO_SELECTED_ITEM;
3954 menu->bTimeToHide = FALSE;
3955
3956 //TRACE("return %04x\n", hMenu );
3957
3958 return hMenu;
3959}
3960
3961
3962/**********************************************************************
3963 * DestroyMenu32 (USER32.134)
3964 */
3965BOOL WINAPI DestroyMenu( HMENU hMenu )
3966{
3967 //TRACE("(%04x)\n", hMenu);
3968
3969 dprintf(("USER32: DestroyMenu %x", hMenu));
3970
3971 /* Silently ignore attempts to destroy default system popup */
3972
3973 if (hMenu && hMenu != MENU_DefSysPopup)
3974 {
3975 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3976 HWND pTPWnd = MENU_GetTopPopupWnd();
3977
3978 if( pTPWnd && (hMenu == GetWindowLongA(pTPWnd,0)) )
3979 SetWindowLongA(pTPWnd,0,0);
3980
3981 if (IS_A_MENU(lppop))
3982 {
3983 lppop->wMagic = 0; /* Mark it as destroyed */
3984
3985 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3986 (!pTPWnd || (lppop->hWnd != pTPWnd)))
3987 DestroyWindow( lppop->hWnd );
3988
3989 if (lppop->items) /* recursively destroy submenus */
3990 {
3991 int i;
3992 MENUITEM *item = lppop->items;
3993 for (i = lppop->nItems; i > 0; i--, item++)
3994 {
3995 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3996 MENU_FreeItemData( item );
3997 }
3998 HeapFree(GetProcessHeap(), 0, lppop->items );
3999 }
4000#ifdef __WIN32OS2__
4001 HeapFree(GetProcessHeap(),0,(LPVOID)lppop);
4002 ObjFreeHandle(hMenu);
4003#else
4004 HeapFree(GetProcessHeap(),0,(LPVOID)hMenu);
4005#endif
4006 MENU_ReleaseTopPopupWnd();
4007 }
4008 else
4009 {
4010 MENU_ReleaseTopPopupWnd();
4011 return FALSE;
4012 }
4013 }
4014 return (hMenu != MENU_DefSysPopup);
4015}
4016
4017/**********************************************************************
4018 * GetSystemMenu32 (USER32.291)
4019 */
4020HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4021{
4022 HMENU retvalue = 0;
4023
4024 dprintf(("USER32: GetSystemMenu %x %d", hWnd, bRevert));
4025
4026 if (IsWindow(hWnd))
4027 {
4028 HMENU hSysMenu = getSysMenu(hWnd);
4029 if(hSysMenu)
4030 {
4031 if( bRevert )
4032 {
4033 DestroyMenu(hSysMenu);
4034 hSysMenu = 0;
4035 setSysMenu(hWnd,hSysMenu);
4036 }
4037 else
4038 {
4039 POPUPMENU *menu = MENU_GetMenu(hSysMenu);
4040 if(menu)
4041 {
4042 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
4043 menu->items[0].hSubMenu = MENU_CopySysPopup();
4044 }
4045 else
4046 {
4047 //WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
4048 // wndPtr->hSysMenu, hWnd);
4049 hSysMenu = 0;
4050 setSysMenu(hWnd,hSysMenu);
4051 }
4052 }
4053 }
4054
4055 if(!hSysMenu && (GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) )
4056 {
4057 hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
4058 setSysMenu(hWnd,hSysMenu);
4059 }
4060
4061 if( hSysMenu )
4062 {
4063 POPUPMENU *menu;
4064 retvalue = GetSubMenu(hSysMenu, 0);
4065
4066 /* Store the dummy sysmenu handle to facilitate the refresh */
4067 /* of the close button if the SC_CLOSE item change */
4068 menu = MENU_GetMenu(retvalue);
4069 if (menu)
4070 menu->hSysMenuOwner = hSysMenu;
4071 }
4072 }
4073 return retvalue;
4074}
4075
4076
4077/*******************************************************************
4078 * SetSystemMenu32 (USER32.508)
4079 */
4080BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4081{
4082 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
4083
4084 dprintf(("USER32: SetSystemMenu"));
4085
4086 if (win32wnd)
4087 {
4088 if (win32wnd->GetSysMenu()) DestroyMenu(win32wnd->GetSysMenu());
4089 win32wnd->SetSysMenu(MENU_GetSysMenu( hwnd, hMenu ));
4090 RELEASE_WNDOBJ(win32wnd);
4091 return TRUE;
4092 }
4093 return FALSE;
4094}
4095
4096/**********************************************************************
4097 * GetMenu32 (USER32.257)
4098 */
4099HMENU WINAPI GetMenu( HWND hWnd )
4100{
4101 HMENU retvalue;
4102
4103 dprintf(("USER32: GetMenu %x", hWnd));
4104
4105 if (GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) return 0;
4106 else return getMenu(hWnd);
4107}
4108
4109/**********************************************************************
4110 * SetMenu32 (USER32.487)
4111 */
4112BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4113{
4114 //TRACE("(%04x, %04x);\n", hWnd, hMenu);
4115
4116 dprintf(("USER32: SetMenu %x %x", hWnd, hMenu));
4117
4118 if (hMenu && !IsMenu(hMenu))
4119 {
4120 //WARN("hMenu is not a menu handle\n");
4121 return FALSE;
4122 }
4123
4124
4125 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD))
4126 {
4127 if (GetCapture() == hWnd) ReleaseCapture();
4128
4129 setMenu(hWnd,hMenu);
4130 if (hMenu != 0)
4131 {
4132 LPPOPUPMENU lpmenu;
4133
4134 if (!(lpmenu = MENU_GetMenu(hMenu)))
4135 {
4136 return FALSE;
4137 }
4138 lpmenu->hWnd = hWnd;
4139 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
4140 lpmenu->Height = 0; /* Make sure we recalculate the size */
4141 }
4142//SvL: This fixes the menu in standard mine sweeper (window isn't visible
4143// when SetMenu is called)
4144// if (IsWindowVisible(hWnd))
4145 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4146 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4147 return TRUE;
4148 }
4149 return FALSE;
4150}
4151
4152
4153/**********************************************************************
4154 * GetSubMenu32 (USER32.288)
4155 */
4156HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4157{
4158 MENUITEM * lpmi;
4159
4160 dprintf(("USER32: GetSubMenu %x %d", hMenu, nPos));
4161
4162 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4163 if (!(lpmi->fType & MF_POPUP)) return 0;
4164 return lpmi->hSubMenu;
4165}
4166
4167
4168/**********************************************************************
4169 * DrawMenuBar (USER32.161)
4170 */
4171BOOL WINAPI DrawMenuBar( HWND hWnd )
4172{
4173 LPPOPUPMENU lppop;
4174
4175 dprintf(("USER32: DrawMenuBar %x", hWnd));
4176
4177 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) && getMenu(hWnd))
4178 {
4179 lppop = MENU_GetMenu(getMenu(hWnd));
4180 if (lppop == NULL)
4181 {
4182 return FALSE;
4183 }
4184
4185 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4186 lppop->hwndOwner = hWnd;
4187 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4188 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4189 return TRUE;
4190 }
4191 return FALSE;
4192}
4193
4194
4195/***********************************************************************
4196 * EndMenu (USER.187) (USER32.175)
4197 */
4198void WINAPI EndMenu(void)
4199{
4200 dprintf(("USER32: EndMenu not implemented!"));
4201 /*
4202 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
4203 */
4204
4205 fEndMenu = TRUE;
4206}
4207
4208
4209/*****************************************************************
4210 * LoadMenu32A (USER32.370)
4211 */
4212HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4213{
4214 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4215
4216 dprintf(("USER32: LoadMenuA %x %x", instance, name));
4217
4218 if (!hrsrc) return 0;
4219 return LoadMenuIndirectA( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4220}
4221
4222
4223/*****************************************************************
4224 * LoadMenu32W (USER32.373)
4225 */
4226HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4227{
4228 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4229
4230 dprintf(("USER32: LoadMenuW %x %x", instance, name));
4231
4232 if (!hrsrc) return 0;
4233 return LoadMenuIndirectW( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4234}
4235
4236
4237/**********************************************************************
4238 * LoadMenuIndirect32A (USER32.371)
4239 */
4240HMENU WINAPI LoadMenuIndirectA(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate)
4241{
4242 HMENU hMenu;
4243 WORD version, offset;
4244 LPCSTR p = (LPCSTR)lpMenuTemplate;
4245
4246 dprintf(("USER32: LoadMenuIndirectA"));
4247
4248 //TRACE("%p\n", template );
4249 version = GET_WORD(p);
4250 p += sizeof(WORD);
4251 switch (version)
4252 {
4253 case 0:
4254 offset = GET_WORD(p);
4255 p += sizeof(WORD) + offset;
4256 if (!(hMenu = CreateMenu())) return 0;
4257 if (!MENU_ParseResource( p, hMenu, TRUE ))
4258 {
4259 DestroyMenu( hMenu );
4260 return 0;
4261 }
4262 return hMenu;
4263 case 1:
4264 offset = GET_WORD(p);
4265 p += sizeof(WORD) + offset;
4266 if (!(hMenu = CreateMenu())) return 0;
4267 if (!MENUEX_ParseResource( p, hMenu))
4268 {
4269 DestroyMenu( hMenu );
4270 return 0;
4271 }
4272 return hMenu;
4273 default:
4274 //ERR("version %d not supported.\n", version);
4275 return 0;
4276 }
4277}
4278
4279
4280/**********************************************************************
4281 * LoadMenuIndirect32W (USER32.372)
4282 */
4283HMENU WINAPI LoadMenuIndirectW(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate )
4284{
4285 dprintf(("USER32: LoadMenuIndirectW"));
4286
4287 /* FIXME: is there anything different between A and W? */
4288 return LoadMenuIndirectA(lpMenuTemplate);
4289}
4290
4291
4292/**********************************************************************
4293 * IsMenu32 (USER32.346)
4294 */
4295BOOL WINAPI IsMenu(HMENU hmenu)
4296{
4297 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4298
4299 dprintf(("USER32: IsMenu"));
4300
4301 return IS_A_MENU(menu);
4302}
4303
4304/**********************************************************************
4305 * GetMenuItemInfo32_common
4306 */
4307
4308static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4309 LPMENUITEMINFOA lpmii, BOOL unicode)
4310{
4311 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4312
4313 //debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
4314
4315 if (!menu)
4316 return FALSE;
4317
4318 if (lpmii->fMask & MIIM_TYPE) {
4319 lpmii->fType = menu->fType;
4320 switch (MENU_ITEM_TYPE(menu->fType)) {
4321 case MF_STRING:
4322 if (menu->text) {
4323 if (unicode) {
4324 if(lpmii->dwTypeData && lpmii->cch)
4325 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4326 lpmii->cch = lstrlenA(menu->text);
4327 } else {
4328 if(lpmii->dwTypeData && lpmii->cch)
4329 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4330 lpmii->cch = lstrlenA(menu->text);
4331 }
4332 }
4333 break;
4334 case MF_OWNERDRAW:
4335 case MF_BITMAP:
4336 lpmii->dwTypeData = menu->text;
4337 /* fall through */
4338 default:
4339 lpmii->cch = 0;
4340 }
4341 }
4342
4343 if (lpmii->fMask & MIIM_STRING) {
4344 if (unicode) {
4345 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4346 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4347 } else {
4348 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4349 lpmii->cch = lstrlenA(menu->text);
4350 }
4351 }
4352
4353 if (lpmii->fMask & MIIM_FTYPE)
4354 lpmii->fType = menu->fType;
4355
4356 if (lpmii->fMask & MIIM_BITMAP)
4357 lpmii->hbmpItem = menu->hbmpItem;
4358
4359 if (lpmii->fMask & MIIM_STATE)
4360 lpmii->fState = menu->fState;
4361
4362 if (lpmii->fMask & MIIM_ID)
4363 lpmii->wID = menu->wID;
4364
4365 if (lpmii->fMask & MIIM_SUBMENU)
4366 lpmii->hSubMenu = menu->hSubMenu;
4367
4368 if (lpmii->fMask & MIIM_CHECKMARKS) {
4369 lpmii->hbmpChecked = menu->hCheckBit;
4370 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4371 }
4372 if (lpmii->fMask & MIIM_DATA)
4373 lpmii->dwItemData = menu->dwItemData;
4374
4375 return TRUE;
4376}
4377
4378/**********************************************************************
4379 * GetMenuItemInfoA (USER32.264)
4380 */
4381BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4382 LPMENUITEMINFOA lpmii)
4383{
4384 dprintf(("USER32: GetMenuItemInfoA"));
4385
4386 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4387}
4388
4389/**********************************************************************
4390 * GetMenuItemInfoW (USER32.265)
4391 */
4392BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4393 LPMENUITEMINFOW lpmii)
4394{
4395 dprintf(("USER32: GetMenuItemInfoW"));
4396
4397 return GetMenuItemInfo_common (hmenu, item, bypos,
4398 (LPMENUITEMINFOA)lpmii, TRUE);
4399}
4400
4401/**********************************************************************
4402 * SetMenuItemInfo32_common
4403 */
4404
4405static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4406 const MENUITEMINFOA *lpmii,
4407 BOOL unicode)
4408{
4409 if (!menu) return FALSE;
4410
4411 if (lpmii->fMask & MIIM_TYPE ) {
4412 /* Get rid of old string. */
4413 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4414 HeapFree(GetProcessHeap(), 0, menu->text);
4415 menu->text = NULL;
4416 }
4417
4418 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4419 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4420 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4421
4422 menu->text = lpmii->dwTypeData;
4423
4424 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4425 if (unicode)
4426 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4427 else
4428 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4429 }
4430 }
4431
4432 if (lpmii->fMask & MIIM_FTYPE ) {
4433 /* free the string when the type is changing */
4434 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4435 HeapFree(GetProcessHeap(), 0, menu->text);
4436 menu->text = NULL;
4437 }
4438 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4439 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4440 }
4441
4442 if (lpmii->fMask & MIIM_STRING ) {
4443 /* free the string when used */
4444 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4445 HeapFree(GetProcessHeap(), 0, menu->text);
4446 if (unicode)
4447 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4448 else
4449 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4450 }
4451 }
4452
4453 if (lpmii->fMask & MIIM_STATE)
4454 {
4455 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4456 menu->fState = lpmii->fState;
4457 }
4458
4459 if (lpmii->fMask & MIIM_ID)
4460 menu->wID = lpmii->wID;
4461
4462 if (lpmii->fMask & MIIM_SUBMENU) {
4463 menu->hSubMenu = lpmii->hSubMenu;
4464 if (menu->hSubMenu) {
4465 POPUPMENU *subMenu = MENU_GetMenu((UINT)menu->hSubMenu);
4466 if (IS_A_MENU(subMenu)) {
4467 subMenu->wFlags |= MF_POPUP;
4468 menu->fType |= MF_POPUP;
4469 }
4470 else
4471 /* FIXME: Return an error ? */
4472 menu->fType &= ~MF_POPUP;
4473 }
4474 else
4475 menu->fType &= ~MF_POPUP;
4476 }
4477
4478 if (lpmii->fMask & MIIM_CHECKMARKS)
4479 {
4480 menu->hCheckBit = lpmii->hbmpChecked;
4481 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4482 }
4483 if (lpmii->fMask & MIIM_DATA)
4484 menu->dwItemData = lpmii->dwItemData;
4485
4486 //debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4487 return TRUE;
4488}
4489
4490/**********************************************************************
4491 * SetMenuItemInfo32A (USER32.491)
4492 */
4493BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4494 const MENUITEMINFOA *lpmii)
4495{
4496 dprintf(("USER32: SetMenuItemInfoA %x %d %d %x", hmenu, item, bypos, lpmii));
4497
4498 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4499 lpmii, FALSE);
4500}
4501
4502/**********************************************************************
4503 * SetMenuItemInfo32W (USER32.492)
4504 */
4505BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4506 const MENUITEMINFOW *lpmii)
4507{
4508 dprintf(("USER32: SetMenuItemInfoW"));
4509
4510 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4511 (const MENUITEMINFOA*)lpmii, TRUE);
4512}
4513
4514/**********************************************************************
4515 * SetMenuDefaultItem (USER32.489)
4516 *
4517 */
4518BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4519{
4520 UINT i;
4521 POPUPMENU *menu;
4522 MENUITEM *item;
4523
4524
4525 dprintf(("USER32: SetMenuDefaultItem"));
4526 //TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4527
4528 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4529
4530 /* reset all default-item flags */
4531 item = menu->items;
4532 for (i = 0; i < menu->nItems; i++, item++)
4533 {
4534 item->fState &= ~MFS_DEFAULT;
4535 }
4536
4537 /* no default item */
4538 if ( -1 == uItem)
4539 {
4540 return TRUE;
4541 }
4542
4543 item = menu->items;
4544 if ( bypos )
4545 {
4546 if ( uItem >= menu->nItems ) return FALSE;
4547 item[uItem].fState |= MFS_DEFAULT;
4548 return TRUE;
4549 }
4550 else
4551 {
4552 for (i = 0; i < menu->nItems; i++, item++)
4553 {
4554 if (item->wID == uItem)
4555 {
4556 item->fState |= MFS_DEFAULT;
4557 return TRUE;
4558 }
4559 }
4560
4561 }
4562 return FALSE;
4563}
4564
4565/**********************************************************************
4566 * GetMenuDefaultItem (USER32.260)
4567 */
4568UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4569{
4570 POPUPMENU *menu;
4571 MENUITEM * item;
4572 UINT i = 0;
4573
4574 dprintf(("USER32: GetMenuDefaultItem"));
4575 //TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4576
4577 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4578
4579 /* find default item */
4580 item = menu->items;
4581
4582 /* empty menu */
4583 if (! item) return -1;
4584
4585 while ( !( item->fState & MFS_DEFAULT ) )
4586 {
4587 i++; item++;
4588 if (i >= menu->nItems ) return -1;
4589 }
4590
4591 /* default: don't return disabled items */
4592 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4593
4594 /* search rekursiv when needed */
4595 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4596 {
4597 UINT ret;
4598 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4599 if ( -1 != ret ) return ret;
4600
4601 /* when item not found in submenu, return the popup item */
4602 }
4603 return ( bypos ) ? i : item->wID;
4604
4605}
4606
4607/**********************************************************************
4608 * InsertMenuItem32A (USER32.323)
4609 */
4610BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4611 const MENUITEMINFOA *lpmii)
4612{
4613 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4614
4615 dprintf(("USER32: InsertMenuItemA %x %d %d %x", hMenu, uItem, bypos, lpmii->wID));
4616
4617 return SetMenuItemInfo_common(item, lpmii, FALSE);
4618}
4619
4620
4621/**********************************************************************
4622 * InsertMenuItem32W (USER32.324)
4623 */
4624BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4625 const MENUITEMINFOW *lpmii)
4626{
4627 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4628
4629 dprintf(("USER32: InsertMenuItemW"));
4630
4631 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4632}
4633
4634/**********************************************************************
4635 * CheckMenuRadioItem32 (USER32.47)
4636 */
4637
4638BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4639 UINT first, UINT last, UINT check,
4640 UINT bypos)
4641{
4642 MENUITEM *mifirst, *milast, *micheck;
4643 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4644
4645 dprintf(("USER32: CheckMenuRadioItem"));
4646 //TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4647 // hMenu, first, last, check, bypos);
4648
4649 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4650 milast = MENU_FindItem (&mlast, &last, bypos);
4651 micheck = MENU_FindItem (&mcheck, &check, bypos);
4652
4653 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4654 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4655 micheck > milast || micheck < mifirst)
4656 return FALSE;
4657
4658 while (mifirst <= milast)
4659 {
4660 if (mifirst == micheck)
4661 {
4662 mifirst->fType |= MFT_RADIOCHECK;
4663 mifirst->fState |= MFS_CHECKED;
4664 } else {
4665 mifirst->fType &= ~MFT_RADIOCHECK;
4666 mifirst->fState &= ~MFS_CHECKED;
4667 }
4668 mifirst++;
4669 }
4670
4671 return TRUE;
4672}
4673
4674/**********************************************************************
4675 * GetMenuItemRect32 (USER32.266)
4676 *
4677 * ATTENTION: Here, the returned values in rect are the screen
4678 * coordinates of the item just like if the menu was
4679 * always on the upper left side of the application.
4680 *
4681 */
4682BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4683 LPRECT rect)
4684{
4685 POPUPMENU *itemMenu;
4686 MENUITEM *item;
4687 HWND referenceHwnd;
4688
4689 //TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4690
4691 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4692 referenceHwnd = hwnd;
4693
4694 if(!hwnd)
4695 {
4696 itemMenu = MENU_GetMenu(hMenu);
4697 if (itemMenu == NULL) {
4698 SetLastError(ERROR_INVALID_PARAMETER);
4699 return FALSE;
4700 }
4701
4702 if(itemMenu->hWnd == 0)
4703 return FALSE;
4704 referenceHwnd = itemMenu->hWnd;
4705 }
4706
4707 if ((rect == NULL) || (item == NULL)) {
4708 SetLastError(ERROR_INVALID_PARAMETER);
4709 return FALSE;
4710 }
4711 *rect = item->rect;
4712
4713 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4714 dprintf(("USER32: GetMenuItemRect %x %x %d (%d,%d)(%d,%d)", hwnd, hMenu, uItem, rect->left, rect->top, rect->right, rect->bottom));
4715
4716 return TRUE;
4717}
4718
4719/**********************************************************************
4720 * SetMenuInfo
4721 *
4722 * FIXME
4723 * MIM_APPLYTOSUBMENUS
4724 * actually use the items to draw the menu
4725 */
4726BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4727{
4728 POPUPMENU *menu;
4729
4730 dprintf(("USER32: SetMenuInfo"));
4731 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4732
4733 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu=MENU_GetMenu(hMenu)))
4734 {
4735
4736 if (lpmi->fMask & MIM_BACKGROUND)
4737 menu->hbrBack = lpmi->hbrBack;
4738
4739 if (lpmi->fMask & MIM_HELPID)
4740 menu->dwContextHelpID = lpmi->dwContextHelpID;
4741
4742 if (lpmi->fMask & MIM_MAXHEIGHT)
4743 menu->cyMax = lpmi->cyMax;
4744
4745 if (lpmi->fMask & MIM_MENUDATA)
4746 menu->dwMenuData = lpmi->dwMenuData;
4747
4748 if (lpmi->fMask & MIM_STYLE)
4749 menu->dwStyle = lpmi->dwStyle;
4750
4751 return TRUE;
4752 }
4753 return FALSE;
4754}
4755
4756/**********************************************************************
4757 * GetMenuInfo
4758 *
4759 * NOTES
4760 * win98/NT5.0
4761 *
4762 */
4763BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4764{ POPUPMENU *menu;
4765
4766 dprintf(("USER32: GetMenuInfo"));
4767 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4768
4769 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4770 {
4771
4772 if (lpmi->fMask & MIM_BACKGROUND)
4773 lpmi->hbrBack = menu->hbrBack;
4774
4775 if (lpmi->fMask & MIM_HELPID)
4776 lpmi->dwContextHelpID = menu->dwContextHelpID;
4777
4778 if (lpmi->fMask & MIM_MAXHEIGHT)
4779 lpmi->cyMax = menu->cyMax;
4780
4781 if (lpmi->fMask & MIM_MENUDATA)
4782 lpmi->dwMenuData = menu->dwMenuData;
4783
4784 if (lpmi->fMask & MIM_STYLE)
4785 lpmi->dwStyle = menu->dwStyle;
4786
4787 return TRUE;
4788 }
4789 return FALSE;
4790}
4791
4792/**********************************************************************
4793 * SetMenuContextHelpId (USER32.488)
4794 */
4795BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4796{
4797 LPPOPUPMENU menu;
4798
4799 dprintf(("USER32: SetMenuContextHelpId"));
4800 //TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4801
4802 menu = MENU_GetMenu(hMenu);
4803 if (menu)
4804 {
4805 menu->dwContextHelpID = dwContextHelpID;
4806 return TRUE;
4807 }
4808 return FALSE;
4809}
4810
4811/**********************************************************************
4812 * GetMenuContextHelpId (USER32.488)
4813 */
4814DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4815{
4816 LPPOPUPMENU menu;
4817
4818 dprintf(("USER32: GetMenuContextHelpId"));
4819
4820 menu = MENU_GetMenu(hMenu);
4821 if (menu)
4822 {
4823 return menu->dwContextHelpID;
4824 }
4825 return 0;
4826}
4827
4828/**********************************************************************
4829 * MenuItemFromPoint (USER32.387)
4830 */
4831UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4832{
4833 dprintf(("USER32: MenuItemFromPoint not implemented!"));
4834
4835 return 0;
4836}
4837
4838/**********************************************************************
4839 * IsMenuActive (Internal)
4840 */
4841BOOL IsMenuActive(void)
4842{
4843 return pTopPopupWnd && (GetWindowLongA(pTopPopupWnd,GWL_STYLE) & WS_VISIBLE);
4844}
4845
4846BOOL POPUPMENU_Register()
4847{
4848 WNDCLASSA wndClass;
4849 BOOL rc;
4850
4851//SvL: Don't check this now
4852// if (GlobalFindAtomA(POPUPMENUCLASSNAME)) return FALSE;
4853
4854 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
4855 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
4856 wndClass.lpfnWndProc = (WNDPROC)PopupMenuWndProc;
4857 wndClass.cbClsExtra = 0;
4858 wndClass.cbWndExtra = sizeof(HMENU);
4859 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
4860 wndClass.hbrBackground = NULL_BRUSH;
4861 wndClass.lpszClassName = POPUPMENUCLASSNAME;
4862
4863 rc = RegisterClassA(&wndClass);
4864 MENU_Init();
4865
4866 return rc;
4867}
4868//******************************************************************************
4869//******************************************************************************
4870BOOL POPUPMENU_Unregister()
4871{
4872 if (GlobalFindAtomA(POPUPMENUCLASSNAME))
4873 return UnregisterClassA(POPUPMENUCLASSNAME,(HINSTANCE)NULL);
4874 else return FALSE;
4875}
4876//******************************************************************************
4877//******************************************************************************
4878
Note: See TracBrowser for help on using the repository browser.