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

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

BS: Fix for MENU_InsertItem (add if menu item not found)

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