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

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

custom build updates

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