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

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

CopyImage fix for icons

File size: 145.7 KB
Line 
1/* $Id: menu.cpp,v 1.27 2000-11-21 15:17:12 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 if (flags & MF_BYPOSITION) {
1869 if (pos > menu->nItems)
1870 pos = menu->nItems;
1871 } else {
1872 if (!MENU_FindItem( &hMenu, &pos, flags ))
1873 pos = menu->nItems;
1874 else {
1875 if (!(menu = MENU_GetMenu( hMenu )))
1876 return NULL;
1877 }
1878 }
1879 /* Create new items array */
1880
1881 newItems = (MENUITEM*)HeapAlloc(GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
1882 if (!newItems)
1883 {
1884 //WARN("allocation failed\n" );
1885 return NULL;
1886 }
1887 if (menu->nItems > 0)
1888 {
1889 /* Copy the old array into the new */
1890 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1891 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1892 (menu->nItems-pos)*sizeof(MENUITEM) );
1893 HeapFree(GetProcessHeap(), 0, menu->items );
1894 }
1895 menu->items = newItems;
1896 menu->nItems++;
1897 memset( &newItems[pos], 0, sizeof(*newItems) );
1898 menu->Height = 0; /* force size recalculate */
1899 return &newItems[pos];
1900}
1901
1902
1903/**********************************************************************
1904 * MENU_ParseResource
1905 *
1906 * Parse a standard menu resource and add items to the menu.
1907 * Return a pointer to the end of the resource.
1908 */
1909static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1910{
1911 WORD flags, id = 0;
1912 LPCSTR str;
1913
1914 do
1915 {
1916 flags = GET_WORD(res);
1917 res += sizeof(WORD);
1918 if (!(flags & MF_POPUP))
1919 {
1920 id = GET_WORD(res);
1921 res += sizeof(WORD);
1922 }
1923 //if (!IS_STRING_ITEM(flags))
1924 // ERR("not a string item %04x\n", flags );
1925 str = res;
1926 if (!unicode) res += strlen(str) + 1;
1927 else res += (lstrlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1928 if (flags & MF_POPUP)
1929 {
1930 HMENU hSubMenu = CreatePopupMenu();
1931 if (!hSubMenu) return NULL;
1932 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1933 return NULL;
1934 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1935 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1936 }
1937 else /* Not a popup */
1938 {
1939 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1940 else AppendMenuW( hMenu, flags, id,
1941 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1942 }
1943 } while (!(flags & MF_END));
1944 return res;
1945}
1946
1947
1948/**********************************************************************
1949 * MENUEX_ParseResource
1950 *
1951 * Parse an extended menu resource and add items to the menu.
1952 * Return a pointer to the end of the resource.
1953 */
1954static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1955{
1956 WORD resinfo;
1957 do {
1958 MENUITEMINFOW mii;
1959
1960 mii.cbSize = sizeof(mii);
1961 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1962 mii.fType = GET_DWORD(res);
1963 res += sizeof(DWORD);
1964 mii.fState = GET_DWORD(res);
1965 res += sizeof(DWORD);
1966 mii.wID = GET_DWORD(res);
1967 res += sizeof(DWORD);
1968 resinfo = GET_WORD(res);
1969 res += sizeof(WORD);
1970 /* Align the text on a word boundary. */
1971 res += (~((int)res - 1)) & 1;
1972 mii.dwTypeData = (LPWSTR) res;
1973 res += (1 + lstrlenW(mii.dwTypeData)) * sizeof(WCHAR);
1974 /* Align the following fields on a dword boundary. */
1975 res += (~((int)res - 1)) & 3;
1976
1977 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
1978 {
1979 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
1980 0, mii.dwTypeData);
1981 //TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1982 // mii.fType, mii.fState, mii.wID, resinfo, newstr);
1983 HeapFree( GetProcessHeap(), 0, newstr );
1984 }
1985
1986 if (resinfo & 1) { /* Pop-up? */
1987 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
1988 res += sizeof(DWORD);
1989 mii.hSubMenu = CreatePopupMenu();
1990 if (!mii.hSubMenu)
1991 return NULL;
1992 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1993 DestroyMenu(mii.hSubMenu);
1994 return NULL;
1995 }
1996 mii.fMask |= MIIM_SUBMENU;
1997 mii.fType |= MF_POPUP;
1998 }
1999 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2000 } while (!(resinfo & MF_END));
2001 return res;
2002}
2003
2004
2005/***********************************************************************
2006 * MENU_GetSubPopup
2007 *
2008 * Return the handle of the selected sub-popup menu (if any).
2009 */
2010static HMENU MENU_GetSubPopup( HMENU hmenu )
2011{
2012 POPUPMENU *menu;
2013 MENUITEM *item;
2014
2015 menu = MENU_GetMenu(hmenu);
2016
2017 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2018
2019 item = &menu->items[menu->FocusedItem];
2020 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2021 return item->hSubMenu;
2022 return 0;
2023}
2024
2025
2026/***********************************************************************
2027 * MENU_HideSubPopups
2028 *
2029 * Hide the sub-popup menus of this menu.
2030 */
2031static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2032 BOOL sendMenuSelect )
2033{
2034 POPUPMENU *menu = MENU_GetMenu(hmenu);
2035
2036 //TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2037
2038 if (menu && uSubPWndLevel)
2039 {
2040 HMENU hsubmenu;
2041 POPUPMENU *submenu;
2042 MENUITEM *item;
2043
2044 if (menu->FocusedItem != NO_SELECTED_ITEM)
2045 {
2046 item = &menu->items[menu->FocusedItem];
2047 if (!(item->fType & MF_POPUP) ||
2048 !(item->fState & MF_MOUSESELECT)) return;
2049 item->fState &= ~MF_MOUSESELECT;
2050 hsubmenu = item->hSubMenu;
2051 } else return;
2052
2053 submenu = MENU_GetMenu(hsubmenu);
2054 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2055 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2056
2057 if (submenu->hWnd == MENU_GetTopPopupWnd() )
2058 {
2059 ShowWindow( submenu->hWnd, SW_HIDE );
2060 uSubPWndLevel = 0;
2061 }
2062 else
2063 {
2064 DestroyWindow( submenu->hWnd );
2065 submenu->hWnd = 0;
2066 }
2067 MENU_ReleaseTopPopupWnd();
2068 }
2069}
2070
2071
2072/***********************************************************************
2073 * MENU_ShowSubPopup
2074 *
2075 * Display the sub-menu of the selected item of this menu.
2076 * Return the handle of the submenu, or hmenu if no submenu to display.
2077 */
2078static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2079 BOOL selectFirst, UINT wFlags,POINT *pt)
2080{
2081 RECT rect;
2082 POPUPMENU *menu;
2083 MENUITEM *item;
2084 HDC hdc;
2085
2086 //TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, selectFirst);
2087
2088 if (!(menu = MENU_GetMenu(hmenu))) return hmenu;
2089
2090 if (menu->FocusedItem == NO_SELECTED_ITEM)
2091 {
2092 return hmenu;
2093 }
2094
2095 item = &menu->items[menu->FocusedItem];
2096 if (!(item->fType & MF_POPUP) ||
2097 (item->fState & (MF_GRAYED | MF_DISABLED)))
2098 {
2099 return hmenu;
2100 }
2101
2102 /* message must be send before using item,
2103 because nearly everything may by changed by the application ! */
2104
2105 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2106 if (!(wFlags & TPM_NONOTIFY))
2107 SendMessageA( hwndOwner, WM_INITMENUPOPUP, item->hSubMenu,
2108 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2109
2110 item = &menu->items[menu->FocusedItem];
2111 rect = item->rect;
2112
2113 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
2114 if (!(item->fState & MF_HILITE))
2115 {
2116 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2117 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2118
2119 SelectObject( hdc, hMenuFont);
2120
2121 item->fState |= MF_HILITE;
2122 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2123 ReleaseDC( menu->hWnd, hdc );
2124 }
2125 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2126 item->rect = rect;
2127
2128 item->fState |= MF_MOUSESELECT;
2129
2130 if (IS_SYSTEM_MENU(menu))
2131 {
2132 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(menu->hWnd);
2133
2134 MENU_InitSysMenuPopup(item->hSubMenu,GetWindowLongA(menu->hWnd,GWL_STYLE), GetClassLongA(menu->hWnd, GCL_STYLE));
2135
2136 if ((wFlags & TPM_CAPTIONSYSMENU) && pt)
2137 {
2138 rect.top = pt->y;
2139 rect.left = pt->x;
2140 rect.bottom = rect.right = 0;
2141 } else
2142 {
2143 if (win32wnd) win32wnd->GetSysPopupPos(&rect);
2144 rect.top = rect.bottom;
2145 rect.right = GetSystemMetrics(SM_CXSIZE);
2146 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2147 }
2148 }
2149 else
2150 {
2151 if (menu->wFlags & MF_POPUP)
2152 {
2153 RECT rectWindow;
2154
2155 GetWindowRect(menu->hWnd,&rectWindow);
2156 rect.left = rectWindow.left + item->rect.right;
2157 rect.top = rectWindow.top + item->rect.top;
2158 rect.right = item->rect.left - item->rect.right;
2159 rect.bottom = item->rect.top - item->rect.bottom;
2160 }
2161 else
2162 {
2163 RECT rectWindow;
2164
2165 GetWindowRect(menu->hWnd,&rectWindow);
2166 rect.left = rectWindow.left + item->rect.left;
2167 rect.top = rectWindow.top + item->rect.bottom;
2168 rect.right = item->rect.right - item->rect.left;
2169 rect.bottom = item->rect.bottom - item->rect.top;
2170 }
2171 }
2172
2173 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2174 rect.left, rect.top, rect.right, rect.bottom );
2175 if (selectFirst)
2176 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2177 return item->hSubMenu;
2178}
2179
2180/***********************************************************************
2181 * MENU_PtMenu
2182 *
2183 * Walks menu chain trying to find a menu pt maps to.
2184 */
2185static HMENU MENU_PtMenu(HMENU hMenu,POINT pt,BOOL inMenuBar)
2186{
2187 POPUPMENU *menu = MENU_GetMenu(hMenu);
2188 register UINT ht = menu->FocusedItem;
2189
2190 /* try subpopup first (if any) */
2191 ht = (ht != NO_SELECTED_ITEM &&
2192 (menu->items[ht].fType & MF_POPUP) &&
2193 (menu->items[ht].fState & MF_MOUSESELECT))
2194 ? (UINT) MENU_PtMenu(menu->items[ht].hSubMenu,pt,inMenuBar) : 0;
2195
2196 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
2197 {
2198 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(menu->hWnd);
2199 if(win32wnd==NULL) {
2200 //SvL: This happens in Moraff's YourJongg 2.0, return here
2201 //TODO: Check if this is supposed to happen at all...
2202 return (HMENU)0;
2203 }
2204
2205 ht = win32wnd->HandleNCHitTest(pt);
2206 if( menu->wFlags & MF_POPUP )
2207 ht = (ht != (UINT)HTNOWHERE &&
2208 ht != (UINT)HTERROR) ? (UINT)hMenu : 0;
2209 else
2210 {
2211 ht = ((ht == HTSYSMENU) && !inMenuBar) ? (UINT)(getSysMenu(menu->hWnd))
2212 : ((ht == HTMENU) && inMenuBar) ? (UINT)(getMenu(menu->hWnd)) : 0;
2213 }
2214 }
2215 return (HMENU)ht;
2216}
2217
2218/***********************************************************************
2219 * MENU_ExecFocusedItem
2220 *
2221 * Execute a menu item (for instance when user pressed Enter).
2222 * Return the wID of the executed item. Otherwise, -1 indicating
2223 * that no menu item wase executed;
2224 * Have to receive the flags for the TrackPopupMenu options to avoid
2225 * sending unwanted message.
2226 *
2227 */
2228static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2229{
2230 MENUITEM *item;
2231 POPUPMENU *menu = MENU_GetMenu(hMenu);
2232
2233 //TRACE("%p hmenu=0x%04x\n", pmt, hMenu);
2234
2235 if (!menu || !menu->nItems ||
2236 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2237
2238 item = &menu->items[menu->FocusedItem];
2239
2240 //TRACE("%08x %08x %08x\n",
2241 // hMenu, item->wID, item->hSubMenu);
2242
2243 if (!(item->fType & MF_POPUP))
2244 {
2245 if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
2246 {
2247 /* If TPM_RETURNCMD is set you return the id, but
2248 do not send a message to the owner */
2249 if(!(wFlags & TPM_RETURNCMD))
2250 {
2251 if( menu->wFlags & MF_SYSMENU )
2252 PostMessageA( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2253 MAKELPARAM(pmt->pt.x, pmt->pt.y) );
2254 else
2255 PostMessageA( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2256 }
2257 return item->wID;
2258 }
2259 }
2260 else
2261 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags,&pmt->pt);
2262
2263 return -1;
2264}
2265
2266/***********************************************************************
2267 * MENU_SwitchTracking
2268 *
2269 * Helper function for menu navigation routines.
2270 */
2271static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2272{
2273 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2274 POPUPMENU *topmenu = MENU_GetMenu(pmt->hTopMenu);
2275
2276 //TRACE("%p hmenu=0x%04x 0x%04x\n", pmt, hPtMenu, id);
2277
2278 if( pmt->hTopMenu != hPtMenu &&
2279 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2280 {
2281 /* both are top level menus (system and menu-bar) */
2282 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2283 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2284 pmt->hTopMenu = hPtMenu;
2285 }
2286 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2287 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2288}
2289
2290
2291/***********************************************************************
2292 * MENU_ButtonDown
2293 *
2294 * Return TRUE if we can go on with menu tracking.
2295 */
2296static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2297{
2298 //TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2299
2300 if (hPtMenu)
2301 {
2302 UINT id = 0;
2303 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2304 MENUITEM *item;
2305
2306 if( IS_SYSTEM_MENU(ptmenu) )
2307 item = ptmenu->items;
2308 else
2309 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2310
2311 if( item )
2312 {
2313 if( ptmenu->FocusedItem != id )
2314 MENU_SwitchTracking( pmt, hPtMenu, id );
2315
2316 /* If the popup menu is not already "popped" */
2317 if(!(item->fState & MF_MOUSESELECT ))
2318 {
2319 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,hPtMenu,FALSE,wFlags,&pmt->pt);
2320 }
2321
2322 return TRUE;
2323 }
2324 /* Else the click was on the menu bar, finish the tracking */
2325 }
2326 return FALSE;
2327}
2328
2329/***********************************************************************
2330 * MENU_ButtonUp
2331 *
2332 * Return the value of MENU_ExecFocusedItem if
2333 * the selected item was not a popup. Else open the popup.
2334 * A -1 return value indicates that we go on with menu tracking.
2335 *
2336 */
2337static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2338{
2339 //TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2340
2341 if (hPtMenu)
2342 {
2343 UINT id = 0;
2344 POPUPMENU *ptmenu = MENU_GetMenu(hPtMenu);
2345 MENUITEM *item;
2346
2347 if( IS_SYSTEM_MENU(ptmenu) )
2348 item = ptmenu->items;
2349 else
2350 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2351
2352 if( item && (ptmenu->FocusedItem == id ))
2353 {
2354 if( !(item->fType & MF_POPUP) )
2355 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2356
2357 /* If we are dealing with the top-level menu and that this */
2358 /* is a click on an already "poppped" item */
2359 /* Stop the menu tracking and close the opened submenus */
2360 if((pmt->hTopMenu == hPtMenu) && (ptmenu->bTimeToHide == TRUE))
2361 return 0;
2362 }
2363 ptmenu->bTimeToHide = TRUE;
2364 }
2365 return -1;
2366}
2367
2368
2369/***********************************************************************
2370 * MENU_MouseMove
2371 *
2372 * Return TRUE if we can go on with menu tracking.
2373 */
2374static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2375{
2376 UINT id = NO_SELECTED_ITEM;
2377 POPUPMENU *ptmenu = NULL;
2378
2379 if( hPtMenu )
2380 {
2381 ptmenu = MENU_GetMenu(hPtMenu);
2382 if( IS_SYSTEM_MENU(ptmenu) )
2383 id = 0;
2384 else
2385 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2386 }
2387
2388 if( id == NO_SELECTED_ITEM )
2389 {
2390 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2391 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2392
2393 }
2394 else if( ptmenu->FocusedItem != id )
2395 {
2396 POPUPMENU *menu;
2397 MENUITEM *item;
2398
2399 MENU_SwitchTracking( pmt, hPtMenu, id );
2400
2401
2402 /*
2403 Test to see if we are trying to popup a submenu or not.
2404 If we aren't, don't change the current menu pointer
2405 and return.
2406 */
2407 if (!(menu = (POPUPMENU *)MENU_GetMenu( hPtMenu )))
2408 {
2409 pmt->hCurrentMenu = hPtMenu;
2410 return TRUE;
2411 }
2412
2413 if (!IsWindow( menu->hWnd ) ||
2414 (menu->FocusedItem == NO_SELECTED_ITEM))
2415 {
2416 pmt->hCurrentMenu = hPtMenu;
2417 return TRUE;
2418 }
2419
2420 item = &menu->items[menu->FocusedItem];
2421 if (!(item->fType & MF_POPUP) ||
2422 (item->fState & (MF_GRAYED | MF_DISABLED)))
2423 {
2424 pmt->hCurrentMenu = hPtMenu;
2425 return TRUE;
2426 }
2427
2428 /* Check to see if we are trying to popup a toplevel menu or a
2429 submenu. Only the submenu has a delay.
2430 */
2431 if (uSubPWndLevel)
2432 {
2433 /*
2434 If we made it here, we want to pop up a submenu. Before we pop it up,
2435 we want a slight delay. This is implemented by remembering the ID of the menu
2436 where the mouse is currently positioned, and setting up a timer. When the
2437 timer fires (handled in MENU_TrackMenu() ), if the mouse is over the same
2438 submenu item, we popup it up. Otherwise, we do nothing.
2439 */
2440 KillTimer (pmt->hOwnerWnd, SUBMENU_POPUP_TIMERID); /* Just in case another timer was set up and not fired yet... */
2441 if ( (SetTimer (pmt->hOwnerWnd, SUBMENU_POPUP_TIMERID, POPUP_MENU_DELAY, NULL)) != SUBMENU_POPUP_TIMERID)
2442 {
2443 /*
2444 For some reason the timer wasn't set up properly... Revert to old
2445 functionality.
2446 */
2447 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,hPtMenu,FALSE,wFlags,&pmt->pt);
2448 return TRUE;
2449 }
2450 } else
2451 {
2452 /* We are trying to popup a top level menu... so no delay */
2453
2454 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags,&pmt->pt);
2455 return TRUE;
2456 }
2457
2458 mouseOverMenuID = id;
2459 isTimerSet = TRUE;
2460 }
2461 return TRUE;
2462}
2463
2464
2465/***********************************************************************
2466 * MENU_DoNextMenu
2467 *
2468 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2469 */
2470static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2471{
2472 POPUPMENU *menu = MENU_GetMenu(pmt->hTopMenu);
2473
2474 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2475 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2476 {
2477 HMENU hNewMenu;
2478 HWND hNewWnd;
2479 UINT id = 0;
2480 LRESULT l = SendMessageA( pmt->hOwnerWnd, WM_NEXTMENU, vk,
2481 (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu );
2482
2483 //TRACE("%04x [%04x] -> %04x [%04x]\n",
2484 // (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2485
2486 if( l == 0 )
2487 {
2488 hNewWnd = pmt->hOwnerWnd;
2489 if( IS_SYSTEM_MENU(menu) )
2490 {
2491 /* switch to the menu bar */
2492
2493 if( (GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_CHILD) || !getMenu(pmt->hOwnerWnd) )
2494 {
2495 return FALSE;
2496 }
2497
2498 hNewMenu = getMenu(pmt->hOwnerWnd);
2499 if( vk == VK_LEFT )
2500 {
2501 menu = MENU_GetMenu(hNewMenu);
2502 id = menu->nItems - 1;
2503 }
2504 }
2505 else if( GetWindowLongA(pmt->hOwnerWnd,GWL_STYLE) & WS_SYSMENU )
2506 {
2507 /* switch to the system menu */
2508 hNewMenu = getSysMenu(pmt->hOwnerWnd);
2509 }
2510 else
2511 {
2512 return FALSE;
2513 }
2514 }
2515 else /* application returned a new menu to switch to */
2516 {
2517 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2518
2519 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2520 {
2521 if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_SYSMENU) &&
2522 GetSubMenu(getSysMenu(hNewWnd), 0) == hNewMenu )
2523 {
2524 /* get the real system menu */
2525 hNewMenu = getSysMenu(hNewWnd);
2526 }
2527 else if( (GetWindowLongA(hNewWnd,GWL_STYLE) & WS_CHILD) || (getMenu(hNewWnd) != hNewMenu) )
2528 {
2529 /* FIXME: Not sure what to do here, perhaps,
2530 * try to track hNewMenu as a popup? */
2531
2532 //TRACE(" -- got confused.\n");
2533 return FALSE;
2534 }
2535 }
2536 else return FALSE;
2537 }
2538
2539 if( hNewMenu != pmt->hTopMenu )
2540 {
2541 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2542 FALSE, 0 );
2543 if( pmt->hCurrentMenu != pmt->hTopMenu )
2544 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2545 }
2546
2547 if( hNewWnd != pmt->hOwnerWnd )
2548 {
2549 ReleaseCapture();
2550 pmt->hOwnerWnd = hNewWnd;
2551 SetCapture(pmt->hOwnerWnd); //SvL: Don't know if this is good enough
2552 //EVENT_Capture( pmt->hOwnerWnd, HTMENU ); //CB: todo
2553 }
2554
2555 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2556 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2557
2558 return TRUE;
2559 }
2560 return FALSE;
2561}
2562
2563/***********************************************************************
2564 * MENU_SuspendPopup
2565 *
2566 * The idea is not to show the popup if the next input message is
2567 * going to hide it anyway.
2568 */
2569static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT uMsg )
2570{
2571 MSG msg;
2572
2573 msg.hwnd = pmt->hOwnerWnd;
2574
2575 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2576 pmt->trackFlags |= TF_SKIPREMOVE;
2577
2578 switch( uMsg )
2579 {
2580 case WM_KEYDOWN:
2581 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2582 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2583 {
2584 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2585 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2586 if( msg.message == WM_KEYDOWN &&
2587 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2588 {
2589 pmt->trackFlags |= TF_SUSPENDPOPUP;
2590 return TRUE;
2591 }
2592 }
2593 break;
2594 }
2595
2596 /* failures go through this */
2597 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2598 return FALSE;
2599}
2600
2601/***********************************************************************
2602 * MENU_KeyLeft
2603 *
2604 * Handle a VK_LEFT key event in a menu.
2605 */
2606static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2607{
2608 POPUPMENU *menu;
2609 HMENU hmenutmp, hmenuprev;
2610 UINT prevcol;
2611
2612 hmenuprev = hmenutmp = pmt->hTopMenu;
2613 menu = MENU_GetMenu(hmenutmp);
2614
2615 /* Try to move 1 column left (if possible) */
2616 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2617 NO_SELECTED_ITEM ) {
2618
2619 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2620 prevcol, TRUE, 0 );
2621 return;
2622 }
2623
2624 /* close topmost popup */
2625 while (hmenutmp != pmt->hCurrentMenu)
2626 {
2627 hmenuprev = hmenutmp;
2628 hmenutmp = MENU_GetSubPopup( hmenuprev );
2629 }
2630
2631 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2632 pmt->hCurrentMenu = hmenuprev;
2633
2634 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2635 {
2636 /* move menu bar selection if no more popups are left */
2637
2638 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2639 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2640
2641 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2642 {
2643 /* A sublevel menu was displayed - display the next one
2644 * unless there is another displacement coming up */
2645
2646 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2647 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2648 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2649 }
2650 }
2651}
2652
2653
2654/***********************************************************************
2655 * MENU_KeyRight
2656 *
2657 * Handle a VK_RIGHT key event in a menu.
2658 */
2659static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2660{
2661 HMENU hmenutmp;
2662 POPUPMENU *menu = MENU_GetMenu(pmt->hTopMenu);
2663 UINT nextcol;
2664
2665 //TRACE("MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2666 // pmt->hCurrentMenu,
2667 // ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2668 // items[0].text,
2669 // pmt->hTopMenu, menu->items[0].text );
2670
2671 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2672 {
2673 /* If already displaying a popup, try to display sub-popup */
2674
2675 hmenutmp = pmt->hCurrentMenu;
2676 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags,&pmt->pt);
2677
2678 /* if subpopup was displayed then we are done */
2679 if (hmenutmp != pmt->hCurrentMenu) return;
2680 }
2681
2682 /* Check to see if there's another column */
2683 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2684 NO_SELECTED_ITEM ) {
2685 //TRACE("Going to %d.\n", nextcol );
2686 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2687 nextcol, TRUE, 0 );
2688 return;
2689 }
2690
2691 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2692 {
2693 if( pmt->hCurrentMenu != pmt->hTopMenu )
2694 {
2695 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2696 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2697 } else hmenutmp = 0;
2698
2699 /* try to move to the next item */
2700 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2701 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2702
2703 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2704 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2705 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2706 pmt->hTopMenu, TRUE, wFlags,&pmt->pt);
2707 }
2708}
2709
2710VOID MENU_DispatchMouseMsg(MSG *msg)
2711{
2712 LONG hittest;
2713
2714 hittest = SendMessageA(msg->hwnd,WM_NCHITTEST,0,MAKELONG(msg->pt.x,msg->pt.y));
2715 if (hittest != HTCLIENT)
2716 SendMessageA(msg->hwnd,msg->message+WM_NCMOUSEMOVE-WM_MOUSEMOVE,hittest,MAKELONG(msg->pt.x,msg->pt.y));
2717 else
2718 DispatchMessageA(msg);
2719}
2720
2721/***********************************************************************
2722 * MENU_TrackMenu
2723 *
2724 * Menu tracking code.
2725 */
2726static INT MENU_TrackMenu(HMENU hmenu,UINT wFlags,INT x,INT y,HWND hwnd,BOOL inMenuBar,const RECT *lprect)
2727{
2728 MSG msg;
2729 POPUPMENU *menu;
2730 BOOL fRemove;
2731 INT executedMenuId = -1;
2732 MTRACKER mt;
2733 BOOL enterIdleSent = FALSE;
2734 BOOL bSysMenu;
2735
2736 mt.trackFlags = 0;
2737 mt.hCurrentMenu = hmenu;
2738 mt.hTopMenu = hmenu;
2739 mt.hOwnerWnd = hwnd;
2740 mt.pt.x = x;
2741 mt.pt.y = y;
2742
2743 //TRACE("hmenu=0x%04x flags=0x%08x (%d,%d) hwnd=0x%04x (%d,%d)-(%d,%d)\n",
2744 // hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2745 // (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2746
2747 fEndMenu = FALSE;
2748 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
2749
2750 bSysMenu = IS_SYSTEM_MENU(menu);
2751
2752 if (wFlags & TPM_BUTTONDOWN)
2753 {
2754 /* Get the result in order to start the tracking or not */
2755 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2756 fEndMenu = !fRemove;
2757 }
2758
2759 //EVENT_Capture( mt.hOwnerWnd, HTMENU ); //CB: todo
2760 //SvL: Set keyboard & mouse event capture
2761 SetCapture(mt.hOwnerWnd);
2762
2763 while (!fEndMenu)
2764 {
2765 menu = MENU_GetMenu(mt.hCurrentMenu);
2766 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2767
2768 /* we have to keep the message in the queue until it's
2769 * clear that menu loop is not over yet. */
2770// if (!GetMessageA(&msg,msg.hwnd,0,0)) break;
2771 //SvL: Getting messages for only the menu delays background paints (i.e. VPBuddy logo)
2772 if (!GetMessageA(&msg,0,0,0)) break;
2773 TranslateMessage( &msg );
2774 mt.pt = msg.pt;
2775
2776 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2777 enterIdleSent=FALSE;
2778
2779 fRemove = FALSE;
2780 if((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2781 {
2782 /* Find a menu for this mouse event */
2783 POINT pt = msg.pt;
2784
2785 hmenu = MENU_PtMenu(mt.hTopMenu,pt,inMenuBar);
2786
2787 //CB: todo: solve conflicts with OS/2's single message queue architecture
2788
2789 switch(msg.message)
2790 {
2791 /* no WM_NC... messages in captured state */
2792
2793 case WM_RBUTTONDBLCLK:
2794 case WM_RBUTTONDOWN:
2795 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2796 goto buttondown;
2797 case WM_LBUTTONDBLCLK:
2798 if (bSysMenu && (hmenu == mt.hTopMenu))
2799 {
2800 fEndMenu = TRUE;
2801 break;
2802 }
2803 /* fall through */
2804 case WM_LBUTTONDOWN:
2805 /* If the message belongs to the menu, removes it from the queue */
2806 /* Else, end menu tracking */
2807
2808 buttondown:
2809 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2810 mouseOverMenuID = -1;
2811 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2812 fEndMenu = !fRemove;
2813 break;
2814
2815 case WM_RBUTTONUP:
2816 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2817 /* fall through */
2818 case WM_LBUTTONUP:
2819 /* Check if a menu was selected by the mouse */
2820 if (hmenu)
2821 {
2822 /* Forcing mouse popup NOW - Ensure timer doesn't popup menu also */
2823 mouseOverMenuID = -1;
2824 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2825
2826 /* End the loop if executedMenuId is an item ID */
2827 /* or if the job was done (executedMenuId = 0). */
2828 fEndMenu = fRemove = (executedMenuId != -1);
2829 }
2830 /* No menu was selected by the mouse */
2831 /* if the function was called by TrackPopupMenu, continue
2832 with the menu tracking. If not, stop it */
2833 else
2834 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2835
2836 break;
2837
2838 case WM_MOUSEMOVE:
2839 /* In win95 winelook, the selected menu item must be changed every time the
2840 mouse moves. In Win31 winelook, the mouse button has to be held down */
2841
2842 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2843 } /* switch(msg.message) - mouse */
2844 }
2845 else if (msg.message == WM_TIMER)
2846 {
2847 UINT id = -1;
2848 POPUPMENU *ptmenu = NULL;
2849
2850 if (isTimerSet)
2851 {
2852 /*
2853 If we get here, an attempt was made to pop up a submenu.
2854 (See MENU_MouseMove() )
2855 */
2856
2857 /* Get the ID of the menu item the mouse is over now. */
2858 if( hmenu )
2859 {
2860 ptmenu = (POPUPMENU *)MENU_GetMenu( hmenu );
2861 if( IS_SYSTEM_MENU(ptmenu) )
2862 id = 0;
2863 else
2864 MENU_FindItemByCoords( ptmenu, mt.pt, &id );
2865
2866 /* If it is over the same item that set up the timer originally .... */
2867 if (mouseOverMenuID != -1 && mouseOverMenuID == id)
2868 {
2869 /* .... Pop up the menu */
2870 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, hmenu, FALSE, wFlags,&mt.pt);
2871 }
2872 }
2873
2874 /* Reset the timer so it doesn't fire again. (So we are ready for the next
2875 attempt to popup a submenu... ) */
2876 KillTimer(mt.hOwnerWnd,SUBMENU_POPUP_TIMERID);
2877 isTimerSet = FALSE;
2878 }
2879 }
2880 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2881 {
2882 fRemove = TRUE; /* Keyboard messages are always removed */
2883 switch(msg.message)
2884 {
2885 case WM_KEYDOWN:
2886 switch(msg.wParam)
2887 {
2888 case VK_HOME:
2889 case VK_END:
2890 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2891 NO_SELECTED_ITEM, FALSE, 0 );
2892 /* fall through */
2893 case VK_UP:
2894 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2895 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2896 break;
2897
2898 case VK_DOWN: /* If on menu bar, pull-down the menu */
2899
2900 menu = MENU_GetMenu(mt.hCurrentMenu);
2901 if (!(menu->wFlags & MF_POPUP))
2902 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags,&mt.pt);
2903 else /* otherwise try to move selection */
2904 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2905 break;
2906
2907 case VK_LEFT:
2908 MENU_KeyLeft( &mt, wFlags );
2909 break;
2910
2911 case VK_RIGHT:
2912 MENU_KeyRight( &mt, wFlags );
2913 break;
2914
2915 case VK_ESCAPE:
2916 fEndMenu = TRUE;
2917 break;
2918
2919 case VK_F1:
2920 {
2921 HELPINFO hi;
2922 hi.cbSize = sizeof(HELPINFO);
2923 hi.iContextType = HELPINFO_MENUITEM;
2924 if (menu->FocusedItem == NO_SELECTED_ITEM)
2925 hi.iCtrlId = 0;
2926 else
2927 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2928 hi.hItemHandle = hmenu;
2929 hi.dwContextId = menu->dwContextHelpID;
2930 hi.MousePos = msg.pt;
2931 //TRACE_(winhelp)("Sending HELPINFO_MENUITEM to 0x%08x\n", hwnd);
2932 SendMessageA(hwnd, WM_HELP, 0, (LPARAM)&hi);
2933 break;
2934 }
2935
2936 default:
2937 break;
2938 }
2939 break; /* WM_KEYDOWN */
2940
2941 case WM_SYSKEYDOWN:
2942 switch(msg.wParam)
2943 {
2944 case VK_MENU:
2945 fEndMenu = TRUE;
2946 break;
2947
2948 }
2949 break; /* WM_SYSKEYDOWN */
2950
2951 case WM_CHAR:
2952 {
2953 UINT pos;
2954
2955 if (msg.wParam == '\r' || msg.wParam == ' ')
2956 {
2957 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2958 fEndMenu = (executedMenuId != -1);
2959
2960 break;
2961 }
2962
2963 /* Hack to avoid control chars. */
2964 /* We will find a better way real soon... */
2965 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2966
2967 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2968 LOWORD(msg.wParam), FALSE );
2969 if (pos == (UINT)-2) fEndMenu = TRUE;
2970 else if (pos == (UINT)-1) MessageBeep(0);
2971 else
2972 {
2973 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
2974 TRUE, 0 );
2975 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2976 fEndMenu = (executedMenuId != -1);
2977 }
2978 }
2979 break;
2980 } /* switch(msg.message) - kbd */
2981 }
2982 else if (msg.message == WM_SYSCOMMAND)
2983 {
2984 /* The user clicked on the system menu/button */
2985 fEndMenu = TRUE;
2986 break;
2987 }
2988 else
2989 {
2990 DispatchMessageA( &msg );
2991 }
2992
2993 if (!fEndMenu) fRemove = TRUE;
2994
2995 /* finally remove message from the queue */
2996
2997 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2998 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
2999 else mt.trackFlags &= ~TF_SKIPREMOVE;
3000 }
3001
3002 ReleaseCapture();
3003
3004 menu = MENU_GetMenu(mt.hTopMenu);
3005
3006 if( IsWindow( mt.hOwnerWnd ) )
3007 {
3008 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
3009
3010 if (menu && menu->wFlags & MF_POPUP)
3011 {
3012 ShowWindow( menu->hWnd, SW_HIDE );
3013 uSubPWndLevel = 0;
3014 }
3015 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3016 SendMessageA( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
3017 }
3018
3019 /* Reset the variable for hiding menu */
3020 menu->bTimeToHide = FALSE;
3021
3022 /* The return value is only used by TrackPopupMenu */
3023 return ((executedMenuId != -1) ? executedMenuId : 0);
3024}
3025
3026/***********************************************************************
3027 * MENU_InitTracking
3028 */
3029static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3030{
3031 //TRACE("hwnd=0x%04x hmenu=0x%04x\n", hWnd, hMenu);
3032
3033 HideCaret(0);
3034
3035 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3036 if (!(wFlags & TPM_NONOTIFY))
3037 SendMessageA( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3038
3039 SendMessageA( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
3040
3041 if (!(wFlags & TPM_NONOTIFY))
3042 SendMessageA( hWnd, WM_INITMENU, hMenu, 0 );
3043
3044 return TRUE;
3045}
3046/***********************************************************************
3047 * MENU_ExitTracking
3048 */
3049static BOOL MENU_ExitTracking(HWND hWnd)
3050{
3051 //TRACE("hwnd=0x%04x\n", hWnd);
3052
3053 SendMessageA( hWnd, WM_EXITMENULOOP, 0, 0 );
3054 ShowCaret(0);
3055 return TRUE;
3056}
3057
3058/***********************************************************************
3059 * MENU_TrackMouseMenuBar
3060 *
3061 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3062 */
3063void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3064{
3065 HMENU hMenu = (ht == 0) ? getMenu(hWnd):getSysMenu(hWnd);
3066 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3067
3068 //TRACE("pwnd=%p ht=0x%04x (%ld,%ld)\n", wndPtr, ht, pt.x, pt.y);
3069
3070 if (IsMenu(hMenu))
3071 {
3072 if (ht == HTCAPTION) wFlags |= TPM_CAPTIONSYSMENU | TPM_RIGHTBUTTON;
3073 if (IsIconic(hWnd)) wFlags |= TPM_BOTTOMALIGN; //CB: todo: for minimized windows
3074
3075 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3076 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd,ht == 0, NULL );
3077 MENU_ExitTracking(hWnd);
3078 }
3079}
3080
3081MENUITEM *MENU_HighlightedItem=NULL;
3082UINT MENU_HighlightedItemID=NO_SELECTED_ITEM;
3083POPUPMENU *MENU_HighlightedMenu=NULL;
3084
3085void MENU_TrackMouseMenuBar_MouseMove(HWND hwnd,POINT pt,BOOL OnMenu)
3086{
3087 HMENU hMenu = getMenu(hwnd);
3088 MENUITEM *item = NULL;
3089 RECT rect;
3090 HDC hdc;
3091 BOOL UnHighlight = (OnMenu==TRUE)?FALSE:TRUE;
3092 POPUPMENU *ptmenu = NULL;
3093 UINT id = NO_SELECTED_ITEM;
3094
3095 if (OnMenu == TRUE && IsMenu(hMenu)) {
3096
3097 ptmenu=(POPUPMENU *)MENU_GetMenu( hMenu );
3098
3099 item=MENU_FindItemByCoords( ptmenu, pt, &id );
3100
3101 if(!item) {
3102 /* No item selected, perhaps we are on the blank spot? */
3103 UnHighlight = TRUE;
3104 /* Paranoid setting */
3105 item=NULL;
3106 } else if(id == MENU_HighlightedItemID) {
3107 /* If it's already highlighted, don't do it again */
3108 return;
3109 } else if(item->fState & MF_MOUSESELECT) {
3110 /* If it's dropped, we let the DrawMenuItem draw the sunken border */
3111 return;
3112 } else if(item->fType & MF_BITMAP) {
3113 UnHighlight = TRUE;
3114 /* (Required) Paranoid setting */
3115 item=NULL;
3116 } else {
3117 hdc = GetDCEx( ptmenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3118 rect = item->rect;
3119 DrawEdge(hdc, &rect, BDR_RAISEDINNER, BF_RECT);
3120 ReleaseDC( ptmenu->hWnd, hdc );
3121
3122 UnHighlight = TRUE;
3123 }
3124 }
3125
3126 if(UnHighlight == TRUE) {
3127 if(MENU_HighlightedItem) {
3128 if(!(MENU_HighlightedItem->fState & MF_MOUSESELECT)) {
3129 hdc = GetDCEx( MENU_HighlightedMenu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
3130 rect = MENU_HighlightedItem->rect;
3131
3132 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_MENU));
3133 ReleaseDC( MENU_HighlightedMenu->hWnd, hdc );
3134 }
3135 }
3136
3137 /* Sets to NULL, NO_SELECTED_ITEM, NULL, unless it found a new
3138 item, then it will be filled in with the proper values */
3139 MENU_HighlightedItem = item;
3140 MENU_HighlightedItemID = id;
3141 MENU_HighlightedMenu= ptmenu;
3142 }
3143}
3144
3145/***********************************************************************
3146 * MENU_TrackKbdMenuBar
3147 *
3148 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3149 */
3150void MENU_TrackKbdMenuBar( HWND hWnd, UINT wParam, INT vkey)
3151{
3152 UINT uItem = NO_SELECTED_ITEM;
3153 HMENU hTrackMenu;
3154 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3155
3156 /* find window that has a menu */
3157
3158 while(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD)
3159 if( !(hWnd = GetParent(hWnd)) ) return;
3160
3161 /* check if we have to track a system menu */
3162
3163 if( (GetWindowLongA(hWnd,GWL_STYLE) & (WS_CHILD | WS_MINIMIZE)) ||
3164 !getMenu(hWnd) || vkey == VK_SPACE )
3165 {
3166 if( !(GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) ) return;
3167 hTrackMenu = getSysMenu(hWnd);
3168 uItem = 0;
3169 wParam |= HTSYSMENU; /* prevent item lookup */
3170 }
3171 else
3172 hTrackMenu = getMenu(hWnd);
3173
3174 if (IsMenu( hTrackMenu ))
3175 {
3176 MENU_InitTracking( hWnd, hTrackMenu, FALSE, wFlags );
3177
3178 if( vkey && vkey != VK_SPACE )
3179 {
3180 uItem = MENU_FindItemByKey( hWnd, hTrackMenu,
3181 vkey, (wParam & HTSYSMENU) );
3182 if( uItem >= (UINT)(-2) )
3183 {
3184 if( uItem == (UINT)(-1) ) MessageBeep(0);
3185 hTrackMenu = 0;
3186 }
3187 }
3188
3189 if( hTrackMenu )
3190 {
3191 MENU_SelectItem( hWnd, hTrackMenu, uItem, TRUE, 0 );
3192
3193 if( uItem == NO_SELECTED_ITEM )
3194 MENU_MoveSelection( hWnd, hTrackMenu, ITEM_NEXT );
3195 else if( vkey )
3196 PostMessageA( hWnd, WM_KEYDOWN, VK_DOWN, 0L );
3197
3198 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hWnd,TRUE, NULL );
3199 }
3200
3201 MENU_ExitTracking (hWnd);
3202 }
3203}
3204
3205
3206/**********************************************************************
3207 * TrackPopupMenu (USER32.549)
3208 *
3209 * Like the win32 API, the function return the command ID only if the
3210 * flag TPM_RETURNCMD is on.
3211 *
3212 */
3213BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3214 INT nReserved, HWND hWnd, const RECT *lpRect )
3215{
3216 BOOL ret = FALSE;
3217
3218 if(lpRect) {
3219 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));
3220 }
3221 else dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x lpRect=NULL", hMenu, wFlags, x, y, nReserved, hWnd));
3222
3223 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3224
3225 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3226 if (!(wFlags & TPM_NONOTIFY))
3227 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3228
3229 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3230 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,FALSE, lpRect );
3231 MENU_ExitTracking(hWnd);
3232
3233 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3234 ret = 1;
3235
3236 return ret;
3237}
3238
3239/**********************************************************************
3240 * TrackPopupMenuEx (USER32.550)
3241 */
3242BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3243 HWND hWnd, LPTPMPARAMS lpTpm )
3244{
3245 dprintf(("USER32: TrackPopupMenuEx"));
3246 //FIXME("not fully implemented\n" );
3247 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3248 lpTpm ? &lpTpm->rcExclude : NULL );
3249}
3250
3251/***********************************************************************
3252 * PopupMenuWndProc
3253 *
3254 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3255 */
3256LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3257 LPARAM lParam )
3258{
3259 LRESULT retvalue;
3260
3261 //TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3262 //hwnd, message, wParam, lParam);
3263
3264 switch(message)
3265 {
3266 case WM_CREATE:
3267 {
3268 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3269 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3270 retvalue = 0;
3271 goto END;
3272 }
3273
3274 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3275 retvalue = MA_NOACTIVATE;
3276 goto END;
3277
3278 case WM_PAINT:
3279 {
3280 PAINTSTRUCT ps;
3281 BeginPaint( hwnd, &ps );
3282 MENU_DrawPopupMenu( hwnd, ps.hdc,
3283 (HMENU)GetWindowLongA( hwnd, 0 ) );
3284 EndPaint( hwnd, &ps );
3285 retvalue = 0;
3286 goto END;
3287 }
3288 case WM_ERASEBKGND:
3289 retvalue = 1;
3290 goto END;
3291
3292 case WM_DESTROY:
3293
3294 /* zero out global pointer in case resident popup window
3295 * was somehow destroyed. */
3296
3297 if(MENU_GetTopPopupWnd() )
3298 {
3299 if( hwnd == pTopPopupWnd )
3300 {
3301 //ERR("resident popup destroyed!\n");
3302
3303 MENU_DestroyTopPopupWnd();
3304 uSubPWndLevel = 0;
3305 }
3306 else
3307 uSubPWndLevel--;
3308 MENU_ReleaseTopPopupWnd();
3309 }
3310 break;
3311
3312 case WM_SHOWWINDOW:
3313
3314 if( wParam )
3315 {
3316 //if( !(*(HMENU*)wndPtr->wExtra) )
3317 // ERR("no menu to display\n");
3318 }
3319 else
3320 SetWindowLongA(hwnd,0,0);
3321 break;
3322
3323 case MM_SETMENUHANDLE:
3324
3325 SetWindowLongA(hwnd,0,wParam);
3326 break;
3327
3328 case MM_GETMENUHANDLE:
3329
3330 retvalue = GetWindowLongA(hwnd,0);
3331 goto END;
3332
3333 default:
3334 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3335 goto END;
3336 }
3337 retvalue = 0;
3338END:
3339 return retvalue;
3340}
3341
3342
3343/***********************************************************************
3344 * MENU_GetMenuBarHeight
3345 *
3346 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3347 */
3348UINT MENU_GetMenuBarHeight(HWND hwnd,UINT menubarWidth,INT orgX,INT orgY)
3349{
3350 HDC hdc;
3351 RECT rectBar;
3352 LPPOPUPMENU lppop;
3353 UINT retvalue;
3354
3355 //TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3356 // hwnd, menubarWidth, orgX, orgY );
3357
3358 if (!(lppop = MENU_GetMenu(getMenu(hwnd))))
3359 {
3360 return 0;
3361 }
3362
3363 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3364 SelectObject( hdc, hMenuFont);
3365 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3366 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3367 ReleaseDC( hwnd, hdc );
3368 retvalue = lppop->Height;
3369 return retvalue;
3370}
3371
3372
3373/*******************************************************************
3374 * ChangeMenu32A (USER32.23)
3375 */
3376BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3377 UINT id, UINT flags )
3378{
3379 dprintf(("USER32: ChangeMenuA"));
3380
3381 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3382 // hMenu, pos, (DWORD)data, id, flags );
3383 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3384 id, data );
3385 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3386 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3387 id, data );
3388 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3389 flags & MF_BYPOSITION ? pos : id,
3390 flags & ~MF_REMOVE );
3391 /* Default: MF_INSERT */
3392 return InsertMenuA( hMenu, pos, flags, id, data );
3393}
3394
3395
3396/*******************************************************************
3397 * ChangeMenu32W (USER32.24)
3398 */
3399BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3400 UINT id, UINT flags )
3401{
3402 dprintf(("USER32: ChangeMenuW"));
3403
3404 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3405 // hMenu, pos, (DWORD)data, id, flags );
3406 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3407 id, data );
3408 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3409 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3410 id, data );
3411 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3412 flags & MF_BYPOSITION ? pos : id,
3413 flags & ~MF_REMOVE );
3414 /* Default: MF_INSERT */
3415 return InsertMenuW( hMenu, pos, flags, id, data );
3416}
3417
3418
3419/*******************************************************************
3420 * CheckMenuItem (USER32.46)
3421 */
3422DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3423{
3424 MENUITEM *item;
3425 DWORD ret;
3426
3427 dprintf(("USER32: CheckMenuItem %x %x %x", hMenu, id, flags));
3428
3429 //TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3430 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3431 ret = item->fState & MF_CHECKED;
3432 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3433 else item->fState &= ~MF_CHECKED;
3434 return ret;
3435}
3436
3437
3438/**********************************************************************
3439 * EnableMenuItem32 (USER32.170)
3440 */
3441ULONG WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3442{
3443 UINT oldflags;
3444 MENUITEM *item;
3445 POPUPMENU *menu;
3446
3447 dprintf(("USER32: EnableMenuItem %x %x %x", hMenu, wItemID, wFlags));
3448
3449 //TRACE("(%04x, %04X, %04X) !\n",
3450 // hMenu, wItemID, wFlags);
3451
3452 /* Get the Popupmenu to access the owner menu */
3453 if (!(menu = MENU_GetMenu(hMenu)))
3454 return (UINT)-1;
3455
3456 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3457 return (UINT)-1;
3458
3459 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3460 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3461
3462 /* In win95 if the close item in the system menu change update the close button */
3463 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3464 {
3465 if (menu->hSysMenuOwner != 0)
3466 {
3467 POPUPMENU* parentMenu;
3468
3469 /* Get the parent menu to access*/
3470 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3471 return (UINT)-1;
3472
3473 /* Refresh the frame to reflect the change*/
3474 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3475 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3476 }
3477 }
3478
3479 return oldflags;
3480}
3481
3482
3483/*******************************************************************
3484 * GetMenuString32A (USER32.268)
3485 */
3486INT WINAPI GetMenuStringA(
3487 HMENU hMenu, /* [in] menuhandle */
3488 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3489 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3490 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3491 UINT wFlags /* [in] MF_ flags */
3492) {
3493 MENUITEM *item;
3494
3495 dprintf(("USER32: GetMenuStringA %x %d %d %x", hMenu, wItemID, nMaxSiz, wFlags));
3496
3497 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3498 // hMenu, wItemID, str, nMaxSiz, wFlags );
3499
3500 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3501
3502 if (!str || !nMaxSiz)
3503 {
3504 if (item && IS_STRING_ITEM(item->fType))
3505 return strlen(item->text);
3506 else
3507 return 0;
3508 }
3509
3510 str[0] = '\0';
3511
3512 if (item)
3513 {
3514 if (!IS_STRING_ITEM(item->fType)) return 0;
3515 lstrcpynA( str, item->text, nMaxSiz );
3516 }
3517
3518 return strlen(str);
3519}
3520
3521
3522/*******************************************************************
3523 * GetMenuString32W (USER32.269)
3524 */
3525INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3526 LPWSTR str, INT nMaxSiz, UINT wFlags )
3527{
3528 MENUITEM *item;
3529
3530 dprintf(("USER32: GetMenuStringW %x %d %x %d %d", hMenu, wItemID, str, nMaxSiz, wFlags));
3531
3532 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3533 // hMenu, wItemID, str, nMaxSiz, wFlags );
3534
3535 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3536
3537 if (!str || !nMaxSiz)
3538 {
3539 if (item && IS_STRING_ITEM(item->fType))
3540 return strlen(item->text);
3541 else
3542 return 0;
3543 }
3544
3545 str[0] = '\0';
3546
3547 if (item)
3548 {
3549 if (!IS_STRING_ITEM(item->fType)) return 0;
3550 lstrcpynAtoW( str, item->text, nMaxSiz );
3551 }
3552
3553 return lstrlenW(str);
3554}
3555
3556
3557/**********************************************************************
3558 * HiliteMenuItem32 (USER32.318)
3559 */
3560BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3561 UINT wHilite )
3562{
3563 LPPOPUPMENU menu;
3564
3565 dprintf(("USER32: HiliteMenuItem"));
3566
3567 //TRACE("(%04x, %04x, %04x, %04x);\n",
3568 // hWnd, hMenu, wItemID, wHilite);
3569 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3570 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3571 if (menu->FocusedItem == wItemID) return TRUE;
3572 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3573 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3574 return TRUE;
3575}
3576
3577
3578/**********************************************************************
3579 * GetMenuState (USER32.267)
3580 */
3581UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3582{
3583 MENUITEM *item;
3584
3585 dprintf(("USER32: GetMenuState %d %d",wItemID,wFlags));
3586
3587 //TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3588 // hMenu, wItemID, wFlags);
3589
3590 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3591
3592 //debug_print_menuitem (" item: ", item, "");
3593 if (item->fType & MF_POPUP)
3594 {
3595 POPUPMENU *menu = MENU_GetMenu(item->hSubMenu);
3596 if (!menu) return -1;
3597 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3598 }
3599 else
3600 {
3601 /* We used to (from way back then) mask the result to 0xff. */
3602 /* I don't know why and it seems wrong as the documented */
3603 /* return flag MF_SEPARATOR is outside that mask. */
3604 return (item->fType | item->fState);
3605 }
3606}
3607
3608
3609/**********************************************************************
3610 * GetMenuItemCount32 (USER32.262)
3611 */
3612INT WINAPI GetMenuItemCount( HMENU hMenu )
3613{
3614 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3615
3616 dprintf(("USER32: GetMenuItemCount %x", hMenu));
3617
3618 if (!IS_A_MENU(menu)) return -1;
3619 //TRACE("(%04x) returning %d\n",
3620 // hMenu, menu->nItems );
3621 return menu->nItems;
3622}
3623
3624/**********************************************************************
3625 * GetMenuItemID32 (USER32.263)
3626 */
3627UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3628{
3629 MENUITEM * lpmi;
3630
3631 dprintf(("USER32: GetMenuItemID %x %d", hMenu, nPos));
3632
3633 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0xFFFFFFFF;
3634 if (lpmi->fType & MF_POPUP) return 0xFFFFFFFF;
3635 return lpmi->wID;
3636
3637}
3638
3639/*******************************************************************
3640 * InsertMenu32A (USER32.322)
3641 */
3642BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3643 UINT id, LPCSTR str )
3644{
3645 MENUITEM *item;
3646
3647 if (IS_STRING_ITEM(flags) && str)
3648 dprintf(("USER32: InsertMenuA %x %d %x %d %s", hMenu, pos, flags, id, str));
3649 // TRACE("hMenu %04x, pos %d, flags %08x, "
3650 // "id %04x, str '%s'\n",
3651 // hMenu, pos, flags, id, str );
3652 else // TRACE("hMenu %04x, pos %d, flags %08x, "
3653 dprintf(("USER32: InsertMenuA %x %d %x %d %x", hMenu, pos, flags, id, str));
3654 // "id %04x, str %08lx (not a string)\n",
3655 // hMenu, pos, flags, id, (DWORD)str );
3656
3657 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3658
3659 if (!(MENU_SetItemData( item, flags, id, str )))
3660 {
3661 RemoveMenu( hMenu, pos, flags );
3662 return FALSE;
3663 }
3664
3665 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3666 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3667
3668 item->hCheckBit = item->hUnCheckBit = 0;
3669 return TRUE;
3670}
3671
3672
3673/*******************************************************************
3674 * InsertMenu32W (USER32.325)
3675 */
3676BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3677 UINT id, LPCWSTR str )
3678{
3679 BOOL ret;
3680
3681 if (IS_STRING_ITEM(flags) && str)
3682 {
3683 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3684 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3685 HeapFree( GetProcessHeap(), 0, newstr );
3686 return ret;
3687 }
3688 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3689}
3690
3691
3692/*******************************************************************
3693 * AppendMenu32A (USER32.5)
3694 */
3695BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3696 UINT id, LPCSTR data )
3697{
3698 dprintf(("USER32: AppendMenuA"));
3699
3700 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3701}
3702
3703
3704/*******************************************************************
3705 * AppendMenu32W (USER32.6)
3706 */
3707BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3708 UINT id, LPCWSTR data )
3709{
3710 dprintf(("USER32: AppendMenuW %x %x %d %x", hMenu, flags, id, data));
3711
3712 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3713}
3714
3715
3716/**********************************************************************
3717 * RemoveMenu (USER32.441)
3718 */
3719BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3720{
3721 LPPOPUPMENU menu;
3722 MENUITEM *item;
3723
3724 dprintf(("USER32: RemoveMenu %x %d %x", hMenu, nPos, wFlags));
3725
3726 //TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3727 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3728 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3729
3730 /* Remove item */
3731
3732 MENU_FreeItemData( item );
3733
3734 if (--menu->nItems == 0)
3735 {
3736 HeapFree(GetProcessHeap(), 0, menu->items );
3737 menu->items = NULL;
3738 }
3739 else
3740 {
3741 while(nPos < menu->nItems)
3742 {
3743 *item = *(item+1);
3744 item++;
3745 nPos++;
3746 }
3747 menu->items = (MENUITEM*)HeapReAlloc(GetProcessHeap(), 0, menu->items,
3748 menu->nItems * sizeof(MENUITEM) );
3749 }
3750 return TRUE;
3751}
3752
3753
3754/**********************************************************************
3755 * DeleteMenu32 (USER32.129)
3756 */
3757BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3758{
3759 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3760
3761 dprintf(("USER32: DeleteMenu %x %d %x", hMenu, nPos, wFlags));
3762
3763 if (!item) return FALSE;
3764 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3765 /* nPos is now the position of the item */
3766 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3767 return TRUE;
3768}
3769
3770
3771/*******************************************************************
3772 * ModifyMenu32A (USER32.397)
3773 */
3774BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3775 UINT id, LPCSTR str )
3776{
3777 MENUITEM *item;
3778
3779
3780 if (IS_STRING_ITEM(flags))
3781 {
3782 dprintf(("USER32: ModifyMenuA, %x %d %x %d %s", hMenu, pos, flags, id, str));
3783 //TRACE("%04x %d %04x %04x '%s'\n",
3784 // hMenu, pos, flags, id, str ? str : "#NULL#" );
3785 if (!str) return FALSE;
3786 }
3787 else
3788 {
3789 dprintf(("USER32: ModifyMenuA, %x %d %x %d %x", hMenu, pos, flags, id, str));
3790 //TRACE("%04x %d %04x %04x %08lx\n",
3791 // hMenu, pos, flags, id, (DWORD)str );
3792 }
3793
3794 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3795 return MENU_SetItemData( item, flags, id, str );
3796}
3797
3798
3799/*******************************************************************
3800 * ModifyMenu32W (USER32.398)
3801 */
3802BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3803 UINT id, LPCWSTR str )
3804{
3805 BOOL ret;
3806
3807 if (IS_STRING_ITEM(flags) && str)
3808 {
3809 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3810 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3811 HeapFree( GetProcessHeap(), 0, newstr );
3812 return ret;
3813 }
3814 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3815}
3816
3817
3818/**********************************************************************
3819 * CreatePopupMenu32 (USER32.82)
3820 */
3821HMENU WINAPI CreatePopupMenu(void)
3822{
3823 HMENU hmenu;
3824 POPUPMENU *menu;
3825
3826 dprintf(("USER32: CreatePopupMenu"));
3827
3828 if (!(hmenu = CreateMenu())) return 0;
3829 menu = MENU_GetMenu(hmenu);
3830 menu->wFlags |= MF_POPUP;
3831 menu->bTimeToHide = FALSE;
3832 return hmenu;
3833}
3834
3835
3836/**********************************************************************
3837 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3838 */
3839DWORD WINAPI GetMenuCheckMarkDimensions(void)
3840{
3841 dprintf(("USER32: GetMenuCheckMarkDimensions"));
3842
3843 return MAKELONG( check_bitmap_width, check_bitmap_height );
3844}
3845
3846
3847/**********************************************************************
3848 * SetMenuItemBitmaps32 (USER32.490)
3849 */
3850BOOL WINAPI SetMenuItemBitmaps(HMENU hMenu, UINT nPos, UINT wFlags,
3851 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3852{
3853 MENUITEM *item;
3854
3855 dprintf(("USER32: SetMenuItemBitmaps %x %d %x %x %x", hMenu, nPos, wFlags, hNewCheck, hNewUnCheck));
3856
3857 //TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3858 // hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3859 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3860
3861 if (!hNewCheck && !hNewUnCheck)
3862 {
3863 item->fState &= ~MF_USECHECKBITMAPS;
3864 }
3865 else /* Install new bitmaps */
3866 {
3867 item->hCheckBit = hNewCheck;
3868 item->hUnCheckBit = hNewUnCheck;
3869 item->fState |= MF_USECHECKBITMAPS;
3870 }
3871 return TRUE;
3872}
3873
3874
3875/**********************************************************************
3876 * CreateMenu (USER32.81)
3877 */
3878HMENU WINAPI CreateMenu(void)
3879{
3880 HMENU hMenu;
3881 LPPOPUPMENU menu;
3882
3883 dprintf(("USER32: CreateMenu"));
3884
3885 if (!(hMenu = (HMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3886 menu = (LPPOPUPMENU)hMenu;
3887
3888 ZeroMemory(menu, sizeof(POPUPMENU));
3889 menu->wMagic = MENU_MAGIC;
3890 menu->FocusedItem = NO_SELECTED_ITEM;
3891 menu->bTimeToHide = FALSE;
3892
3893 //TRACE("return %04x\n", hMenu );
3894
3895 return hMenu;
3896}
3897
3898
3899/**********************************************************************
3900 * DestroyMenu32 (USER32.134)
3901 */
3902BOOL WINAPI DestroyMenu( HMENU hMenu )
3903{
3904 //TRACE("(%04x)\n", hMenu);
3905
3906 dprintf(("USER32: DestroyMenu %x", hMenu));
3907
3908 /* Silently ignore attempts to destroy default system popup */
3909
3910 if (hMenu && hMenu != MENU_DefSysPopup)
3911 {
3912 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3913 HWND pTPWnd = MENU_GetTopPopupWnd();
3914
3915 if( pTPWnd && (hMenu == GetWindowLongA(pTPWnd,0)) )
3916 SetWindowLongA(pTPWnd,0,0);
3917
3918 if (IS_A_MENU(lppop))
3919 {
3920 lppop->wMagic = 0; /* Mark it as destroyed */
3921
3922 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3923 (!pTPWnd || (lppop->hWnd != pTPWnd)))
3924 DestroyWindow( lppop->hWnd );
3925
3926 if (lppop->items) /* recursively destroy submenus */
3927 {
3928 int i;
3929 MENUITEM *item = lppop->items;
3930 for (i = lppop->nItems; i > 0; i--, item++)
3931 {
3932 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3933 MENU_FreeItemData( item );
3934 }
3935 HeapFree(GetProcessHeap(), 0, lppop->items );
3936 }
3937 HeapFree(GetProcessHeap(),0,(LPVOID)hMenu);
3938 MENU_ReleaseTopPopupWnd();
3939 }
3940 else
3941 {
3942 MENU_ReleaseTopPopupWnd();
3943 return FALSE;
3944 }
3945 }
3946 return (hMenu != MENU_DefSysPopup);
3947}
3948
3949/**********************************************************************
3950 * GetSystemMenu32 (USER32.291)
3951 */
3952HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3953{
3954 HMENU retvalue = 0;
3955 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hWnd);
3956
3957 dprintf(("USER32: GetSystemMenu"));
3958
3959 if (win32wnd)
3960 {
3961 HMENU hSysMenu = getSysMenu(hWnd);
3962 if(hSysMenu)
3963 {
3964 if( bRevert )
3965 {
3966 DestroyMenu(hSysMenu);
3967 hSysMenu = 0;
3968 setSysMenu(hWnd,hSysMenu);
3969 }
3970 else
3971 {
3972 POPUPMENU *menu = MENU_GetMenu(hSysMenu);
3973 if(menu)
3974 {
3975 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
3976 menu->items[0].hSubMenu = MENU_CopySysPopup();
3977 }
3978 else
3979 {
3980 //WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
3981 // wndPtr->hSysMenu, hWnd);
3982 hSysMenu = 0;
3983 setSysMenu(hWnd,hSysMenu);
3984 }
3985 }
3986 }
3987
3988 if(!hSysMenu && (GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) )
3989 {
3990 hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
3991 setSysMenu(hWnd,hSysMenu);
3992 }
3993
3994 if( hSysMenu )
3995 {
3996 POPUPMENU *menu;
3997 retvalue = GetSubMenu(hSysMenu, 0);
3998
3999 /* Store the dummy sysmenu handle to facilitate the refresh */
4000 /* of the close button if the SC_CLOSE item change */
4001 menu = MENU_GetMenu(retvalue);
4002 if (menu)
4003 menu->hSysMenuOwner = hSysMenu;
4004 }
4005 }
4006 return retvalue;
4007}
4008
4009
4010/*******************************************************************
4011 * SetSystemMenu32 (USER32.508)
4012 */
4013BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4014{
4015 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
4016
4017 dprintf(("USER32: SetSystemMenu"));
4018
4019 if (win32wnd)
4020 {
4021 if (win32wnd->GetSysMenu()) DestroyMenu(win32wnd->GetSysMenu());
4022 win32wnd->SetSysMenu(MENU_GetSysMenu( hwnd, hMenu ));
4023 return TRUE;
4024 }
4025 return FALSE;
4026}
4027
4028/**********************************************************************
4029 * GetMenu32 (USER32.257)
4030 */
4031HMENU WINAPI GetMenu( HWND hWnd )
4032{
4033 HMENU retvalue;
4034
4035 dprintf(("USER32: GetMenu %x", hWnd));
4036
4037 if (GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) return 0;
4038 else return getMenu(hWnd);
4039}
4040
4041/**********************************************************************
4042 * SetMenu32 (USER32.487)
4043 */
4044BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4045{
4046 //TRACE("(%04x, %04x);\n", hWnd, hMenu);
4047
4048 dprintf(("USER32: SetMenu %x %x", hWnd, hMenu));
4049
4050 if (hMenu && !IsMenu(hMenu))
4051 {
4052 //WARN("hMenu is not a menu handle\n");
4053 return FALSE;
4054 }
4055
4056
4057 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD))
4058 {
4059 if (GetCapture() == hWnd) ReleaseCapture();
4060
4061 setMenu(hWnd,hMenu);
4062 if (hMenu != 0)
4063 {
4064 LPPOPUPMENU lpmenu;
4065
4066 if (!(lpmenu = MENU_GetMenu(hMenu)))
4067 {
4068 return FALSE;
4069 }
4070 lpmenu->hWnd = hWnd;
4071 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
4072 lpmenu->Height = 0; /* Make sure we recalculate the size */
4073 }
4074//SvL: This fixes the menu in standard mine sweeper (window isn't visible
4075// when SetMenu is called)
4076// if (IsWindowVisible(hWnd))
4077 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4078 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4079 return TRUE;
4080 }
4081 return FALSE;
4082}
4083
4084
4085/**********************************************************************
4086 * GetSubMenu32 (USER32.288)
4087 */
4088HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4089{
4090 MENUITEM * lpmi;
4091
4092 dprintf(("USER32: GetSubMenu %x %d", nPos));
4093
4094 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4095 if (!(lpmi->fType & MF_POPUP)) return 0;
4096 return lpmi->hSubMenu;
4097}
4098
4099
4100/**********************************************************************
4101 * DrawMenuBar (USER32.161)
4102 */
4103BOOL WINAPI DrawMenuBar( HWND hWnd )
4104{
4105 LPPOPUPMENU lppop;
4106
4107 dprintf(("USER32: DrawMenuBar %x", hWnd));
4108
4109 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) && getMenu(hWnd))
4110 {
4111 lppop = MENU_GetMenu(getMenu(hWnd));
4112 if (lppop == NULL)
4113 {
4114 return FALSE;
4115 }
4116
4117 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4118 lppop->hwndOwner = hWnd;
4119 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4120 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4121 return TRUE;
4122 }
4123 return FALSE;
4124}
4125
4126
4127/***********************************************************************
4128 * EndMenu (USER.187) (USER32.175)
4129 */
4130void WINAPI EndMenu(void)
4131{
4132 dprintf(("USER32: EndMenu not implemented!"));
4133 /*
4134 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
4135 */
4136
4137 fEndMenu = TRUE;
4138}
4139
4140
4141/*****************************************************************
4142 * LoadMenu32A (USER32.370)
4143 */
4144HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4145{
4146 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4147
4148 dprintf(("USER32: LoadMenuA %x %x", instance, name));
4149
4150 if (!hrsrc) return 0;
4151 return LoadMenuIndirectA( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4152}
4153
4154
4155/*****************************************************************
4156 * LoadMenu32W (USER32.373)
4157 */
4158HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4159{
4160 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4161
4162 dprintf(("USER32: LoadMenuW %x %x", instance, name));
4163
4164 if (!hrsrc) return 0;
4165 return LoadMenuIndirectW( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4166}
4167
4168
4169/**********************************************************************
4170 * LoadMenuIndirect32A (USER32.371)
4171 */
4172HMENU WINAPI LoadMenuIndirectA(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate)
4173{
4174 HMENU hMenu;
4175 WORD version, offset;
4176 LPCSTR p = (LPCSTR)lpMenuTemplate;
4177
4178 dprintf(("USER32: LoadMenuIndirectA"));
4179
4180 //TRACE("%p\n", template );
4181 version = GET_WORD(p);
4182 p += sizeof(WORD);
4183 switch (version)
4184 {
4185 case 0:
4186 offset = GET_WORD(p);
4187 p += sizeof(WORD) + offset;
4188 if (!(hMenu = CreateMenu())) return 0;
4189 if (!MENU_ParseResource( p, hMenu, TRUE ))
4190 {
4191 DestroyMenu( hMenu );
4192 return 0;
4193 }
4194 return hMenu;
4195 case 1:
4196 offset = GET_WORD(p);
4197 p += sizeof(WORD) + offset;
4198 if (!(hMenu = CreateMenu())) return 0;
4199 if (!MENUEX_ParseResource( p, hMenu))
4200 {
4201 DestroyMenu( hMenu );
4202 return 0;
4203 }
4204 return hMenu;
4205 default:
4206 //ERR("version %d not supported.\n", version);
4207 return 0;
4208 }
4209}
4210
4211
4212/**********************************************************************
4213 * LoadMenuIndirect32W (USER32.372)
4214 */
4215HMENU WINAPI LoadMenuIndirectW(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate )
4216{
4217 dprintf(("USER32: LoadMenuIndirectW"));
4218
4219 /* FIXME: is there anything different between A and W? */
4220 return LoadMenuIndirectA(lpMenuTemplate);
4221}
4222
4223
4224/**********************************************************************
4225 * IsMenu32 (USER32.346)
4226 */
4227BOOL WINAPI IsMenu(HMENU hmenu)
4228{
4229 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4230
4231 dprintf(("USER32: IsMenu"));
4232
4233 return IS_A_MENU(menu);
4234}
4235
4236/**********************************************************************
4237 * GetMenuItemInfo32_common
4238 */
4239
4240static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4241 LPMENUITEMINFOA lpmii, BOOL unicode)
4242{
4243 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4244
4245 //debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
4246
4247 if (!menu)
4248 return FALSE;
4249
4250 if (lpmii->fMask & MIIM_TYPE) {
4251 lpmii->fType = menu->fType;
4252 switch (MENU_ITEM_TYPE(menu->fType)) {
4253 case MF_STRING:
4254 if (menu->text && lpmii->dwTypeData && lpmii->cch) {
4255 if (unicode) {
4256 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4257 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4258 } else {
4259 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4260 lpmii->cch = lstrlenA(menu->text);
4261 }
4262 }
4263 break;
4264 case MF_OWNERDRAW:
4265 case MF_BITMAP:
4266 lpmii->dwTypeData = menu->text;
4267 /* fall through */
4268 default:
4269 lpmii->cch = 0;
4270 }
4271 }
4272
4273 if (lpmii->fMask & MIIM_STRING) {
4274 if (unicode) {
4275 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4276 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4277 } else {
4278 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4279 lpmii->cch = lstrlenA(menu->text);
4280 }
4281 }
4282
4283 if (lpmii->fMask & MIIM_FTYPE)
4284 lpmii->fType = menu->fType;
4285
4286 if (lpmii->fMask & MIIM_BITMAP)
4287 lpmii->hbmpItem = menu->hbmpItem;
4288
4289 if (lpmii->fMask & MIIM_STATE)
4290 lpmii->fState = menu->fState;
4291
4292 if (lpmii->fMask & MIIM_ID)
4293 lpmii->wID = menu->wID;
4294
4295 if (lpmii->fMask & MIIM_SUBMENU)
4296 lpmii->hSubMenu = menu->hSubMenu;
4297
4298 if (lpmii->fMask & MIIM_CHECKMARKS) {
4299 lpmii->hbmpChecked = menu->hCheckBit;
4300 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4301 }
4302 if (lpmii->fMask & MIIM_DATA)
4303 lpmii->dwItemData = menu->dwItemData;
4304
4305 return TRUE;
4306}
4307
4308/**********************************************************************
4309 * GetMenuItemInfoA (USER32.264)
4310 */
4311BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4312 LPMENUITEMINFOA lpmii)
4313{
4314 dprintf(("USER32: GetMenuItemInfoA"));
4315
4316 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4317}
4318
4319/**********************************************************************
4320 * GetMenuItemInfoW (USER32.265)
4321 */
4322BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4323 LPMENUITEMINFOW lpmii)
4324{
4325 dprintf(("USER32: GetMenuItemInfoW"));
4326
4327 return GetMenuItemInfo_common (hmenu, item, bypos,
4328 (LPMENUITEMINFOA)lpmii, TRUE);
4329}
4330
4331/**********************************************************************
4332 * SetMenuItemInfo32_common
4333 */
4334
4335static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4336 const MENUITEMINFOA *lpmii,
4337 BOOL unicode)
4338{
4339 if (!menu) return FALSE;
4340
4341 if (lpmii->fMask & MIIM_TYPE ) {
4342 /* Get rid of old string. */
4343 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4344 HeapFree(GetProcessHeap(), 0, menu->text);
4345 menu->text = NULL;
4346 }
4347
4348 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4349 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4350 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4351
4352 menu->text = lpmii->dwTypeData;
4353
4354 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4355 if (unicode)
4356 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4357 else
4358 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4359 }
4360 }
4361
4362 if (lpmii->fMask & MIIM_FTYPE ) {
4363 /* free the string when the type is changing */
4364 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4365 HeapFree(GetProcessHeap(), 0, menu->text);
4366 menu->text = NULL;
4367 }
4368 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4369 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4370 }
4371
4372 if (lpmii->fMask & MIIM_STRING ) {
4373 /* free the string when used */
4374 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4375 HeapFree(GetProcessHeap(), 0, menu->text);
4376 if (unicode)
4377 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4378 else
4379 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4380 }
4381 }
4382
4383 if (lpmii->fMask & MIIM_STATE)
4384 {
4385 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4386 menu->fState = lpmii->fState;
4387 }
4388
4389 if (lpmii->fMask & MIIM_ID)
4390 menu->wID = lpmii->wID;
4391
4392 if (lpmii->fMask & MIIM_SUBMENU) {
4393 menu->hSubMenu = lpmii->hSubMenu;
4394 if (menu->hSubMenu) {
4395 POPUPMENU *subMenu = MENU_GetMenu((UINT)menu->hSubMenu);
4396 if (IS_A_MENU(subMenu)) {
4397 subMenu->wFlags |= MF_POPUP;
4398 menu->fType |= MF_POPUP;
4399 }
4400 else
4401 /* FIXME: Return an error ? */
4402 menu->fType &= ~MF_POPUP;
4403 }
4404 else
4405 menu->fType &= ~MF_POPUP;
4406 }
4407
4408 if (lpmii->fMask & MIIM_CHECKMARKS)
4409 {
4410 menu->hCheckBit = lpmii->hbmpChecked;
4411 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4412 }
4413 if (lpmii->fMask & MIIM_DATA)
4414 menu->dwItemData = lpmii->dwItemData;
4415
4416 //debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4417 return TRUE;
4418}
4419
4420/**********************************************************************
4421 * SetMenuItemInfo32A (USER32.491)
4422 */
4423BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4424 const MENUITEMINFOA *lpmii)
4425{
4426 dprintf(("USER32: SetMenuItemInfoA %x %d %d %x", hmenu, item, bypos, lpmii));
4427
4428 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4429 lpmii, FALSE);
4430}
4431
4432/**********************************************************************
4433 * SetMenuItemInfo32W (USER32.492)
4434 */
4435BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4436 const MENUITEMINFOW *lpmii)
4437{
4438 dprintf(("USER32: SetMenuItemInfoW"));
4439
4440 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4441 (const MENUITEMINFOA*)lpmii, TRUE);
4442}
4443
4444/**********************************************************************
4445 * SetMenuDefaultItem (USER32.489)
4446 *
4447 */
4448BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4449{
4450 UINT i;
4451 POPUPMENU *menu;
4452 MENUITEM *item;
4453
4454
4455 dprintf(("USER32: SetMenuDefaultItem"));
4456 //TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4457
4458 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4459
4460 /* reset all default-item flags */
4461 item = menu->items;
4462 for (i = 0; i < menu->nItems; i++, item++)
4463 {
4464 item->fState &= ~MFS_DEFAULT;
4465 }
4466
4467 /* no default item */
4468 if ( -1 == uItem)
4469 {
4470 return TRUE;
4471 }
4472
4473 item = menu->items;
4474 if ( bypos )
4475 {
4476 if ( uItem >= menu->nItems ) return FALSE;
4477 item[uItem].fState |= MFS_DEFAULT;
4478 return TRUE;
4479 }
4480 else
4481 {
4482 for (i = 0; i < menu->nItems; i++, item++)
4483 {
4484 if (item->wID == uItem)
4485 {
4486 item->fState |= MFS_DEFAULT;
4487 return TRUE;
4488 }
4489 }
4490
4491 }
4492 return FALSE;
4493}
4494
4495/**********************************************************************
4496 * GetMenuDefaultItem (USER32.260)
4497 */
4498UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4499{
4500 POPUPMENU *menu;
4501 MENUITEM * item;
4502 UINT i = 0;
4503
4504 dprintf(("USER32: GetMenuDefaultItem"));
4505 //TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4506
4507 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4508
4509 /* find default item */
4510 item = menu->items;
4511
4512 /* empty menu */
4513 if (! item) return -1;
4514
4515 while ( !( item->fState & MFS_DEFAULT ) )
4516 {
4517 i++; item++;
4518 if (i >= menu->nItems ) return -1;
4519 }
4520
4521 /* default: don't return disabled items */
4522 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4523
4524 /* search rekursiv when needed */
4525 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4526 {
4527 UINT ret;
4528 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4529 if ( -1 != ret ) return ret;
4530
4531 /* when item not found in submenu, return the popup item */
4532 }
4533 return ( bypos ) ? i : item->wID;
4534
4535}
4536
4537/**********************************************************************
4538 * InsertMenuItem32A (USER32.323)
4539 */
4540BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4541 const MENUITEMINFOA *lpmii)
4542{
4543 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4544
4545 dprintf(("USER32: InsertMenuItemA %x %d %d %x", hMenu, uItem, bypos, lpmii->wID));
4546
4547 return SetMenuItemInfo_common(item, lpmii, FALSE);
4548}
4549
4550
4551/**********************************************************************
4552 * InsertMenuItem32W (USER32.324)
4553 */
4554BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4555 const MENUITEMINFOW *lpmii)
4556{
4557 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4558
4559 dprintf(("USER32: InsertMenuItemW"));
4560
4561 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4562}
4563
4564/**********************************************************************
4565 * CheckMenuRadioItem32 (USER32.47)
4566 */
4567
4568BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4569 UINT first, UINT last, UINT check,
4570 UINT bypos)
4571{
4572 MENUITEM *mifirst, *milast, *micheck;
4573 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4574
4575 dprintf(("USER32: CheckMenuRadioItem"));
4576 //TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4577 // hMenu, first, last, check, bypos);
4578
4579 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4580 milast = MENU_FindItem (&mlast, &last, bypos);
4581 micheck = MENU_FindItem (&mcheck, &check, bypos);
4582
4583 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4584 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4585 micheck > milast || micheck < mifirst)
4586 return FALSE;
4587
4588 while (mifirst <= milast)
4589 {
4590 if (mifirst == micheck)
4591 {
4592 mifirst->fType |= MFT_RADIOCHECK;
4593 mifirst->fState |= MFS_CHECKED;
4594 } else {
4595 mifirst->fType &= ~MFT_RADIOCHECK;
4596 mifirst->fState &= ~MFS_CHECKED;
4597 }
4598 mifirst++;
4599 }
4600
4601 return TRUE;
4602}
4603
4604/**********************************************************************
4605 * GetMenuItemRect32 (USER32.266)
4606 *
4607 * ATTENTION: Here, the returned values in rect are the screen
4608 * coordinates of the item just like if the menu was
4609 * always on the upper left side of the application.
4610 *
4611 */
4612BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4613 LPRECT rect)
4614{
4615 POPUPMENU *itemMenu;
4616 MENUITEM *item;
4617 HWND referenceHwnd;
4618
4619 //TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4620
4621 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4622 referenceHwnd = hwnd;
4623
4624 if(!hwnd)
4625 {
4626 itemMenu = MENU_GetMenu(hMenu);
4627 if (itemMenu == NULL) {
4628 SetLastError(ERROR_INVALID_PARAMETER);
4629 return FALSE;
4630 }
4631
4632 if(itemMenu->hWnd == 0)
4633 return FALSE;
4634 referenceHwnd = itemMenu->hWnd;
4635 }
4636
4637 if ((rect == NULL) || (item == NULL)) {
4638 SetLastError(ERROR_INVALID_PARAMETER);
4639 return FALSE;
4640 }
4641 *rect = item->rect;
4642
4643 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4644 dprintf(("USER32: GetMenuItemRect %x %x %d (%d,%d)(%d,%d)", hwnd, hMenu, uItem, rect->left, rect->top, rect->right, rect->bottom));
4645
4646 return TRUE;
4647}
4648
4649/**********************************************************************
4650 * SetMenuInfo
4651 *
4652 * FIXME
4653 * MIM_APPLYTOSUBMENUS
4654 * actually use the items to draw the menu
4655 */
4656BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4657{
4658 POPUPMENU *menu;
4659
4660 dprintf(("USER32: SetMenuInfo"));
4661 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4662
4663 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu=MENU_GetMenu(hMenu)))
4664 {
4665
4666 if (lpmi->fMask & MIM_BACKGROUND)
4667 menu->hbrBack = lpmi->hbrBack;
4668
4669 if (lpmi->fMask & MIM_HELPID)
4670 menu->dwContextHelpID = lpmi->dwContextHelpID;
4671
4672 if (lpmi->fMask & MIM_MAXHEIGHT)
4673 menu->cyMax = lpmi->cyMax;
4674
4675 if (lpmi->fMask & MIM_MENUDATA)
4676 menu->dwMenuData = lpmi->dwMenuData;
4677
4678 if (lpmi->fMask & MIM_STYLE)
4679 menu->dwStyle = lpmi->dwStyle;
4680
4681 return TRUE;
4682 }
4683 return FALSE;
4684}
4685
4686/**********************************************************************
4687 * GetMenuInfo
4688 *
4689 * NOTES
4690 * win98/NT5.0
4691 *
4692 */
4693BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4694{ POPUPMENU *menu;
4695
4696 dprintf(("USER32: GetMenuInfo"));
4697 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4698
4699 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4700 {
4701
4702 if (lpmi->fMask & MIM_BACKGROUND)
4703 lpmi->hbrBack = menu->hbrBack;
4704
4705 if (lpmi->fMask & MIM_HELPID)
4706 lpmi->dwContextHelpID = menu->dwContextHelpID;
4707
4708 if (lpmi->fMask & MIM_MAXHEIGHT)
4709 lpmi->cyMax = menu->cyMax;
4710
4711 if (lpmi->fMask & MIM_MENUDATA)
4712 lpmi->dwMenuData = menu->dwMenuData;
4713
4714 if (lpmi->fMask & MIM_STYLE)
4715 lpmi->dwStyle = menu->dwStyle;
4716
4717 return TRUE;
4718 }
4719 return FALSE;
4720}
4721
4722/**********************************************************************
4723 * SetMenuContextHelpId (USER32.488)
4724 */
4725BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4726{
4727 LPPOPUPMENU menu;
4728
4729 dprintf(("USER32: SetMenuContextHelpId"));
4730 //TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4731
4732 menu = MENU_GetMenu(hMenu);
4733 if (menu)
4734 {
4735 menu->dwContextHelpID = dwContextHelpID;
4736 return TRUE;
4737 }
4738 return FALSE;
4739}
4740
4741/**********************************************************************
4742 * GetMenuContextHelpId (USER32.488)
4743 */
4744DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4745{
4746 LPPOPUPMENU menu;
4747
4748 dprintf(("USER32: GetMenuContextHelpId"));
4749
4750 menu = MENU_GetMenu(hMenu);
4751 if (menu)
4752 {
4753 return menu->dwContextHelpID;
4754 }
4755 return 0;
4756}
4757
4758/**********************************************************************
4759 * MenuItemFromPoint (USER32.387)
4760 */
4761UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4762{
4763 dprintf(("USER32: MenuItemFromPoint not implemented!"));
4764
4765 return 0;
4766}
4767
4768/**********************************************************************
4769 * IsMenuActive (Internal)
4770 */
4771BOOL IsMenuActive(void)
4772{
4773 return pTopPopupWnd && (GetWindowLongA(pTopPopupWnd,GWL_STYLE) & WS_VISIBLE);
4774}
4775
4776BOOL POPUPMENU_Register()
4777{
4778 WNDCLASSA wndClass;
4779 BOOL rc;
4780
4781//SvL: Don't check this now
4782// if (GlobalFindAtomA(POPUPMENUCLASSNAME)) return FALSE;
4783
4784 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
4785 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
4786 wndClass.lpfnWndProc = (WNDPROC)PopupMenuWndProc;
4787 wndClass.cbClsExtra = 0;
4788 wndClass.cbWndExtra = sizeof(HMENU);
4789 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
4790 wndClass.hbrBackground = NULL_BRUSH;
4791 wndClass.lpszClassName = POPUPMENUCLASSNAME;
4792
4793 rc = RegisterClassA(&wndClass);
4794 MENU_Init();
4795
4796 return rc;
4797}
4798//******************************************************************************
4799//******************************************************************************
4800BOOL POPUPMENU_Unregister()
4801{
4802 if (GlobalFindAtomA(POPUPMENUCLASSNAME))
4803 return UnregisterClassA(POPUPMENUCLASSNAME,(HINSTANCE)NULL);
4804 else return FALSE;
4805}
4806//******************************************************************************
4807//******************************************************************************
4808
Note: See TracBrowser for help on using the repository browser.