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

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

reference count (window + class objects) rewrite

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