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

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

moved drag 'n drop code to seperate file; minor updates

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