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

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

Bugfix for bitmap menu items (wrong cast)

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