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

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

Fix for painting a completely ownerdrawn menu (owner window was wrong); don't do anything in DoNCPaint if window is invisible

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