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

Last change on this file since 4517 was 4517, checked in by sandervl, 25 years ago

RegisterClassW fix + use different version of IsWindow in menu.cpp

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