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

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

change menu hilite background color in OS/2 look mode

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