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

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

lots of changes/fixes

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