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

Last change on this file since 3153 was 3153, checked in by cbratschi, 25 years ago

merged with Corel 20000317, small icon

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