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

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

16 bits menu handle fixes

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