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

Last change on this file since 2852 was 2852, checked in by cbratschi, 26 years ago

merged with Corel WINE 20000212, added WS_EX_CONTEXTHELP

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