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

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

update; put back removed parts

File size: 145.2 KB
Line 
1/* $Id: menu.cpp,v 1.23 2000-05-22 19:03:45 sandervl Exp $*/
2/*
3 * Menu functions
4 *
5 * Copyright 1993 Martin Ayotte
6 * Copyright 1994 Alexandre Julliard
7 * Copyright 1997 Morten Welinder
8 *
9 * Copyright 1999 Christoph Bratschi
10 *
11 * Corel version: 200005313
12 * (WINE version: 20000130)
13 *
14 * Status: ???
15 * Version: ???
16 */
17
18/*
19 * Note: the style MF_MOUSESELECT is used to mark popup items that
20 * have been selected, i.e. their popup menu is currently displayed.
21 * This is probably not the meaning this style has in MS-Windows.
22 */
23
24#include <assert.h>
25#include <ctype.h>
26#include <stdlib.h>
27#include <string.h>
28#include <os2win.h>
29#include <heapstring.h>
30#include "controls.h"
31#include "menu.h"
32
33#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 if(lpRect) {
3207 dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x (%d,%d)(%d,%d)", hMenu, wFlags, x, y, nReserved, hWnd, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom));
3208 }
3209 else dprintf(("USER32: TrackPopupMenu %x %x (%d,%d) %x %x lpRect=NULL", hMenu, wFlags, x, y, nReserved, hWnd));
3210
3211 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3212
3213 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3214 if (!(wFlags & TPM_NONOTIFY))
3215 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3216
3217 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3218 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,FALSE, lpRect );
3219 MENU_ExitTracking(hWnd);
3220
3221 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3222 ret = 1;
3223
3224 return ret;
3225}
3226
3227/**********************************************************************
3228 * TrackPopupMenuEx (USER32.550)
3229 */
3230BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3231 HWND hWnd, LPTPMPARAMS lpTpm )
3232{
3233 dprintf(("USER32: TrackPopupMenuEx"));
3234 //FIXME("not fully implemented\n" );
3235 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3236 lpTpm ? &lpTpm->rcExclude : NULL );
3237}
3238
3239/***********************************************************************
3240 * PopupMenuWndProc
3241 *
3242 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3243 */
3244LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3245 LPARAM lParam )
3246{
3247 LRESULT retvalue;
3248
3249 //TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3250 //hwnd, message, wParam, lParam);
3251
3252 switch(message)
3253 {
3254 case WM_CREATE:
3255 {
3256 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3257 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3258 retvalue = 0;
3259 goto END;
3260 }
3261
3262 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3263 retvalue = MA_NOACTIVATE;
3264 goto END;
3265
3266 case WM_PAINT:
3267 {
3268 PAINTSTRUCT ps;
3269 BeginPaint( hwnd, &ps );
3270 MENU_DrawPopupMenu( hwnd, ps.hdc,
3271 (HMENU)GetWindowLongA( hwnd, 0 ) );
3272 EndPaint( hwnd, &ps );
3273 retvalue = 0;
3274 goto END;
3275 }
3276 case WM_ERASEBKGND:
3277 retvalue = 1;
3278 goto END;
3279
3280 case WM_DESTROY:
3281
3282 /* zero out global pointer in case resident popup window
3283 * was somehow destroyed. */
3284
3285 if(MENU_GetTopPopupWnd() )
3286 {
3287 if( hwnd == pTopPopupWnd )
3288 {
3289 //ERR("resident popup destroyed!\n");
3290
3291 MENU_DestroyTopPopupWnd();
3292 uSubPWndLevel = 0;
3293 }
3294 else
3295 uSubPWndLevel--;
3296 MENU_ReleaseTopPopupWnd();
3297 }
3298 break;
3299
3300 case WM_SHOWWINDOW:
3301
3302 if( wParam )
3303 {
3304 //if( !(*(HMENU*)wndPtr->wExtra) )
3305 // ERR("no menu to display\n");
3306 }
3307 else
3308 SetWindowLongA(hwnd,0,0);
3309 break;
3310
3311 case MM_SETMENUHANDLE:
3312
3313 SetWindowLongA(hwnd,0,wParam);
3314 break;
3315
3316 case MM_GETMENUHANDLE:
3317
3318 retvalue = GetWindowLongA(hwnd,0);
3319 goto END;
3320
3321 default:
3322 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3323 goto END;
3324 }
3325 retvalue = 0;
3326END:
3327 return retvalue;
3328}
3329
3330
3331/***********************************************************************
3332 * MENU_GetMenuBarHeight
3333 *
3334 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3335 */
3336UINT MENU_GetMenuBarHeight(HWND hwnd,UINT menubarWidth,INT orgX,INT orgY)
3337{
3338 HDC hdc;
3339 RECT rectBar;
3340 LPPOPUPMENU lppop;
3341 UINT retvalue;
3342
3343 //TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3344 // hwnd, menubarWidth, orgX, orgY );
3345
3346 if (!(lppop = MENU_GetMenu(getMenu(hwnd))))
3347 {
3348 return 0;
3349 }
3350
3351 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3352 SelectObject( hdc, hMenuFont);
3353 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3354 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3355 ReleaseDC( hwnd, hdc );
3356 retvalue = lppop->Height;
3357 return retvalue;
3358}
3359
3360
3361/*******************************************************************
3362 * ChangeMenu32A (USER32.23)
3363 */
3364BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3365 UINT id, UINT flags )
3366{
3367 dprintf(("USER32: ChangeMenuA"));
3368
3369 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3370 // hMenu, pos, (DWORD)data, id, flags );
3371 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3372 id, data );
3373 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3374 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3375 id, data );
3376 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3377 flags & MF_BYPOSITION ? pos : id,
3378 flags & ~MF_REMOVE );
3379 /* Default: MF_INSERT */
3380 return InsertMenuA( hMenu, pos, flags, id, data );
3381}
3382
3383
3384/*******************************************************************
3385 * ChangeMenu32W (USER32.24)
3386 */
3387BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3388 UINT id, UINT flags )
3389{
3390 dprintf(("USER32: ChangeMenuW"));
3391
3392 //TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3393 // hMenu, pos, (DWORD)data, id, flags );
3394 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3395 id, data );
3396 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3397 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3398 id, data );
3399 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3400 flags & MF_BYPOSITION ? pos : id,
3401 flags & ~MF_REMOVE );
3402 /* Default: MF_INSERT */
3403 return InsertMenuW( hMenu, pos, flags, id, data );
3404}
3405
3406
3407/*******************************************************************
3408 * CheckMenuItem (USER32.46)
3409 */
3410DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3411{
3412 MENUITEM *item;
3413 DWORD ret;
3414
3415 dprintf(("USER32: CheckMenuItem %x %x %x", hMenu, id, flags));
3416
3417 //TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3418 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3419 ret = item->fState & MF_CHECKED;
3420 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3421 else item->fState &= ~MF_CHECKED;
3422 return ret;
3423}
3424
3425
3426/**********************************************************************
3427 * EnableMenuItem32 (USER32.170)
3428 */
3429ULONG WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3430{
3431 UINT oldflags;
3432 MENUITEM *item;
3433 POPUPMENU *menu;
3434
3435 dprintf(("USER32: EnableMenuItem %x %x %x", hMenu, wItemID, wFlags));
3436
3437 //TRACE("(%04x, %04X, %04X) !\n",
3438 // hMenu, wItemID, wFlags);
3439
3440 /* Get the Popupmenu to access the owner menu */
3441 if (!(menu = MENU_GetMenu(hMenu)))
3442 return (UINT)-1;
3443
3444 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3445 return (UINT)-1;
3446
3447 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3448 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3449
3450 /* In win95 if the close item in the system menu change update the close button */
3451 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3452 {
3453 if (menu->hSysMenuOwner != 0)
3454 {
3455 POPUPMENU* parentMenu;
3456
3457 /* Get the parent menu to access*/
3458 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3459 return (UINT)-1;
3460
3461 /* Refresh the frame to reflect the change*/
3462 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3463 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3464 }
3465 }
3466
3467 return oldflags;
3468}
3469
3470
3471/*******************************************************************
3472 * GetMenuString32A (USER32.268)
3473 */
3474INT WINAPI GetMenuStringA(
3475 HMENU hMenu, /* [in] menuhandle */
3476 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3477 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3478 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3479 UINT wFlags /* [in] MF_ flags */
3480) {
3481 MENUITEM *item;
3482
3483 dprintf(("USER32: GetMenuStringA %x %d %d %x", hMenu, wItemID, nMaxSiz, wFlags));
3484
3485 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3486 // hMenu, wItemID, str, nMaxSiz, wFlags );
3487
3488 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3489
3490 if (!str || !nMaxSiz)
3491 {
3492 if (item && IS_STRING_ITEM(item->fType))
3493 return strlen(item->text);
3494 else
3495 return 0;
3496 }
3497
3498 str[0] = '\0';
3499
3500 if (item)
3501 {
3502 if (!IS_STRING_ITEM(item->fType)) return 0;
3503 lstrcpynA( str, item->text, nMaxSiz );
3504 }
3505
3506 return strlen(str);
3507}
3508
3509
3510/*******************************************************************
3511 * GetMenuString32W (USER32.269)
3512 */
3513INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3514 LPWSTR str, INT nMaxSiz, UINT wFlags )
3515{
3516 MENUITEM *item;
3517
3518 dprintf(("USER32: GetMenuStringW"));
3519
3520 //TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3521 // hMenu, wItemID, str, nMaxSiz, wFlags );
3522
3523 item = MENU_FindItem( &hMenu, &wItemID, wFlags );
3524
3525 if (!str || !nMaxSiz)
3526 {
3527 if (item && IS_STRING_ITEM(item->fType))
3528 return strlen(item->text);
3529 else
3530 return 0;
3531 }
3532
3533 str[0] = '\0';
3534
3535 if (item)
3536 {
3537 if (!IS_STRING_ITEM(item->fType)) return 0;
3538 lstrcpynAtoW( str, item->text, nMaxSiz );
3539 }
3540
3541 return lstrlenW(str);
3542}
3543
3544
3545/**********************************************************************
3546 * HiliteMenuItem32 (USER32.318)
3547 */
3548BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3549 UINT wHilite )
3550{
3551 LPPOPUPMENU menu;
3552
3553 dprintf(("USER32: HiliteMenuItem"));
3554
3555 //TRACE("(%04x, %04x, %04x, %04x);\n",
3556 // hWnd, hMenu, wItemID, wHilite);
3557 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3558 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3559 if (menu->FocusedItem == wItemID) return TRUE;
3560 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3561 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3562 return TRUE;
3563}
3564
3565
3566/**********************************************************************
3567 * GetMenuState (USER32.267)
3568 */
3569UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3570{
3571 MENUITEM *item;
3572
3573 dprintf(("USER32: GetMenuState %d %d",wItemID,wFlags));
3574
3575 //TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3576 // hMenu, wItemID, wFlags);
3577
3578 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3579
3580 //debug_print_menuitem (" item: ", item, "");
3581 if (item->fType & MF_POPUP)
3582 {
3583 POPUPMENU *menu = MENU_GetMenu(item->hSubMenu);
3584 if (!menu) return -1;
3585 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3586 }
3587 else
3588 {
3589 /* We used to (from way back then) mask the result to 0xff. */
3590 /* I don't know why and it seems wrong as the documented */
3591 /* return flag MF_SEPARATOR is outside that mask. */
3592 return (item->fType | item->fState);
3593 }
3594}
3595
3596
3597/**********************************************************************
3598 * GetMenuItemCount32 (USER32.262)
3599 */
3600INT WINAPI GetMenuItemCount( HMENU hMenu )
3601{
3602 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3603
3604 dprintf(("USER32: GetMenuItemCount"));
3605
3606 if (!IS_A_MENU(menu)) return -1;
3607 //TRACE("(%04x) returning %d\n",
3608 // hMenu, menu->nItems );
3609 return menu->nItems;
3610}
3611
3612/**********************************************************************
3613 * GetMenuItemID32 (USER32.263)
3614 */
3615UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3616{
3617 MENUITEM * lpmi;
3618
3619 dprintf(("USER32: GetMenuItemID"));
3620
3621 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0xFFFFFFFF;
3622 if (lpmi->fType & MF_POPUP) return 0xFFFFFFFF;
3623 return lpmi->wID;
3624
3625}
3626
3627/*******************************************************************
3628 * InsertMenu32A (USER32.322)
3629 */
3630BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3631 UINT id, LPCSTR str )
3632{
3633 MENUITEM *item;
3634
3635 if (IS_STRING_ITEM(flags) && str)
3636 dprintf(("USER32: InsertMenuA %x %d %x %d %s", hMenu, pos, flags, id, str));
3637 // TRACE("hMenu %04x, pos %d, flags %08x, "
3638 // "id %04x, str '%s'\n",
3639 // hMenu, pos, flags, id, str );
3640 else // TRACE("hMenu %04x, pos %d, flags %08x, "
3641 dprintf(("USER32: InsertMenuA %x %d %x %d %x", hMenu, pos, flags, id, str));
3642 // "id %04x, str %08lx (not a string)\n",
3643 // hMenu, pos, flags, id, (DWORD)str );
3644
3645 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3646
3647 if (!(MENU_SetItemData( item, flags, id, str )))
3648 {
3649 RemoveMenu( hMenu, pos, flags );
3650 return FALSE;
3651 }
3652
3653 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3654 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3655
3656 item->hCheckBit = item->hUnCheckBit = 0;
3657 return TRUE;
3658}
3659
3660
3661/*******************************************************************
3662 * InsertMenu32W (USER32.325)
3663 */
3664BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3665 UINT id, LPCWSTR str )
3666{
3667 BOOL ret;
3668
3669 if (IS_STRING_ITEM(flags) && str)
3670 {
3671 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3672 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3673 HeapFree( GetProcessHeap(), 0, newstr );
3674 return ret;
3675 }
3676 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3677}
3678
3679
3680/*******************************************************************
3681 * AppendMenu32A (USER32.5)
3682 */
3683BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3684 UINT id, LPCSTR data )
3685{
3686 dprintf(("USER32: AppendMenuA"));
3687
3688 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3689}
3690
3691
3692/*******************************************************************
3693 * AppendMenu32W (USER32.6)
3694 */
3695BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3696 UINT id, LPCWSTR data )
3697{
3698 dprintf(("USER32: AppendMenuW %x %x %d %x", hMenu, flags, id, data));
3699
3700 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3701}
3702
3703
3704/**********************************************************************
3705 * RemoveMenu (USER32.441)
3706 */
3707BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3708{
3709 LPPOPUPMENU menu;
3710 MENUITEM *item;
3711
3712 dprintf(("USER32: RemoveMenu %x %d %x", hMenu, nPos, wFlags));
3713
3714 //TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3715 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3716 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3717
3718 /* Remove item */
3719
3720 MENU_FreeItemData( item );
3721
3722 if (--menu->nItems == 0)
3723 {
3724 HeapFree(GetProcessHeap(), 0, menu->items );
3725 menu->items = NULL;
3726 }
3727 else
3728 {
3729 while(nPos < menu->nItems)
3730 {
3731 *item = *(item+1);
3732 item++;
3733 nPos++;
3734 }
3735 menu->items = (MENUITEM*)HeapReAlloc(GetProcessHeap(), 0, menu->items,
3736 menu->nItems * sizeof(MENUITEM) );
3737 }
3738 return TRUE;
3739}
3740
3741
3742/**********************************************************************
3743 * DeleteMenu32 (USER32.129)
3744 */
3745BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3746{
3747 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3748
3749 dprintf(("USER32: DeleteMenu %x %d %x", hMenu, nPos, wFlags));
3750
3751 if (!item) return FALSE;
3752 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3753 /* nPos is now the position of the item */
3754 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3755 return TRUE;
3756}
3757
3758
3759/*******************************************************************
3760 * ModifyMenu32A (USER32.397)
3761 */
3762BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3763 UINT id, LPCSTR str )
3764{
3765 MENUITEM *item;
3766
3767
3768 if (IS_STRING_ITEM(flags))
3769 {
3770 dprintf(("USER32: ModifyMenuA, %x %d %x %d %s", hMenu, pos, flags, id, str));
3771 //TRACE("%04x %d %04x %04x '%s'\n",
3772 // hMenu, pos, flags, id, str ? str : "#NULL#" );
3773 if (!str) return FALSE;
3774 }
3775 else
3776 {
3777 dprintf(("USER32: ModifyMenuA, %x %d %x %d %x", hMenu, pos, flags, id, str));
3778 //TRACE("%04x %d %04x %04x %08lx\n",
3779 // hMenu, pos, flags, id, (DWORD)str );
3780 }
3781
3782 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3783 return MENU_SetItemData( item, flags, id, str );
3784}
3785
3786
3787/*******************************************************************
3788 * ModifyMenu32W (USER32.398)
3789 */
3790BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3791 UINT id, LPCWSTR str )
3792{
3793 BOOL ret;
3794
3795 if (IS_STRING_ITEM(flags) && str)
3796 {
3797 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3798 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3799 HeapFree( GetProcessHeap(), 0, newstr );
3800 return ret;
3801 }
3802 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3803}
3804
3805
3806/**********************************************************************
3807 * CreatePopupMenu32 (USER32.82)
3808 */
3809HMENU WINAPI CreatePopupMenu(void)
3810{
3811 HMENU hmenu;
3812 POPUPMENU *menu;
3813
3814 dprintf(("USER32: CreatePopupMenu"));
3815
3816 if (!(hmenu = CreateMenu())) return 0;
3817 menu = MENU_GetMenu(hmenu);
3818 menu->wFlags |= MF_POPUP;
3819 menu->bTimeToHide = FALSE;
3820 return hmenu;
3821}
3822
3823
3824/**********************************************************************
3825 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3826 */
3827DWORD WINAPI GetMenuCheckMarkDimensions(void)
3828{
3829 dprintf(("USER32: GetMenuCheckMarkDimensions"));
3830
3831 return MAKELONG( check_bitmap_width, check_bitmap_height );
3832}
3833
3834
3835/**********************************************************************
3836 * SetMenuItemBitmaps32 (USER32.490)
3837 */
3838BOOL WINAPI SetMenuItemBitmaps(HMENU hMenu, UINT nPos, UINT wFlags,
3839 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3840{
3841 MENUITEM *item;
3842
3843 dprintf(("USER32: SetMenuItemBitmaps %x %d %x %x %x", hMenu, nPos, wFlags, hNewCheck, hNewUnCheck));
3844
3845 //TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3846 // hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3847 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3848
3849 if (!hNewCheck && !hNewUnCheck)
3850 {
3851 item->fState &= ~MF_USECHECKBITMAPS;
3852 }
3853 else /* Install new bitmaps */
3854 {
3855 item->hCheckBit = hNewCheck;
3856 item->hUnCheckBit = hNewUnCheck;
3857 item->fState |= MF_USECHECKBITMAPS;
3858 }
3859 return TRUE;
3860}
3861
3862
3863/**********************************************************************
3864 * CreateMenu (USER32.81)
3865 */
3866HMENU WINAPI CreateMenu(void)
3867{
3868 HMENU hMenu;
3869 LPPOPUPMENU menu;
3870
3871 dprintf(("USER32: CreateMenu"));
3872
3873 if (!(hMenu = (HMENU)HeapAlloc(GetProcessHeap(),0,sizeof(POPUPMENU)))) return 0;
3874 menu = (LPPOPUPMENU)hMenu;
3875
3876 ZeroMemory(menu, sizeof(POPUPMENU));
3877 menu->wMagic = MENU_MAGIC;
3878 menu->FocusedItem = NO_SELECTED_ITEM;
3879 menu->bTimeToHide = FALSE;
3880
3881 //TRACE("return %04x\n", hMenu );
3882
3883 return hMenu;
3884}
3885
3886
3887/**********************************************************************
3888 * DestroyMenu32 (USER32.134)
3889 */
3890BOOL WINAPI DestroyMenu( HMENU hMenu )
3891{
3892 //TRACE("(%04x)\n", hMenu);
3893
3894 dprintf(("USER32: DestroyMenu %x", hMenu));
3895
3896 /* Silently ignore attempts to destroy default system popup */
3897
3898 if (hMenu && hMenu != MENU_DefSysPopup)
3899 {
3900 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3901 HWND pTPWnd = MENU_GetTopPopupWnd();
3902
3903 if( pTPWnd && (hMenu == GetWindowLongA(pTPWnd,0)) )
3904 SetWindowLongA(pTPWnd,0,0);
3905
3906 if (IS_A_MENU(lppop))
3907 {
3908 lppop->wMagic = 0; /* Mark it as destroyed */
3909
3910 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3911 (!pTPWnd || (lppop->hWnd != pTPWnd)))
3912 DestroyWindow( lppop->hWnd );
3913
3914 if (lppop->items) /* recursively destroy submenus */
3915 {
3916 int i;
3917 MENUITEM *item = lppop->items;
3918 for (i = lppop->nItems; i > 0; i--, item++)
3919 {
3920 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3921 MENU_FreeItemData( item );
3922 }
3923 HeapFree(GetProcessHeap(), 0, lppop->items );
3924 }
3925 HeapFree(GetProcessHeap(),0,(LPVOID)hMenu);
3926 MENU_ReleaseTopPopupWnd();
3927 }
3928 else
3929 {
3930 MENU_ReleaseTopPopupWnd();
3931 return FALSE;
3932 }
3933 }
3934 return (hMenu != MENU_DefSysPopup);
3935}
3936
3937/**********************************************************************
3938 * GetSystemMenu32 (USER32.291)
3939 */
3940HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3941{
3942 HMENU retvalue = 0;
3943 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hWnd);
3944
3945 dprintf(("USER32: GetSystemMenu"));
3946
3947 if (win32wnd)
3948 {
3949 HMENU hSysMenu = getSysMenu(hWnd);
3950 if(hSysMenu)
3951 {
3952 if( bRevert )
3953 {
3954 DestroyMenu(hSysMenu);
3955 hSysMenu = 0;
3956 setSysMenu(hWnd,hSysMenu);
3957 }
3958 else
3959 {
3960 POPUPMENU *menu = MENU_GetMenu(hSysMenu);
3961 if(menu)
3962 {
3963 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
3964 menu->items[0].hSubMenu = MENU_CopySysPopup();
3965 }
3966 else
3967 {
3968 //WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
3969 // wndPtr->hSysMenu, hWnd);
3970 hSysMenu = 0;
3971 setSysMenu(hWnd,hSysMenu);
3972 }
3973 }
3974 }
3975
3976 if(!hSysMenu && (GetWindowLongA(hWnd,GWL_STYLE) & WS_SYSMENU) )
3977 {
3978 hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
3979 setSysMenu(hWnd,hSysMenu);
3980 }
3981
3982 if( hSysMenu )
3983 {
3984 POPUPMENU *menu;
3985 retvalue = GetSubMenu(hSysMenu, 0);
3986
3987 /* Store the dummy sysmenu handle to facilitate the refresh */
3988 /* of the close button if the SC_CLOSE item change */
3989 menu = MENU_GetMenu(retvalue);
3990 if (menu)
3991 menu->hSysMenuOwner = hSysMenu;
3992 }
3993 }
3994 return retvalue;
3995}
3996
3997
3998/*******************************************************************
3999 * SetSystemMenu32 (USER32.508)
4000 */
4001BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4002{
4003 Win32BaseWindow *win32wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
4004
4005 dprintf(("USER32: SetSystemMenu"));
4006
4007 if (win32wnd)
4008 {
4009 if (win32wnd->GetSysMenu()) DestroyMenu(win32wnd->GetSysMenu());
4010 win32wnd->SetSysMenu(MENU_GetSysMenu( hwnd, hMenu ));
4011 return TRUE;
4012 }
4013 return FALSE;
4014}
4015
4016/**********************************************************************
4017 * GetMenu32 (USER32.257)
4018 */
4019HMENU WINAPI GetMenu( HWND hWnd )
4020{
4021 HMENU retvalue;
4022
4023 dprintf(("USER32: GetMenu %x", hWnd));
4024
4025 if (GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) return 0;
4026 else return getMenu(hWnd);
4027}
4028
4029/**********************************************************************
4030 * SetMenu32 (USER32.487)
4031 */
4032BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4033{
4034 //TRACE("(%04x, %04x);\n", hWnd, hMenu);
4035
4036 dprintf(("USER32: SetMenu %x %x", hWnd, hMenu));
4037
4038 if (hMenu && !IsMenu(hMenu))
4039 {
4040 //WARN("hMenu is not a menu handle\n");
4041 return FALSE;
4042 }
4043
4044
4045 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD))
4046 {
4047 if (GetCapture() == hWnd) ReleaseCapture();
4048
4049 setMenu(hWnd,hMenu);
4050 if (hMenu != 0)
4051 {
4052 LPPOPUPMENU lpmenu;
4053
4054 if (!(lpmenu = MENU_GetMenu(hMenu)))
4055 {
4056 return FALSE;
4057 }
4058 lpmenu->hWnd = hWnd;
4059 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
4060 lpmenu->Height = 0; /* Make sure we recalculate the size */
4061 }
4062//SvL: This fixes the menu in standard mine sweeper (window isn't visible
4063// when SetMenu is called)
4064// if (IsWindowVisible(hWnd))
4065 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4066 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4067 return TRUE;
4068 }
4069 return FALSE;
4070}
4071
4072
4073/**********************************************************************
4074 * GetSubMenu32 (USER32.288)
4075 */
4076HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4077{
4078 MENUITEM * lpmi;
4079
4080 dprintf(("USER32: GetSubMenu %x %d", nPos));
4081
4082 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4083 if (!(lpmi->fType & MF_POPUP)) return 0;
4084 return lpmi->hSubMenu;
4085}
4086
4087
4088/**********************************************************************
4089 * DrawMenuBar (USER32.161)
4090 */
4091BOOL WINAPI DrawMenuBar( HWND hWnd )
4092{
4093 LPPOPUPMENU lppop;
4094
4095 dprintf(("USER32: DrawMenuBar %x", hWnd));
4096
4097 if (!(GetWindowLongA(hWnd,GWL_STYLE) & WS_CHILD) && getMenu(hWnd))
4098 {
4099 lppop = MENU_GetMenu(getMenu(hWnd));
4100 if (lppop == NULL)
4101 {
4102 return FALSE;
4103 }
4104
4105 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4106 lppop->hwndOwner = hWnd;
4107 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4108 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4109 return TRUE;
4110 }
4111 return FALSE;
4112}
4113
4114
4115/***********************************************************************
4116 * EndMenu (USER.187) (USER32.175)
4117 */
4118void WINAPI EndMenu(void)
4119{
4120 dprintf(("USER32: EndMenu not implemented!"));
4121 /*
4122 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
4123 */
4124
4125 fEndMenu = TRUE;
4126}
4127
4128
4129/*****************************************************************
4130 * LoadMenu32A (USER32.370)
4131 */
4132HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4133{
4134 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4135
4136 dprintf(("USER32: LoadMenuA %x %x", instance, name));
4137
4138 if (!hrsrc) return 0;
4139 return LoadMenuIndirectA( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4140}
4141
4142
4143/*****************************************************************
4144 * LoadMenu32W (USER32.373)
4145 */
4146HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4147{
4148 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4149
4150 dprintf(("USER32: LoadMenuW %x %x", instance, name));
4151
4152 if (!hrsrc) return 0;
4153 return LoadMenuIndirectW( (MENUITEMTEMPLATEHEADER*)LoadResource( instance, hrsrc ));
4154}
4155
4156
4157/**********************************************************************
4158 * LoadMenuIndirect32A (USER32.371)
4159 */
4160HMENU WINAPI LoadMenuIndirectA(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate)
4161{
4162 HMENU hMenu;
4163 WORD version, offset;
4164 LPCSTR p = (LPCSTR)lpMenuTemplate;
4165
4166 dprintf(("USER32: LoadMenuIndirectA"));
4167
4168 //TRACE("%p\n", template );
4169 version = GET_WORD(p);
4170 p += sizeof(WORD);
4171 switch (version)
4172 {
4173 case 0:
4174 offset = GET_WORD(p);
4175 p += sizeof(WORD) + offset;
4176 if (!(hMenu = CreateMenu())) return 0;
4177 if (!MENU_ParseResource( p, hMenu, TRUE ))
4178 {
4179 DestroyMenu( hMenu );
4180 return 0;
4181 }
4182 return hMenu;
4183 case 1:
4184 offset = GET_WORD(p);
4185 p += sizeof(WORD) + offset;
4186 if (!(hMenu = CreateMenu())) return 0;
4187 if (!MENUEX_ParseResource( p, hMenu))
4188 {
4189 DestroyMenu( hMenu );
4190 return 0;
4191 }
4192 return hMenu;
4193 default:
4194 //ERR("version %d not supported.\n", version);
4195 return 0;
4196 }
4197}
4198
4199
4200/**********************************************************************
4201 * LoadMenuIndirect32W (USER32.372)
4202 */
4203HMENU WINAPI LoadMenuIndirectW(CONST MENUITEMTEMPLATEHEADER *lpMenuTemplate )
4204{
4205 dprintf(("USER32: LoadMenuIndirectW"));
4206
4207 /* FIXME: is there anything different between A and W? */
4208 return LoadMenuIndirectA(lpMenuTemplate);
4209}
4210
4211
4212/**********************************************************************
4213 * IsMenu32 (USER32.346)
4214 */
4215BOOL WINAPI IsMenu(HMENU hmenu)
4216{
4217 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4218
4219 dprintf(("USER32: IsMenu"));
4220
4221 return IS_A_MENU(menu);
4222}
4223
4224/**********************************************************************
4225 * GetMenuItemInfo32_common
4226 */
4227
4228static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4229 LPMENUITEMINFOA lpmii, BOOL unicode)
4230{
4231 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4232
4233 //debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
4234
4235 if (!menu)
4236 return FALSE;
4237
4238 if (lpmii->fMask & MIIM_TYPE) {
4239 lpmii->fType = menu->fType;
4240 switch (MENU_ITEM_TYPE(menu->fType)) {
4241 case MF_STRING:
4242 if (menu->text && lpmii->dwTypeData && lpmii->cch) {
4243 if (unicode) {
4244 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4245 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4246 } else {
4247 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4248 lpmii->cch = lstrlenA(menu->text);
4249 }
4250 }
4251 break;
4252 case MF_OWNERDRAW:
4253 case MF_BITMAP:
4254 lpmii->dwTypeData = menu->text;
4255 /* fall through */
4256 default:
4257 lpmii->cch = 0;
4258 }
4259 }
4260
4261 if (lpmii->fMask & MIIM_STRING) {
4262 if (unicode) {
4263 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4264 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4265 } else {
4266 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4267 lpmii->cch = lstrlenA(menu->text);
4268 }
4269 }
4270
4271 if (lpmii->fMask & MIIM_FTYPE)
4272 lpmii->fType = menu->fType;
4273
4274 if (lpmii->fMask & MIIM_BITMAP)
4275 lpmii->hbmpItem = menu->hbmpItem;
4276
4277 if (lpmii->fMask & MIIM_STATE)
4278 lpmii->fState = menu->fState;
4279
4280 if (lpmii->fMask & MIIM_ID)
4281 lpmii->wID = menu->wID;
4282
4283 if (lpmii->fMask & MIIM_SUBMENU)
4284 lpmii->hSubMenu = menu->hSubMenu;
4285
4286 if (lpmii->fMask & MIIM_CHECKMARKS) {
4287 lpmii->hbmpChecked = menu->hCheckBit;
4288 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4289 }
4290 if (lpmii->fMask & MIIM_DATA)
4291 lpmii->dwItemData = menu->dwItemData;
4292
4293 return TRUE;
4294}
4295
4296/**********************************************************************
4297 * GetMenuItemInfoA (USER32.264)
4298 */
4299BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4300 LPMENUITEMINFOA lpmii)
4301{
4302 dprintf(("USER32: GetMenuItemInfoA"));
4303
4304 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4305}
4306
4307/**********************************************************************
4308 * GetMenuItemInfoW (USER32.265)
4309 */
4310BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4311 LPMENUITEMINFOW lpmii)
4312{
4313 dprintf(("USER32: GetMenuItemInfoW"));
4314
4315 return GetMenuItemInfo_common (hmenu, item, bypos,
4316 (LPMENUITEMINFOA)lpmii, TRUE);
4317}
4318
4319/**********************************************************************
4320 * SetMenuItemInfo32_common
4321 */
4322
4323static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4324 const MENUITEMINFOA *lpmii,
4325 BOOL unicode)
4326{
4327 if (!menu) return FALSE;
4328
4329 if (lpmii->fMask & MIIM_TYPE ) {
4330 /* Get rid of old string. */
4331 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4332 HeapFree(GetProcessHeap(), 0, menu->text);
4333 menu->text = NULL;
4334 }
4335
4336 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4337 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4338 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4339
4340 menu->text = lpmii->dwTypeData;
4341
4342 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4343 if (unicode)
4344 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4345 else
4346 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4347 }
4348 }
4349
4350 if (lpmii->fMask & MIIM_FTYPE ) {
4351 /* free the string when the type is changing */
4352 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4353 HeapFree(GetProcessHeap(), 0, menu->text);
4354 menu->text = NULL;
4355 }
4356 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4357 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4358 }
4359
4360 if (lpmii->fMask & MIIM_STRING ) {
4361 /* free the string when used */
4362 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4363 HeapFree(GetProcessHeap(), 0, menu->text);
4364 if (unicode)
4365 menu->text = HEAP_strdupWtoA(GetProcessHeap(), 0, (LPWSTR) lpmii->dwTypeData);
4366 else
4367 menu->text = HEAP_strdupA(GetProcessHeap(), 0, lpmii->dwTypeData);
4368 }
4369 }
4370
4371 if (lpmii->fMask & MIIM_STATE)
4372 {
4373 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4374 menu->fState = lpmii->fState;
4375 }
4376
4377 if (lpmii->fMask & MIIM_ID)
4378 menu->wID = lpmii->wID;
4379
4380 if (lpmii->fMask & MIIM_SUBMENU) {
4381 menu->hSubMenu = lpmii->hSubMenu;
4382 if (menu->hSubMenu) {
4383 POPUPMENU *subMenu = MENU_GetMenu((UINT)menu->hSubMenu);
4384 if (IS_A_MENU(subMenu)) {
4385 subMenu->wFlags |= MF_POPUP;
4386 menu->fType |= MF_POPUP;
4387 }
4388 else
4389 /* FIXME: Return an error ? */
4390 menu->fType &= ~MF_POPUP;
4391 }
4392 else
4393 menu->fType &= ~MF_POPUP;
4394 }
4395
4396 if (lpmii->fMask & MIIM_CHECKMARKS)
4397 {
4398 menu->hCheckBit = lpmii->hbmpChecked;
4399 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4400 }
4401 if (lpmii->fMask & MIIM_DATA)
4402 menu->dwItemData = lpmii->dwItemData;
4403
4404 //debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4405 return TRUE;
4406}
4407
4408/**********************************************************************
4409 * SetMenuItemInfo32A (USER32.491)
4410 */
4411BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4412 const MENUITEMINFOA *lpmii)
4413{
4414 dprintf(("USER32: SetMenuItemInfoA %x %d %d %x", hmenu, item, bypos, lpmii));
4415
4416 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4417 lpmii, FALSE);
4418}
4419
4420/**********************************************************************
4421 * SetMenuItemInfo32W (USER32.492)
4422 */
4423BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4424 const MENUITEMINFOW *lpmii)
4425{
4426 dprintf(("USER32: SetMenuItemInfoW"));
4427
4428 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4429 (const MENUITEMINFOA*)lpmii, TRUE);
4430}
4431
4432/**********************************************************************
4433 * SetMenuDefaultItem (USER32.489)
4434 *
4435 */
4436BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4437{
4438 UINT i;
4439 POPUPMENU *menu;
4440 MENUITEM *item;
4441
4442
4443 dprintf(("USER32: SetMenuDefaultItem"));
4444 //TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4445
4446 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4447
4448 /* reset all default-item flags */
4449 item = menu->items;
4450 for (i = 0; i < menu->nItems; i++, item++)
4451 {
4452 item->fState &= ~MFS_DEFAULT;
4453 }
4454
4455 /* no default item */
4456 if ( -1 == uItem)
4457 {
4458 return TRUE;
4459 }
4460
4461 item = menu->items;
4462 if ( bypos )
4463 {
4464 if ( uItem >= menu->nItems ) return FALSE;
4465 item[uItem].fState |= MFS_DEFAULT;
4466 return TRUE;
4467 }
4468 else
4469 {
4470 for (i = 0; i < menu->nItems; i++, item++)
4471 {
4472 if (item->wID == uItem)
4473 {
4474 item->fState |= MFS_DEFAULT;
4475 return TRUE;
4476 }
4477 }
4478
4479 }
4480 return FALSE;
4481}
4482
4483/**********************************************************************
4484 * GetMenuDefaultItem (USER32.260)
4485 */
4486UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4487{
4488 POPUPMENU *menu;
4489 MENUITEM * item;
4490 UINT i = 0;
4491
4492 dprintf(("USER32: GetMenuDefaultItem"));
4493 //TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4494
4495 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4496
4497 /* find default item */
4498 item = menu->items;
4499
4500 /* empty menu */
4501 if (! item) return -1;
4502
4503 while ( !( item->fState & MFS_DEFAULT ) )
4504 {
4505 i++; item++;
4506 if (i >= menu->nItems ) return -1;
4507 }
4508
4509 /* default: don't return disabled items */
4510 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4511
4512 /* search rekursiv when needed */
4513 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4514 {
4515 UINT ret;
4516 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4517 if ( -1 != ret ) return ret;
4518
4519 /* when item not found in submenu, return the popup item */
4520 }
4521 return ( bypos ) ? i : item->wID;
4522
4523}
4524
4525/**********************************************************************
4526 * InsertMenuItem32A (USER32.323)
4527 */
4528BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4529 const MENUITEMINFOA *lpmii)
4530{
4531 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4532
4533 dprintf(("USER32: InsertMenuItemA %x %d %d %x", hMenu, uItem, bypos, lpmii->wID));
4534
4535 return SetMenuItemInfo_common(item, lpmii, FALSE);
4536}
4537
4538
4539/**********************************************************************
4540 * InsertMenuItem32W (USER32.324)
4541 */
4542BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4543 const MENUITEMINFOW *lpmii)
4544{
4545 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4546
4547 dprintf(("USER32: InsertMenuItemW"));
4548
4549 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4550}
4551
4552/**********************************************************************
4553 * CheckMenuRadioItem32 (USER32.47)
4554 */
4555
4556BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4557 UINT first, UINT last, UINT check,
4558 UINT bypos)
4559{
4560 MENUITEM *mifirst, *milast, *micheck;
4561 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4562
4563 dprintf(("USER32: CheckMenuRadioItem"));
4564 //TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4565 // hMenu, first, last, check, bypos);
4566
4567 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4568 milast = MENU_FindItem (&mlast, &last, bypos);
4569 micheck = MENU_FindItem (&mcheck, &check, bypos);
4570
4571 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4572 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4573 micheck > milast || micheck < mifirst)
4574 return FALSE;
4575
4576 while (mifirst <= milast)
4577 {
4578 if (mifirst == micheck)
4579 {
4580 mifirst->fType |= MFT_RADIOCHECK;
4581 mifirst->fState |= MFS_CHECKED;
4582 } else {
4583 mifirst->fType &= ~MFT_RADIOCHECK;
4584 mifirst->fState &= ~MFS_CHECKED;
4585 }
4586 mifirst++;
4587 }
4588
4589 return TRUE;
4590}
4591
4592/**********************************************************************
4593 * GetMenuItemRect32 (USER32.266)
4594 *
4595 * ATTENTION: Here, the returned values in rect are the screen
4596 * coordinates of the item just like if the menu was
4597 * always on the upper left side of the application.
4598 *
4599 */
4600BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4601 LPRECT rect)
4602{
4603 POPUPMENU *itemMenu;
4604 MENUITEM *item;
4605 HWND referenceHwnd;
4606
4607 //TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4608
4609 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4610 referenceHwnd = hwnd;
4611
4612 if(!hwnd)
4613 {
4614 itemMenu = MENU_GetMenu(hMenu);
4615 if (itemMenu == NULL) {
4616 SetLastError(ERROR_INVALID_PARAMETER);
4617 return FALSE;
4618 }
4619
4620 if(itemMenu->hWnd == 0)
4621 return FALSE;
4622 referenceHwnd = itemMenu->hWnd;
4623 }
4624
4625 if ((rect == NULL) || (item == NULL)) {
4626 SetLastError(ERROR_INVALID_PARAMETER);
4627 return FALSE;
4628 }
4629 *rect = item->rect;
4630
4631 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4632 dprintf(("USER32: GetMenuItemRect %x %x %d (%d,%d)(%d,%d)", hwnd, hMenu, uItem, rect->left, rect->top, rect->right, rect->bottom));
4633
4634 return TRUE;
4635}
4636
4637/**********************************************************************
4638 * SetMenuInfo
4639 *
4640 * FIXME
4641 * MIM_APPLYTOSUBMENUS
4642 * actually use the items to draw the menu
4643 */
4644BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4645{
4646 POPUPMENU *menu;
4647
4648 dprintf(("USER32: SetMenuInfo"));
4649 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4650
4651 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu=MENU_GetMenu(hMenu)))
4652 {
4653
4654 if (lpmi->fMask & MIM_BACKGROUND)
4655 menu->hbrBack = lpmi->hbrBack;
4656
4657 if (lpmi->fMask & MIM_HELPID)
4658 menu->dwContextHelpID = lpmi->dwContextHelpID;
4659
4660 if (lpmi->fMask & MIM_MAXHEIGHT)
4661 menu->cyMax = lpmi->cyMax;
4662
4663 if (lpmi->fMask & MIM_MENUDATA)
4664 menu->dwMenuData = lpmi->dwMenuData;
4665
4666 if (lpmi->fMask & MIM_STYLE)
4667 menu->dwStyle = lpmi->dwStyle;
4668
4669 return TRUE;
4670 }
4671 return FALSE;
4672}
4673
4674/**********************************************************************
4675 * GetMenuInfo
4676 *
4677 * NOTES
4678 * win98/NT5.0
4679 *
4680 */
4681BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4682{ POPUPMENU *menu;
4683
4684 dprintf(("USER32: GetMenuInfo"));
4685 //TRACE("(0x%04x %p)\n", hMenu, lpmi);
4686
4687 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4688 {
4689
4690 if (lpmi->fMask & MIM_BACKGROUND)
4691 lpmi->hbrBack = menu->hbrBack;
4692
4693 if (lpmi->fMask & MIM_HELPID)
4694 lpmi->dwContextHelpID = menu->dwContextHelpID;
4695
4696 if (lpmi->fMask & MIM_MAXHEIGHT)
4697 lpmi->cyMax = menu->cyMax;
4698
4699 if (lpmi->fMask & MIM_MENUDATA)
4700 lpmi->dwMenuData = menu->dwMenuData;
4701
4702 if (lpmi->fMask & MIM_STYLE)
4703 lpmi->dwStyle = menu->dwStyle;
4704
4705 return TRUE;
4706 }
4707 return FALSE;
4708}
4709
4710/**********************************************************************
4711 * SetMenuContextHelpId (USER32.488)
4712 */
4713BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4714{
4715 LPPOPUPMENU menu;
4716
4717 dprintf(("USER32: SetMenuContextHelpId"));
4718 //TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4719
4720 menu = MENU_GetMenu(hMenu);
4721 if (menu)
4722 {
4723 menu->dwContextHelpID = dwContextHelpID;
4724 return TRUE;
4725 }
4726 return FALSE;
4727}
4728
4729/**********************************************************************
4730 * GetMenuContextHelpId (USER32.488)
4731 */
4732DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4733{
4734 LPPOPUPMENU menu;
4735
4736 dprintf(("USER32: GetMenuContextHelpId"));
4737
4738 menu = MENU_GetMenu(hMenu);
4739 if (menu)
4740 {
4741 return menu->dwContextHelpID;
4742 }
4743 return 0;
4744}
4745
4746/**********************************************************************
4747 * MenuItemFromPoint (USER32.387)
4748 */
4749UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4750{
4751 dprintf(("USER32: MenuItemFromPoint not implemented!"));
4752
4753 return 0;
4754}
4755
4756/**********************************************************************
4757 * IsMenuActive (Internal)
4758 */
4759BOOL IsMenuActive(void)
4760{
4761 return pTopPopupWnd && (GetWindowLongA(pTopPopupWnd,GWL_STYLE) & WS_VISIBLE);
4762}
4763
4764BOOL POPUPMENU_Register()
4765{
4766 WNDCLASSA wndClass;
4767 BOOL rc;
4768
4769//SvL: Don't check this now
4770// if (GlobalFindAtomA(POPUPMENUCLASSNAME)) return FALSE;
4771
4772 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
4773 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
4774 wndClass.lpfnWndProc = (WNDPROC)PopupMenuWndProc;
4775 wndClass.cbClsExtra = 0;
4776 wndClass.cbWndExtra = sizeof(HMENU);
4777 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
4778 wndClass.hbrBackground = NULL_BRUSH;
4779 wndClass.lpszClassName = POPUPMENUCLASSNAME;
4780
4781 rc = RegisterClassA(&wndClass);
4782 MENU_Init();
4783
4784 return rc;
4785}
4786//******************************************************************************
4787//******************************************************************************
4788BOOL POPUPMENU_Unregister()
4789{
4790 if (GlobalFindAtomA(POPUPMENUCLASSNAME))
4791 return UnregisterClassA(POPUPMENUCLASSNAME,(HINSTANCE)NULL);
4792 else return FALSE;
4793}
4794//******************************************************************************
4795//******************************************************************************
4796
Note: See TracBrowser for help on using the repository browser.