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

Last change on this file since 9466 was 9466, checked in by sandervl, 23 years ago

PF: Support the standard system menu hotkeys in OS/2 GUI mode

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