source: trunk/src/user32/new/menu.cpp@ 2290

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

* empty log message *

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