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

Last change on this file since 5968 was 5968, checked in by sandervl, 24 years ago

bug fixes

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