[USER32_WINETEST]
[reactos.git] / rostests / winetests / user32 / menu.c
1 /*
2 * Unit tests for menus
3 *
4 * Copyright 2005 Robert Shearman
5 * Copyright 2007 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define _WIN32_WINNT 0x0501
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #define OEMRESOURCE /* For OBM_MNARROW */
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35
36 #include "wine/test.h"
37
38 static ATOM atomMenuCheckClass;
39
40 static BOOL (WINAPI *pGetMenuInfo)(HMENU,LPCMENUINFO);
41 static UINT (WINAPI *pSendInput)(UINT, INPUT*, size_t);
42 static BOOL (WINAPI *pSetMenuInfo)(HMENU,LPCMENUINFO);
43 static BOOL (WINAPI *pEndMenu) (void);
44
45 static void init_function_pointers(void)
46 {
47 HMODULE hdll = GetModuleHandleA("user32");
48
49 #define GET_PROC(func) \
50 p ## func = (void*)GetProcAddress(hdll, #func); \
51 if(!p ## func) \
52 trace("GetProcAddress(%s) failed\n", #func);
53
54 GET_PROC(GetMenuInfo)
55 GET_PROC(SendInput)
56 GET_PROC(SetMenuInfo)
57 GET_PROC(EndMenu)
58
59 #undef GET_PROC
60 }
61
62 static BOOL correct_behavior(void)
63 {
64 HMENU hmenu;
65 MENUITEMINFO info;
66 BOOL rc;
67
68 hmenu = CreateMenu();
69
70 memset(&info, 0, sizeof(MENUITEMINFO));
71 info.cbSize= sizeof(MENUITEMINFO);
72 SetLastError(0xdeadbeef);
73 rc = GetMenuItemInfo(hmenu, 0, TRUE, &info);
74 /* Win9x : 0xdeadbeef
75 * NT4 : ERROR_INVALID_PARAMETER
76 * >= W2K : ERROR_MENU_ITEM_NOT_FOUND
77 */
78 if (!rc && GetLastError() != ERROR_MENU_ITEM_NOT_FOUND)
79 {
80 win_skip("NT4 and below can't handle a bigger MENUITEMINFO struct\n");
81 DestroyMenu(hmenu);
82 return FALSE;
83 }
84
85 DestroyMenu(hmenu);
86 return TRUE;
87 }
88
89 static LRESULT WINAPI menu_check_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
90 {
91 switch (msg)
92 {
93 case WM_ENTERMENULOOP:
94 /* mark window as having entered menu loop */
95 SetWindowLongPtr(hwnd, GWLP_USERDATA, TRUE);
96 /* exit menu modal loop
97 * ( A SendMessage does not work on NT3.51 here ) */
98 return PostMessage(hwnd, WM_CANCELMODE, 0, 0);
99 }
100 return DefWindowProc(hwnd, msg, wparam, lparam);
101 }
102
103 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define
104 * our own type */
105 typedef struct
106 {
107 DWORD type;
108 union
109 {
110 MOUSEINPUT mi;
111 KEYBDINPUT ki;
112 HARDWAREINPUT hi;
113 } u;
114 } TEST_INPUT;
115
116 /* globals to communicate between test and wndproc */
117
118 static BOOL bMenuVisible;
119 static BOOL got_input;
120 static HMENU hMenus[4];
121
122 #define MOD_SIZE 10
123 #define MOD_NRMENUS 8
124
125 /* menu texts with their sizes */
126 static struct {
127 LPCSTR text;
128 SIZE size; /* size of text up to any \t */
129 SIZE sc_size; /* size of the short-cut */
130 } MOD_txtsizes[] = {
131 { "Pinot &Noir" },
132 { "&Merlot\bF4" },
133 { "Shira&z\tAlt+S" },
134 { "" },
135 { NULL }
136 };
137
138 static unsigned int MOD_maxid;
139 static RECT MOD_rc[MOD_NRMENUS];
140 static int MOD_avec, MOD_hic;
141 static int MOD_odheight;
142 static SIZE MODsizes[MOD_NRMENUS]= { {MOD_SIZE, MOD_SIZE},{MOD_SIZE, MOD_SIZE},
143 {MOD_SIZE, MOD_SIZE},{MOD_SIZE, MOD_SIZE}};
144 static int MOD_GotDrawItemMsg = FALSE;
145 static int gflag_initmenupopup,
146 gflag_entermenuloop,
147 gflag_initmenu;
148
149 /* wndproc used by test_menu_ownerdraw() */
150 static LRESULT WINAPI menu_ownerdraw_wnd_proc(HWND hwnd, UINT msg,
151 WPARAM wparam, LPARAM lparam)
152 {
153 static HMENU hmenupopup;
154 switch (msg)
155 {
156 case WM_INITMENUPOPUP:
157 gflag_initmenupopup++;
158 hmenupopup = (HMENU) wparam;
159 break;
160 case WM_ENTERMENULOOP:
161 gflag_entermenuloop++;
162 break;
163 case WM_INITMENU:
164 gflag_initmenu++;
165 break;
166 case WM_MEASUREITEM:
167 {
168 MEASUREITEMSTRUCT* pmis = (MEASUREITEMSTRUCT*)lparam;
169 if( winetest_debug)
170 trace("WM_MEASUREITEM received data %lx size %dx%d\n",
171 pmis->itemData, pmis->itemWidth, pmis->itemHeight);
172 MOD_odheight = pmis->itemHeight;
173 pmis->itemWidth = MODsizes[pmis->itemData].cx;
174 pmis->itemHeight = MODsizes[pmis->itemData].cy;
175 return TRUE;
176 }
177 case WM_DRAWITEM:
178 {
179 DRAWITEMSTRUCT * pdis;
180 TEXTMETRIC tm;
181 HPEN oldpen;
182 char chrs[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
183 SIZE sz;
184 int i;
185 pdis = (DRAWITEMSTRUCT *) lparam;
186 if( winetest_debug) {
187 RECT rc;
188 GetMenuItemRect( hwnd, (HMENU)pdis->hwndItem, pdis->itemData ,&rc);
189 trace("WM_DRAWITEM received hwnd %p hmenu %p itemdata %ld item %d rc %d,%d-%d,%d itemrc: %d,%d-%d,%d\n",
190 hwnd, pdis->hwndItem, pdis->itemData, pdis->itemID,
191 pdis->rcItem.left, pdis->rcItem.top,
192 pdis->rcItem.right,pdis->rcItem.bottom,
193 rc.left,rc.top,rc.right,rc.bottom);
194 oldpen=SelectObject( pdis->hDC, GetStockObject(
195 pdis->itemState & ODS_SELECTED ? WHITE_PEN :BLACK_PEN));
196 Rectangle( pdis->hDC, pdis->rcItem.left,pdis->rcItem.top,
197 pdis->rcItem.right,pdis->rcItem.bottom );
198 SelectObject( pdis->hDC, oldpen);
199 }
200 /* calculate widths of some menu texts */
201 if( ! MOD_txtsizes[0].size.cx)
202 for(i = 0; MOD_txtsizes[i].text; i++) {
203 char buf[100], *p;
204 RECT rc={0,0,0,0};
205 strcpy( buf, MOD_txtsizes[i].text);
206 if( ( p = strchr( buf, '\t'))) {
207 *p = '\0';
208 DrawText( pdis->hDC, p + 1, -1, &rc,
209 DT_SINGLELINE|DT_CALCRECT);
210 MOD_txtsizes[i].sc_size.cx= rc.right - rc.left;
211 MOD_txtsizes[i].sc_size.cy= rc.bottom - rc.top;
212 }
213 DrawText( pdis->hDC, buf, -1, &rc,
214 DT_SINGLELINE|DT_CALCRECT);
215 MOD_txtsizes[i].size.cx= rc.right - rc.left;
216 MOD_txtsizes[i].size.cy= rc.bottom - rc.top;
217 }
218
219 if( pdis->itemData > MOD_maxid) return TRUE;
220 /* store the rectangl */
221 MOD_rc[pdis->itemData] = pdis->rcItem;
222 /* calculate average character width */
223 GetTextExtentPoint( pdis->hDC, chrs, 52, &sz );
224 MOD_avec = (sz.cx + 26)/52;
225 GetTextMetrics( pdis->hDC, &tm);
226 MOD_hic = tm.tmHeight;
227 MOD_GotDrawItemMsg = TRUE;
228 return TRUE;
229 }
230 case WM_ENTERIDLE:
231 {
232 ok( lparam || broken(!lparam), /* win9x, nt4 */
233 "Menu window handle is NULL!\n");
234 if( lparam) {
235 HMENU hmenu = (HMENU)SendMessageA( (HWND)lparam, MN_GETHMENU, 0, 0);
236 ok( hmenupopup == hmenu, "MN_GETHMENU returns %p expected %p\n",
237 hmenu, hmenupopup);
238 }
239 PostMessage(hwnd, WM_CANCELMODE, 0, 0);
240 return TRUE;
241 }
242
243 }
244 return DefWindowProc(hwnd, msg, wparam, lparam);
245 }
246
247 static void register_menu_check_class(void)
248 {
249 WNDCLASS wc =
250 {
251 0,
252 menu_check_wnd_proc,
253 0,
254 0,
255 GetModuleHandle(NULL),
256 NULL,
257 LoadCursor(NULL, IDC_ARROW),
258 (HBRUSH)(COLOR_BTNFACE+1),
259 NULL,
260 TEXT("WineMenuCheck"),
261 };
262
263 atomMenuCheckClass = RegisterClass(&wc);
264 }
265
266 /* demonstrates that windows locks the menu object so that it is still valid
267 * even after a client calls DestroyMenu on it */
268 static void test_menu_locked_by_window(void)
269 {
270 BOOL ret;
271 HMENU hmenu;
272 HWND hwnd = CreateWindowEx(0, MAKEINTATOM(atomMenuCheckClass), NULL,
273 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
274 NULL, NULL, NULL, NULL);
275 ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
276 hmenu = CreateMenu();
277 ok(hmenu != NULL, "CreateMenu failed with error %d\n", GetLastError());
278 ret = InsertMenu(hmenu, 0, MF_STRING, 0, TEXT("&Test"));
279 ok(ret, "InsertMenu failed with error %d\n", GetLastError());
280 ret = SetMenu(hwnd, hmenu);
281 ok(ret, "SetMenu failed with error %d\n", GetLastError());
282 ret = DestroyMenu(hmenu);
283 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
284
285 ret = DrawMenuBar(hwnd);
286 todo_wine {
287 ok(ret, "DrawMenuBar failed with error %d\n", GetLastError());
288 }
289 ret = IsMenu(GetMenu(hwnd));
290 ok(!ret || broken(ret) /* nt4 */, "Menu handle should have been destroyed\n");
291
292 SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
293 /* did we process the WM_INITMENU message? */
294 ret = GetWindowLongPtr(hwnd, GWLP_USERDATA);
295 todo_wine {
296 ok(ret, "WM_INITMENU should have been sent\n");
297 }
298
299 DestroyWindow(hwnd);
300 }
301
302 /* demonstrates that subpopup's are locked
303 * even after a client calls DestroyMenu on it */
304 static LRESULT WINAPI subpopuplocked_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
305 {
306 HWND hwndmenu;
307 switch (msg)
308 {
309 case WM_ENTERIDLE:
310 hwndmenu = GetCapture();
311 if( hwndmenu) {
312 PostMessage( hwndmenu, WM_KEYDOWN, VK_DOWN, 0);
313 PostMessage( hwndmenu, WM_KEYDOWN, VK_RIGHT, 0);
314 PostMessage( hwndmenu, WM_KEYDOWN, VK_RETURN, 0);
315 }
316 }
317 return DefWindowProc(hwnd, msg, wparam, lparam);
318 }
319
320 static void test_subpopup_locked_by_menu(void)
321 {
322 DWORD gle;
323 BOOL ret;
324 HMENU hmenu, hsubmenu;
325 MENUINFO mi = { sizeof( MENUINFO)};
326 MENUITEMINFO mii = { sizeof( MENUITEMINFO)};
327 HWND hwnd;
328 const int itemid = 0x1234567;
329 if( !pGetMenuInfo)
330 {
331 win_skip("GetMenuInfo is not available\n");
332 return;
333 }
334 /* create window, popupmenu with one subpopup */
335 hwnd = CreateWindowEx(0, MAKEINTATOM(atomMenuCheckClass), NULL,
336 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
337 NULL, NULL, NULL, NULL);
338 ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
339 SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR) subpopuplocked_wnd_proc);
340 hmenu = CreatePopupMenu();
341 ok(hmenu != NULL, "CreateMenu failed with error %d\n", GetLastError());
342 hsubmenu = CreatePopupMenu();
343 ok(hsubmenu != NULL, "CreateMenu failed with error %d\n", GetLastError());
344 ret = InsertMenu(hmenu, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)hsubmenu,
345 TEXT("PopUpLockTest"));
346 ok(ret, "InsertMenu failed with error %d\n", GetLastError());
347 ret = InsertMenu(hsubmenu, 0, MF_BYPOSITION | MF_STRING, itemid, TEXT("PopUpMenu"));
348 ok(ret, "InsertMenu failed with error %d\n", GetLastError());
349 /* first some tests that all this functions properly */
350 mii.fMask = MIIM_SUBMENU;
351 ret = GetMenuItemInfo( hmenu, 0, TRUE, &mii);
352 ok( ret, "GetMenuItemInfo failed error %d\n", GetLastError());
353 ok( mii.hSubMenu == hsubmenu, "submenu is %p\n", mii.hSubMenu);
354 mi.fMask |= MIM_STYLE;
355 ret = pGetMenuInfo( hsubmenu, &mi);
356 ok( ret , "GetMenuInfo returned 0 with error %d\n", GetLastError());
357 ret = IsMenu( hsubmenu);
358 ok( ret , "Menu handle is not valid\n");
359 SetLastError( 0xdeadbeef);
360 ret = TrackPopupMenu( hmenu, 0x100, 100,100, 0, hwnd, NULL);
361 if( ret == (itemid & 0xffff)) {
362 win_skip("not on 16 bit menu subsystem\n");
363 DestroyMenu( hsubmenu);
364 } else {
365 gle = GetLastError();
366 ok( ret == itemid , "TrackPopupMenu returned %d error is %d\n", ret, gle);
367 ok( gle == 0 ||
368 broken( gle == 0xdeadbeef), /* win2k0 */
369 "Last error is %d\n", gle);
370 /* then destroy the sub-popup */
371 ret = DestroyMenu( hsubmenu);
372 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
373 /* and repeat the tests */
374 mii.fMask = MIIM_SUBMENU;
375 ret = GetMenuItemInfo( hmenu, 0, TRUE, &mii);
376 ok( ret, "GetMenuItemInfo failed error %d\n", GetLastError());
377 /* GetMenuInfo fails now */
378 ok( mii.hSubMenu == hsubmenu, "submenu is %p\n", mii.hSubMenu);
379 mi.fMask |= MIM_STYLE;
380 ret = pGetMenuInfo( hsubmenu, &mi);
381 ok( !ret , "GetMenuInfo should have failed\n");
382 /* IsMenu says it is not */
383 ret = IsMenu( hsubmenu);
384 ok( !ret , "Menu handle should be invalid\n");
385 /* but TrackPopupMenu still works! */
386 SetLastError( 0xdeadbeef);
387 ret = TrackPopupMenu( hmenu, 0x100, 100,100, 0, hwnd, NULL);
388 gle = GetLastError();
389 todo_wine {
390 ok( ret == itemid , "TrackPopupMenu returned %d error is %d\n", ret, gle);
391 }
392 ok( gle == 0 ||
393 broken(gle == 0xdeadbeef) || /* wow64 */
394 broken(gle == ERROR_INVALID_PARAMETER), /* win2k0 */
395 "Last error is %d\n", gle);
396 }
397 /* clean up */
398 DestroyMenu( hmenu);
399 DestroyWindow(hwnd);
400 }
401
402 static void test_menu_ownerdraw(void)
403 {
404 int i,j,k;
405 BOOL ret;
406 HMENU hmenu;
407 LONG leftcol;
408 HWND hwnd = CreateWindowEx(0, MAKEINTATOM(atomMenuCheckClass), NULL,
409 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
410 NULL, NULL, NULL, NULL);
411 ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
412 if( !hwnd) return;
413 SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR)menu_ownerdraw_wnd_proc);
414 hmenu = CreatePopupMenu();
415 ok(hmenu != NULL, "CreateMenu failed with error %d\n", GetLastError());
416 if( !hmenu) { DestroyWindow(hwnd);return;}
417 k=0;
418 for( j=0;j<2;j++) /* create columns */
419 for(i=0;i<2;i++) { /* create rows */
420 ret = AppendMenu( hmenu, MF_OWNERDRAW |
421 (i==0 ? MF_MENUBREAK : 0), k, MAKEINTRESOURCE(k));
422 k++;
423 ok( ret, "AppendMenu failed for %d\n", k-1);
424 }
425 MOD_maxid = k-1;
426 assert( k <= sizeof(MOD_rc)/sizeof(RECT));
427 /* display the menu */
428 ret = TrackPopupMenu( hmenu, 0x100, 100,100, 0, hwnd, NULL);
429
430 /* columns have a 4 pixel gap between them */
431 ok( MOD_rc[0].right + 4 == MOD_rc[2].left,
432 "item rectangles are not separated by 4 pixels space\n");
433 /* height should be what the MEASUREITEM message has returned */
434 ok( MOD_rc[0].bottom - MOD_rc[0].top == MOD_SIZE,
435 "menu item has wrong height: %d should be %d\n",
436 MOD_rc[0].bottom - MOD_rc[0].top, MOD_SIZE);
437 /* no gaps between the rows */
438 ok( MOD_rc[0].bottom - MOD_rc[1].top == 0,
439 "There should not be a space between the rows, gap is %d\n",
440 MOD_rc[0].bottom - MOD_rc[1].top);
441 /* test the correct value of the item height that was sent
442 * by the WM_MEASUREITEM message */
443 ok( MOD_odheight == HIWORD( GetDialogBaseUnits()) || /* WinNT,2k,XP */
444 MOD_odheight == MOD_hic, /* Win95,98,ME */
445 "Wrong height field in MEASUREITEMSTRUCT, expected %d or %d actual %d\n",
446 HIWORD( GetDialogBaseUnits()), MOD_hic, MOD_odheight);
447 /* test what MF_MENUBREAK did at the first position. Also show
448 * that an MF_SEPARATOR is ignored in the height calculation. */
449 leftcol= MOD_rc[0].left;
450 ModifyMenu( hmenu, 0, MF_BYCOMMAND| MF_OWNERDRAW| MF_SEPARATOR, 0, 0);
451 /* display the menu */
452 ret = TrackPopupMenu( hmenu, 0x100, 100,100, 0, hwnd, NULL);
453 /* left should be 4 pixels less now */
454 ok( leftcol == MOD_rc[0].left + 4,
455 "columns should be 4 pixels to the left (actual %d).\n",
456 leftcol - MOD_rc[0].left);
457 /* test width */
458 ok( MOD_rc[0].right - MOD_rc[0].left == 2 * MOD_avec + MOD_SIZE,
459 "width of owner drawn menu item is wrong. Got %d expected %d\n",
460 MOD_rc[0].right - MOD_rc[0].left , 2*MOD_avec + MOD_SIZE);
461 /* and height */
462 ok( MOD_rc[0].bottom - MOD_rc[0].top == MOD_SIZE,
463 "Height is incorrect. Got %d expected %d\n",
464 MOD_rc[0].bottom - MOD_rc[0].top, MOD_SIZE);
465
466 /* test width/height of an ownerdraw menu bar as well */
467 ret = DestroyMenu(hmenu);
468 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
469 hmenu = CreateMenu();
470 ok(hmenu != NULL, "CreateMenu failed with error %d\n", GetLastError());
471 if( !hmenu) { DestroyWindow(hwnd);return;}
472 MOD_maxid=1;
473 for(i=0;i<2;i++) {
474 ret = AppendMenu( hmenu, MF_OWNERDRAW , i, 0);
475 ok( ret, "AppendMenu failed for %d\n", i);
476 }
477 ret = SetMenu( hwnd, hmenu);
478 UpdateWindow( hwnd); /* hack for wine to draw the window + menu */
479 ok(ret, "SetMenu failed with error %d\n", GetLastError());
480 /* test width */
481 ok( MOD_rc[0].right - MOD_rc[0].left == 2 * MOD_avec + MOD_SIZE,
482 "width of owner drawn menu item is wrong. Got %d expected %d\n",
483 MOD_rc[0].right - MOD_rc[0].left , 2*MOD_avec + MOD_SIZE);
484 /* test hight */
485 ok( MOD_rc[0].bottom - MOD_rc[0].top == GetSystemMetrics( SM_CYMENU) - 1,
486 "Height of owner drawn menu item is wrong. Got %d expected %d\n",
487 MOD_rc[0].bottom - MOD_rc[0].top, GetSystemMetrics( SM_CYMENU) - 1);
488
489 /* clean up */
490 ret = DestroyMenu(hmenu);
491 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
492 DestroyWindow(hwnd);
493 }
494
495 /* helper for test_menu_bmp_and_string() */
496 static void test_mbs_help( int ispop, int hassub, int mnuopt,
497 HWND hwnd, int arrowwidth, int count, HBITMAP hbmp,
498 SIZE bmpsize, LPCSTR text, SIZE size, SIZE sc_size)
499 {
500 BOOL ret;
501 HMENU hmenu, submenu;
502 MENUITEMINFO mii={ sizeof( MENUITEMINFO )};
503 MENUINFO mi;
504 RECT rc;
505 CHAR text_copy[16];
506 int hastab, expect;
507 int failed = 0;
508
509 MOD_GotDrawItemMsg = FALSE;
510 mii.fMask = MIIM_FTYPE | MIIM_DATA | MIIM_STATE;
511 mii.fType = 0;
512 /* check the menu item unless MNS_CHECKORBMP is set */
513 mii.fState = (mnuopt != 2 ? MFS_CHECKED : MFS_UNCHECKED);
514 mii.dwItemData =0;
515 MODsizes[0] = bmpsize;
516 hastab = 0;
517 if( text ) {
518 char *p;
519 mii.fMask |= MIIM_STRING;
520 strcpy(text_copy, text);
521 mii.dwTypeData = text_copy; /* structure member declared non-const */
522 if( ( p = strchr( text, '\t'))) {
523 hastab = *(p + 1) ? 2 : 1;
524 }
525 }
526 /* tabs don't make sense in menubars */
527 if(hastab && !ispop) return;
528 if( hbmp) {
529 mii.fMask |= MIIM_BITMAP;
530 mii.hbmpItem = hbmp;
531 }
532 submenu = CreateMenu();
533 ok( submenu != 0, "CreateMenu failed with error %d\n", GetLastError());
534 if( ispop)
535 hmenu = CreatePopupMenu();
536 else
537 hmenu = CreateMenu();
538 ok( hmenu != 0, "Create{Popup}Menu failed with error %d\n", GetLastError());
539 if( hassub) {
540 mii.fMask |= MIIM_SUBMENU;
541 mii.hSubMenu = submenu;
542 }
543 if( mnuopt) {
544 mi.cbSize = sizeof(mi);
545 mi.fMask = MIM_STYLE;
546 pGetMenuInfo( hmenu, &mi);
547 if( mnuopt) mi.dwStyle |= mnuopt == 1 ? MNS_NOCHECK : MNS_CHECKORBMP;
548 ret = pSetMenuInfo( hmenu, &mi);
549 ok( ret, "SetMenuInfo failed with error %d\n", GetLastError());
550 }
551 ret = InsertMenuItem( hmenu, 0, FALSE, &mii);
552 ok( ret, "InsertMenuItem failed with error %d\n", GetLastError());
553 failed = !ret;
554 if( winetest_debug) {
555 HDC hdc=GetDC(hwnd);
556 RECT rc = {100, 50, 400, 70};
557 char buf[100];
558
559 sprintf( buf,"%d text \"%s\" mnuopt %d", count, text ? text: "(nil)", mnuopt);
560 FillRect( hdc, &rc, (HBRUSH) COLOR_WINDOW);
561 TextOut( hdc, 10, 50, buf, strlen( buf));
562 ReleaseDC( hwnd, hdc);
563 }
564 if(ispop)
565 ret = TrackPopupMenu( hmenu, 0x100, 100,100, 0, hwnd, NULL);
566 else {
567 ret = SetMenu( hwnd, hmenu);
568 ok(ret, "SetMenu failed with error %d\n", GetLastError());
569 DrawMenuBar( hwnd);
570 }
571 ret = GetMenuItemRect( hwnd, hmenu, 0, &rc);
572 if (0) /* comment out menu size checks, behavior is different in almost every Windows version */
573 /* the tests should however succeed on win2000, XP and Wine (at least up to 1.1.15) */
574 /* with a variety of dpis and desktop font sizes */
575 {
576 /* check menu width */
577 if( ispop)
578 expect = ( text || hbmp ?
579 4 + (mnuopt != 1 ? GetSystemMetrics(SM_CXMENUCHECK) : 0)
580 : 0) +
581 arrowwidth + MOD_avec + (hbmp ?
582 ((INT_PTR)hbmp<0||(INT_PTR)hbmp>12 ? bmpsize.cx + 2 : GetSystemMetrics( SM_CXMENUSIZE) + 2)
583 : 0) +
584 (text && hastab ? /* TAB space */
585 MOD_avec + ( hastab==2 ? sc_size.cx : 0) : 0) +
586 (text ? 2 + (text[0] ? size.cx :0): 0) ;
587 else
588 expect = !(text || hbmp) ? 0 :
589 ( hbmp ? (text ? 2:0) + bmpsize.cx : 0 ) +
590 (text ? 2 * MOD_avec + (text[0] ? size.cx :0): 0) ;
591 ok( rc.right - rc.left == expect,
592 "menu width wrong, got %d expected %d\n", rc.right - rc.left, expect);
593 failed = failed || !(rc.right - rc.left == expect);
594 /* check menu height */
595 if( ispop)
596 expect = max( ( !(text || hbmp) ? GetSystemMetrics( SM_CYMENUSIZE)/2 : 0),
597 max( (text ? max( 2 + size.cy, MOD_hic + 4) : 0),
598 (hbmp ?
599 ((INT_PTR)hbmp<0||(INT_PTR)hbmp>12 ?
600 bmpsize.cy + 2
601 : GetSystemMetrics( SM_CYMENUSIZE) + 2)
602 : 0)));
603 else
604 expect = ( !(text || hbmp) ? GetSystemMetrics( SM_CYMENUSIZE)/2 :
605 max( GetSystemMetrics( SM_CYMENU) - 1, (hbmp ? bmpsize.cy : 0)));
606 ok( rc.bottom - rc.top == expect,
607 "menu height wrong, got %d expected %d (%d)\n",
608 rc.bottom - rc.top, expect, GetSystemMetrics( SM_CYMENU));
609 failed = failed || !(rc.bottom - rc.top == expect);
610 if( hbmp == HBMMENU_CALLBACK && MOD_GotDrawItemMsg) {
611 /* check the position of the bitmap */
612 /* horizontal */
613 if (!ispop)
614 expect = 3;
615 else if (mnuopt == 0)
616 expect = 4 + GetSystemMetrics(SM_CXMENUCHECK);
617 else if (mnuopt == 1)
618 expect = 4;
619 else /* mnuopt == 2 */
620 expect = 2;
621 ok( expect == MOD_rc[0].left,
622 "bitmap left is %d expected %d\n", MOD_rc[0].left, expect);
623 failed = failed || !(expect == MOD_rc[0].left);
624 /* vertical */
625 expect = (rc.bottom - rc.top - MOD_rc[0].bottom + MOD_rc[0].top) / 2;
626 ok( expect == MOD_rc[0].top,
627 "bitmap top is %d expected %d\n", MOD_rc[0].top, expect);
628 failed = failed || !(expect == MOD_rc[0].top);
629 }
630 }
631 /* if there was a failure, report details */
632 if( failed) {
633 trace("*** count %d %s text \"%s\" bitmap %p bmsize %d,%d textsize %d+%d,%d mnuopt %d hastab %d\n",
634 count, (ispop? "POPUP": "MENUBAR"),text ? text: "(nil)", hbmp, bmpsize.cx, bmpsize.cy,
635 size.cx, size.cy, sc_size.cx, mnuopt, hastab);
636 trace(" check %d,%d arrow %d avechar %d\n",
637 GetSystemMetrics(SM_CXMENUCHECK ),
638 GetSystemMetrics(SM_CYMENUCHECK ),arrowwidth, MOD_avec);
639 if( hbmp == HBMMENU_CALLBACK)
640 trace( " rc %d,%d-%d,%d bmp.rc %d,%d-%d,%d\n",
641 rc.left, rc.top, rc.top, rc.bottom, MOD_rc[0].left,
642 MOD_rc[0].top,MOD_rc[0].right, MOD_rc[0].bottom);
643 }
644 /* clean up */
645 ret = DestroyMenu(submenu);
646 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
647 ret = DestroyMenu(hmenu);
648 ok(ret, "DestroyMenu failed with error %d\n", GetLastError());
649 }
650
651
652 static void test_menu_bmp_and_string(void)
653 {
654 BYTE bmfill[300];
655 HBITMAP hbm_arrow;
656 BITMAP bm;
657 INT arrowwidth;
658 HWND hwnd;
659 HMENU hsysmenu;
660 MENUINFO mi= {sizeof(MENUINFO)};
661 MENUITEMINFOA mii= {sizeof(MENUITEMINFOA)};
662 int count, szidx, txtidx, bmpidx, hassub, mnuopt, ispop;
663
664 if( !pGetMenuInfo)
665 {
666 win_skip("GetMenuInfo is not available\n");
667 return;
668 }
669
670 memset( bmfill, 0xcc, sizeof( bmfill));
671 hwnd = CreateWindowEx(0, MAKEINTATOM(atomMenuCheckClass), NULL, WS_SYSMENU |
672 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
673 NULL, NULL, NULL, NULL);
674 hbm_arrow=LoadBitmap( 0, (CHAR*)OBM_MNARROW);
675 GetObject( hbm_arrow, sizeof(bm), &bm);
676 arrowwidth = bm.bmWidth;
677 ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
678 if( !hwnd) return;
679 /* test system menu */
680 hsysmenu = GetSystemMenu( hwnd, FALSE);
681 ok( hsysmenu != NULL, "GetSystemMenu failed with error %d\n", GetLastError());
682 mi.fMask = MIM_STYLE;
683 mi.dwStyle = 0;
684 ok( pGetMenuInfo( hsysmenu, &mi), "GetMenuInfo failed gle=%d\n", GetLastError());
685 ok( MNS_CHECKORBMP == mi.dwStyle, "System Menu Style is %08x, without the bit %08x\n",
686 mi.dwStyle, MNS_CHECKORBMP);
687 mii.fMask = MIIM_BITMAP;
688 mii.hbmpItem = NULL;
689 ok( GetMenuItemInfoA( hsysmenu, SC_CLOSE, FALSE, &mii), "GetMenuItemInfoA failed gle=%d\n", GetLastError());
690 ok( HBMMENU_POPUP_CLOSE == mii.hbmpItem, "Item info did not get the right hbitmap: got %p expected %p\n",
691 mii.hbmpItem, HBMMENU_POPUP_CLOSE);
692
693 SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR)menu_ownerdraw_wnd_proc);
694
695 if( winetest_debug)
696 trace(" check %d,%d arrow %d avechar %d\n",
697 GetSystemMetrics(SM_CXMENUCHECK ),
698 GetSystemMetrics(SM_CYMENUCHECK ),arrowwidth, MOD_avec);
699 count = 0;
700 MOD_maxid = 0;
701 for( ispop=1; ispop >= 0; ispop--){
702 static SIZE bmsizes[]= {
703 {10,10},{38,38},{1,30},{55,5}};
704 for( szidx=0; szidx < sizeof( bmsizes) / sizeof( SIZE); szidx++) {
705 HBITMAP hbm = CreateBitmap( bmsizes[szidx].cx, bmsizes[szidx].cy,1,1,bmfill);
706 HBITMAP bitmaps[] = { HBMMENU_CALLBACK, hbm, HBMMENU_POPUP_CLOSE, NULL };
707 ok( hbm != 0, "CreateBitmap failed err %d\n", GetLastError());
708 for( txtidx = 0; txtidx < sizeof(MOD_txtsizes)/sizeof(MOD_txtsizes[0]); txtidx++) {
709 for( hassub = 0; hassub < 2 ; hassub++) { /* add submenu item */
710 for( mnuopt = 0; mnuopt < 3 ; mnuopt++){ /* test MNS_NOCHECK/MNS_CHECKORBMP */
711 for( bmpidx = 0; bmpidx <sizeof(bitmaps)/sizeof(HBITMAP); bmpidx++) {
712 /* no need to test NULL bitmaps of several sizes */
713 if( !bitmaps[bmpidx] && szidx > 0) continue;
714 /* the HBMMENU_POPUP not to test for menu bars */
715 if( !ispop &&
716 bitmaps[bmpidx] >= HBMMENU_POPUP_CLOSE &&
717 bitmaps[bmpidx] <= HBMMENU_POPUP_MINIMIZE) continue;
718 if( !ispop && hassub) continue;
719 test_mbs_help( ispop, hassub, mnuopt,
720 hwnd, arrowwidth, ++count,
721 bitmaps[bmpidx],
722 bmsizes[szidx],
723 MOD_txtsizes[txtidx].text,
724 MOD_txtsizes[txtidx].size,
725 MOD_txtsizes[txtidx].sc_size);
726 }
727 }
728 }
729 }
730 DeleteObject( hbm);
731 }
732 }
733 /* clean up */
734 DestroyWindow(hwnd);
735 }
736
737 static void test_menu_add_string( void )
738 {
739 HMENU hmenu;
740 MENUITEMINFO info;
741 BOOL rc;
742 int ret;
743
744 char string[0x80];
745 char string2[0x80];
746
747 char strback[0x80];
748 WCHAR strbackW[0x80];
749 static CHAR blah[] = "blah";
750 static const WCHAR expectedString[] = {'D','u','m','m','y',' ','s','t','r','i','n','g', 0};
751
752 hmenu = CreateMenu();
753
754 memset( &info, 0, sizeof info );
755 info.cbSize = sizeof info;
756 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_ID;
757 info.dwTypeData = blah;
758 info.cch = 6;
759 info.dwItemData = 0;
760 info.wID = 1;
761 info.fState = 0;
762 InsertMenuItem(hmenu, 0, TRUE, &info );
763
764 memset( &info, 0, sizeof info );
765 info.cbSize = sizeof info;
766 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
767 info.dwTypeData = string;
768 info.cch = sizeof string;
769 string[0] = 0;
770 GetMenuItemInfo( hmenu, 0, TRUE, &info );
771
772 ok( !strcmp( string, "blah" ), "menu item name differed\n");
773
774 /* Test combination of ownerdraw and strings with GetMenuItemString(A/W) */
775 strcpy(string, "Dummy string");
776 memset(&info, 0x00, sizeof(info));
777 info.cbSize= sizeof(MENUITEMINFO);
778 info.fMask= MIIM_FTYPE | MIIM_STRING; /* Set OwnerDraw + typeData */
779 info.fType= MFT_OWNERDRAW;
780 info.dwTypeData= string;
781 rc = InsertMenuItem( hmenu, 0, TRUE, &info );
782 ok (rc, "InsertMenuItem failed\n");
783
784 strcpy(string,"Garbage");
785 ok (GetMenuString( hmenu, 0, strback, 99, MF_BYPOSITION), "GetMenuString on ownerdraw entry failed\n");
786 ok (!strcmp( strback, "Dummy string" ), "Menu text from Ansi version incorrect\n");
787
788 SetLastError(0xdeadbeef);
789 ret = GetMenuStringW( hmenu, 0, strbackW, 99, MF_BYPOSITION );
790 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
791 win_skip("GetMenuStringW is not implemented\n");
792 else
793 {
794 ok (ret, "GetMenuStringW on ownerdraw entry failed\n");
795 ok (!lstrcmpW( strbackW, expectedString ), "Menu text from Unicode version incorrect\n");
796 }
797
798 /* Just change ftype to string and see what text is stored */
799 memset(&info, 0x00, sizeof(info));
800 info.cbSize= sizeof(MENUITEMINFO);
801 info.fMask= MIIM_FTYPE; /* Set string type */
802 info.fType= MFT_STRING;
803 info.dwTypeData= (char *)0xdeadbeef;
804 rc = SetMenuItemInfo( hmenu, 0, TRUE, &info );
805 ok (rc, "SetMenuItemInfo failed\n");
806
807 /* Did we keep the old dwTypeData? */
808 ok (GetMenuString( hmenu, 0, strback, 99, MF_BYPOSITION), "GetMenuString on ownerdraw entry failed\n");
809 ok (!strcmp( strback, "Dummy string" ), "Menu text from Ansi version incorrect\n");
810
811 /* Ensure change to bitmap type fails */
812 memset(&info, 0x00, sizeof(info));
813 info.cbSize= sizeof(MENUITEMINFO);
814 info.fMask= MIIM_FTYPE; /* Set as bitmap type */
815 info.fType= MFT_BITMAP;
816 info.dwTypeData= (char *)0xdeadbee2;
817 rc = SetMenuItemInfo( hmenu, 0, TRUE, &info );
818 ok (!rc, "SetMenuItemInfo unexpectedly worked\n");
819
820 /* Just change ftype back and ensure data hasn't been freed */
821 info.fType= MFT_OWNERDRAW; /* Set as ownerdraw type */
822 info.dwTypeData= (char *)0xdeadbee3;
823 rc = SetMenuItemInfo( hmenu, 0, TRUE, &info );
824 ok (rc, "SetMenuItemInfo failed\n");
825
826 /* Did we keep the old dwTypeData? */
827 ok (GetMenuString( hmenu, 0, strback, 99, MF_BYPOSITION), "GetMenuString on ownerdraw entry failed\n");
828 ok (!strcmp( strback, "Dummy string" ), "Menu text from Ansi version incorrect\n");
829
830 /* Just change string value (not type) */
831 memset(&info, 0x00, sizeof(info));
832 info.cbSize= sizeof(MENUITEMINFO);
833 info.fMask= MIIM_STRING; /* Set typeData */
834 strcpy(string2, "string2");
835 info.dwTypeData= string2;
836 rc = SetMenuItemInfo( hmenu, 0, TRUE, &info );
837 ok (rc, "SetMenuItemInfo failed\n");
838
839 ok (GetMenuString( hmenu, 0, strback, 99, MF_BYPOSITION), "GetMenuString on ownerdraw entry failed\n");
840 ok (!strcmp( strback, "string2" ), "Menu text from Ansi version incorrect\n");
841
842 /* crashes with wine 0.9.5 */
843 memset(&info, 0x00, sizeof(info));
844 info.cbSize= sizeof(MENUITEMINFO);
845 info.fMask= MIIM_FTYPE | MIIM_STRING; /* Set OwnerDraw + typeData */
846 info.fType= MFT_OWNERDRAW;
847 rc = InsertMenuItem( hmenu, 0, TRUE, &info );
848 ok (rc, "InsertMenuItem failed\n");
849 ok (!GetMenuString( hmenu, 0, NULL, 0, MF_BYPOSITION),
850 "GetMenuString on ownerdraw entry succeeded.\n");
851 SetLastError(0xdeadbeef);
852 ret = GetMenuStringW( hmenu, 0, NULL, 0, MF_BYPOSITION);
853 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
854 win_skip("GetMenuStringW is not implemented\n");
855 else
856 ok (!ret, "GetMenuStringW on ownerdraw entry succeeded.\n");
857
858 DestroyMenu( hmenu );
859 }
860
861 /* define building blocks for the menu item info tests */
862 static int strncmpW( const WCHAR *str1, const WCHAR *str2, int n )
863 {
864 if (n <= 0) return 0;
865 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
866 return *str1 - *str2;
867 }
868
869 static WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
870 {
871 WCHAR *p = dst;
872 while ((*p++ = *src++));
873 return dst;
874 }
875
876 static void insert_menu_item( int line, HMENU hmenu, BOOL ansi, UINT mask, UINT type, UINT state, UINT id,
877 HMENU submenu, HBITMAP checked, HBITMAP unchecked, ULONG_PTR data,
878 void *type_data, UINT len, HBITMAP item, BOOL expect )
879 {
880 MENUITEMINFOA info;
881 BOOL ret;
882
883 /* magic bitmap handle to test smaller cbSize */
884 if (item == (HBITMAP)(ULONG_PTR)0xdeadbeef)
885 info.cbSize = FIELD_OFFSET(MENUITEMINFOA,hbmpItem);
886 else
887 info.cbSize = sizeof(info);
888 info.fMask = mask;
889 info.fType = type;
890 info.fState = state;
891 info.wID = id;
892 info.hSubMenu = submenu;
893 info.hbmpChecked = checked;
894 info.hbmpUnchecked = unchecked;
895 info.dwItemData = data;
896 info.dwTypeData = type_data;
897 info.cch = len;
898 info.hbmpItem = item;
899 SetLastError( 0xdeadbeef );
900 if (ansi) ret = InsertMenuItemA( hmenu, 0, TRUE, &info );
901 else ret = InsertMenuItemW( hmenu, 0, TRUE, (MENUITEMINFOW*)&info );
902 if (!expect) ok_(__FILE__, line)( !ret, "InsertMenuItem should have failed.\n" );
903 else ok_(__FILE__, line)( ret, "InsertMenuItem failed, err %u\n", GetLastError());
904 }
905
906 static void check_menu_item_info( int line, HMENU hmenu, BOOL ansi, UINT mask, UINT type, UINT state,
907 UINT id, HMENU submenu, HBITMAP checked, HBITMAP unchecked,
908 ULONG_PTR data, void *type_data, UINT in_len, UINT out_len,
909 HBITMAP item, LPCSTR expname, BOOL expect, BOOL expstring )
910 {
911 MENUITEMINFOA info;
912 BOOL ret;
913 WCHAR buffer[80];
914
915 SetLastError( 0xdeadbeef );
916 memset( &info, 0xcc, sizeof(info) );
917 info.cbSize = sizeof(info);
918 info.fMask = mask;
919 info.dwTypeData = type_data;
920 info.cch = in_len;
921
922 ret = ansi ? GetMenuItemInfoA( hmenu, 0, TRUE, &info ) :
923 GetMenuItemInfoW( hmenu, 0, TRUE, (MENUITEMINFOW *)&info );
924 if (!expect)
925 {
926 ok_(__FILE__, line)( !ret, "GetMenuItemInfo should have failed.\n" );
927 return;
928 }
929 ok_(__FILE__, line)( ret, "GetMenuItemInfo failed, err %u\n", GetLastError());
930 if (mask & MIIM_TYPE)
931 ok_(__FILE__, line)( info.fType == type || info.fType == LOWORD(type),
932 "wrong type %x/%x\n", info.fType, type );
933 if (mask & MIIM_STATE)
934 ok_(__FILE__, line)( info.fState == state || info.fState == LOWORD(state),
935 "wrong state %x/%x\n", info.fState, state );
936 if (mask & MIIM_ID)
937 ok_(__FILE__, line)( info.wID == id || info.wID == LOWORD(id),
938 "wrong id %x/%x\n", info.wID, id );
939 if (mask & MIIM_SUBMENU)
940 ok_(__FILE__, line)( info.hSubMenu == submenu || (ULONG_PTR)info.hSubMenu == LOWORD(submenu),
941 "wrong submenu %p/%p\n", info.hSubMenu, submenu );
942 if (mask & MIIM_CHECKMARKS)
943 {
944 ok_(__FILE__, line)( info.hbmpChecked == checked || (ULONG_PTR)info.hbmpChecked == LOWORD(checked),
945 "wrong bmpchecked %p/%p\n", info.hbmpChecked, checked );
946 ok_(__FILE__, line)( info.hbmpUnchecked == unchecked || (ULONG_PTR)info.hbmpUnchecked == LOWORD(unchecked),
947 "wrong bmpunchecked %p/%p\n", info.hbmpUnchecked, unchecked );
948 }
949 if (mask & MIIM_DATA)
950 ok_(__FILE__, line)( info.dwItemData == data || info.dwItemData == LOWORD(data),
951 "wrong item data %lx/%lx\n", info.dwItemData, data );
952 if (mask & MIIM_BITMAP)
953 ok_(__FILE__, line)( info.hbmpItem == item || (ULONG_PTR)info.hbmpItem == LOWORD(item),
954 "wrong bmpitem %p/%p\n", info.hbmpItem, item );
955 ok_(__FILE__, line)( info.dwTypeData == type_data || (ULONG_PTR)info.dwTypeData == LOWORD(type_data),
956 "wrong type data %p/%p\n", info.dwTypeData, type_data );
957 ok_(__FILE__, line)( info.cch == out_len, "wrong len %x/%x\n", info.cch, out_len );
958 if (expname)
959 {
960 if(ansi)
961 ok_(__FILE__, line)( !strncmp( expname, info.dwTypeData, out_len ),
962 "menu item name differed from '%s' '%s'\n", expname, info.dwTypeData );
963 else
964 ok_(__FILE__, line)( !strncmpW( (WCHAR *)expname, (WCHAR *)info.dwTypeData, out_len ),
965 "menu item name wrong\n" );
966
967 SetLastError( 0xdeadbeef );
968 ret = ansi ? GetMenuStringA( hmenu, 0, (char *)buffer, 80, MF_BYPOSITION ) :
969 GetMenuStringW( hmenu, 0, buffer, 80, MF_BYPOSITION );
970 if (expstring)
971 ok_(__FILE__, line)( ret, "GetMenuString failed, err %u\n", GetLastError());
972 else
973 ok_(__FILE__, line)( !ret, "GetMenuString should have failed\n" );
974 }
975 }
976
977 static void modify_menu( int line, HMENU hmenu, BOOL ansi, UINT flags, UINT_PTR id, void *data )
978 {
979 BOOL ret;
980
981 SetLastError( 0xdeadbeef );
982 if (ansi) ret = ModifyMenuA( hmenu, 0, flags, id, data );
983 else ret = ModifyMenuW( hmenu, 0, flags, id, data );
984 ok_(__FILE__,line)( ret, "ModifyMenuA failed, err %u\n", GetLastError());
985 }
986
987 static void set_menu_item_info( int line, HMENU hmenu, BOOL ansi, UINT mask, UINT type, UINT state,
988 UINT id, HMENU submenu, HBITMAP checked, HBITMAP unchecked, ULONG_PTR data,
989 void *type_data, UINT len, HBITMAP item )
990
991 {
992 MENUITEMINFOA info;
993 BOOL ret;
994
995 /* magic bitmap handle to test smaller cbSize */
996 if (item == (HBITMAP)(ULONG_PTR)0xdeadbeef)
997 info.cbSize = FIELD_OFFSET(MENUITEMINFOA,hbmpItem);
998 else
999 info.cbSize = sizeof(info);
1000 info.fMask = mask;
1001 info.fType = type;
1002 info.fState = state;
1003 info.wID = id;
1004 info.hSubMenu = submenu;
1005 info.hbmpChecked = checked;
1006 info.hbmpUnchecked = unchecked;
1007 info.dwItemData = data;
1008 info.dwTypeData = type_data;
1009 info.cch = len;
1010 info.hbmpItem = item;
1011 SetLastError( 0xdeadbeef );
1012 if (ansi) ret = SetMenuItemInfoA( hmenu, 0, TRUE, &info );
1013 else ret = SetMenuItemInfoW( hmenu, 0, TRUE, (MENUITEMINFOW*)&info );
1014 ok_(__FILE__, line)( ret, "SetMenuItemInfo failed, err %u\n", GetLastError());
1015 }
1016
1017 #define TMII_INSMI( c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,eret1 )\
1018 hmenu = CreateMenu();\
1019 submenu = CreateMenu();\
1020 if(ansi)strcpy( string, init );\
1021 else strcpyW( string, init );\
1022 insert_menu_item( __LINE__, hmenu, ansi, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, eret1 )
1023
1024 /* GetMenuItemInfo + GetMenuString */
1025 #define TMII_GMII( c2,l2,\
1026 d3,e3,f3,g3,h3,i3,j3,k3,l3,m3,\
1027 expname, eret2, eret3)\
1028 check_menu_item_info( __LINE__, hmenu, ansi, c2, d3, e3, f3, g3, h3, i3, j3, k3, l2, l3, m3, \
1029 expname, eret2, eret3 )
1030
1031 #define TMII_DONE \
1032 RemoveMenu(hmenu, 0, TRUE );\
1033 DestroyMenu( hmenu );\
1034 DestroyMenu( submenu );
1035
1036 /* modify menu */
1037 #define TMII_MODM( flags, id, data ) \
1038 modify_menu( __LINE__, hmenu, ansi, flags, id, data )
1039
1040 /* SetMenuItemInfo */
1041 #define TMII_SMII( c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1 ) \
1042 set_menu_item_info( __LINE__, hmenu, ansi, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1 )
1043
1044
1045 #define OK 1
1046 #define ER 0
1047
1048
1049 static void test_menu_iteminfo( void )
1050 {
1051 int ansi = TRUE;
1052 char txtA[]="wine";
1053 char initA[]="XYZ";
1054 char emptyA[]="";
1055 WCHAR txtW[]={'W','i','n','e',0};
1056 WCHAR initW[]={'X','Y','Z',0};
1057 WCHAR emptyW[]={0};
1058 void *txt, *init, *empty, *string;
1059 HBITMAP hbm = CreateBitmap(1,1,1,1,NULL);
1060 char stringA[0x80];
1061 HMENU hmenu, submenu=CreateMenu();
1062 HBITMAP dummy_hbm = (HBITMAP)(ULONG_PTR)0xdeadbeef;
1063
1064 do {
1065 if( ansi) {txt=txtA;init=initA;empty=emptyA;string=stringA;}
1066 else {txt=txtW;init=initW;empty=emptyW;string=stringA;}
1067 trace( "%s string %p hbm %p txt %p\n", ansi ? "ANSI tests: " : "Unicode tests:", string, hbm, txt);
1068 /* test all combinations of MFT_STRING, MFT_OWNERDRAW and MFT_BITMAP */
1069 /* (since MFT_STRING is zero, there are four of them) */
1070 TMII_INSMI( MIIM_TYPE, MFT_STRING, 0, 0, 0, 0, 0, 0, txt, 0, 0, OK );
1071 TMII_GMII ( MIIM_TYPE, 80,
1072 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1073 txt, OK, OK );
1074 TMII_DONE
1075 TMII_INSMI( MIIM_TYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1076 TMII_GMII ( MIIM_TYPE, 80,
1077 MFT_STRING|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1078 NULL, OK, ER );
1079 TMII_DONE
1080 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, 6, 0, OK );
1081 TMII_GMII ( MIIM_TYPE, 80,
1082 MFT_BITMAP, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1083 NULL, OK, ER );
1084 TMII_DONE
1085 TMII_INSMI( MIIM_TYPE, MFT_BITMAP|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, hbm, 6, 0, OK );
1086 TMII_GMII ( MIIM_TYPE, 80,
1087 MFT_BITMAP|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1088 NULL, OK, ER );
1089 TMII_DONE
1090 /* not enough space for name*/
1091 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1092 TMII_GMII ( MIIM_TYPE, 0,
1093 MFT_STRING, 0, 0, 0, 0, 0, 0, NULL, 4, 0,
1094 NULL, OK, OK );
1095 TMII_DONE
1096 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1097 TMII_GMII ( MIIM_TYPE, 5,
1098 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1099 txt, OK, OK );
1100 TMII_DONE
1101 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1102 TMII_GMII ( MIIM_TYPE, 4,
1103 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 3, 0,
1104 txt, OK, OK );
1105 TMII_DONE
1106 TMII_INSMI( MIIM_FTYPE|MIIM_STRING, MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1107 TMII_GMII ( MIIM_TYPE, 0,
1108 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 0, 0,
1109 NULL, OK, ER );
1110 TMII_DONE
1111 /* cannot combine MIIM_TYPE with some other flags */
1112 TMII_INSMI( MIIM_TYPE|MIIM_STRING, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, ER );
1113 TMII_DONE
1114 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1115 TMII_GMII ( MIIM_TYPE|MIIM_STRING, 80,
1116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1117 NULL, ER, OK );
1118 TMII_DONE
1119 TMII_INSMI( MIIM_TYPE|MIIM_FTYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, ER );
1120 TMII_DONE
1121 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1122 TMII_GMII ( MIIM_TYPE|MIIM_FTYPE, 80,
1123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1124 NULL, ER, OK );
1125 TMII_DONE
1126 TMII_INSMI( MIIM_TYPE|MIIM_BITMAP, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, 6, hbm, ER );
1127 TMII_DONE
1128 /* but succeeds with some others */
1129 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1130 TMII_GMII ( MIIM_TYPE|MIIM_SUBMENU, 80,
1131 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1132 txt, OK, OK );
1133 TMII_DONE
1134 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1135 TMII_GMII ( MIIM_TYPE|MIIM_STATE, 80,
1136 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1137 txt, OK, OK );
1138 TMII_DONE
1139 TMII_INSMI( MIIM_TYPE|MIIM_ID, MFT_STRING, -1, 888, 0, 0, 0, -1, txt, 6, 0, OK );
1140 TMII_GMII ( MIIM_TYPE|MIIM_ID, 80,
1141 MFT_STRING, 0, 888, 0, 0, 0, 0, string, 4, 0,
1142 txt, OK, OK );
1143 TMII_DONE
1144 TMII_INSMI( MIIM_TYPE|MIIM_DATA, MFT_STRING, -1, -1, 0, 0, 0, 999, txt, 6, 0, OK );
1145 TMII_GMII ( MIIM_TYPE|MIIM_DATA, 80,
1146 MFT_STRING, 0, 0, 0, 0, 0, 999, string, 4, 0,
1147 txt, OK, OK );
1148 TMII_DONE
1149 /* to be continued */
1150 /* set text with MIIM_TYPE and retrieve with MIIM_STRING */
1151 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1152 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1153 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1154 txt, OK, OK );
1155 TMII_DONE
1156 /* set text with MIIM_TYPE and retrieve with MIIM_STRING; MFT_OWNERDRAW causes an empty string */
1157 TMII_INSMI( MIIM_TYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1158 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1159 MFT_STRING|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 0, 0,
1160 empty, OK, ER );
1161 TMII_DONE
1162 TMII_INSMI( MIIM_TYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1163 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1164 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 0, 0,
1165 empty, OK, ER );
1166 TMII_DONE
1167 TMII_INSMI( MIIM_TYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1168 TMII_GMII ( MIIM_FTYPE, 80,
1169 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 80, 0,
1170 init, OK, ER );
1171 TMII_DONE
1172 TMII_INSMI( MIIM_TYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1173 TMII_GMII ( 0, 80,
1174 0, 0, 0, 0, 0, 0, 0, string, 80, 0,
1175 init, OK, OK );
1176 TMII_DONE
1177 /* contrary to MIIM_TYPE,you can set the text for an owner draw menu */
1178 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1179 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1180 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 4, 0,
1181 txt, OK, OK );
1182 TMII_DONE
1183 /* same but retrieve with MIIM_TYPE */
1184 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1185 TMII_GMII ( MIIM_TYPE, 80,
1186 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1187 NULL, OK, OK );
1188 TMII_DONE
1189 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1190 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1191 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 0, 0,
1192 empty, OK, ER );
1193 TMII_DONE
1194 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1195 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1196 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 0, 0,
1197 empty, OK, ER );
1198 TMII_DONE
1199
1200 /* How is that with bitmaps? */
1201 TMII_INSMI( MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1202 TMII_GMII ( MIIM_TYPE, 80,
1203 MFT_BITMAP, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1204 NULL, OK, ER );
1205 TMII_DONE
1206 TMII_INSMI( MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1207 TMII_GMII ( MIIM_BITMAP|MIIM_FTYPE, 80,
1208 0, 0, 0, 0, 0, 0, 0, string, 80, hbm,
1209 init, OK, ER );
1210 TMII_DONE
1211 /* MIIM_BITMAP does not like MFT_BITMAP */
1212 TMII_INSMI( MIIM_BITMAP|MIIM_FTYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, 0, -1, hbm, ER );
1213 TMII_DONE
1214 /* no problem with OWNERDRAWN */
1215 TMII_INSMI( MIIM_BITMAP|MIIM_FTYPE, MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1216 TMII_GMII ( MIIM_BITMAP|MIIM_FTYPE, 80,
1217 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 80, hbm,
1218 init, OK, ER );
1219 TMII_DONE
1220 /* setting MFT_BITMAP with MFT_FTYPE fails anyway */
1221 TMII_INSMI( MIIM_FTYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, 0, -1, 0, ER );
1222 TMII_DONE
1223
1224 /* menu with submenu */
1225 TMII_INSMI( MIIM_SUBMENU|MIIM_FTYPE, MFT_STRING, -1, -1, submenu, 0, 0, -1, txt, 0, 0, OK );
1226 TMII_GMII ( MIIM_SUBMENU, 80,
1227 0, 0, 0, submenu, 0, 0, 0, string, 80, 0,
1228 init, OK, ER );
1229 TMII_DONE
1230 TMII_INSMI( MIIM_SUBMENU|MIIM_FTYPE, MFT_STRING, -1, -1, submenu, 0, 0, -1, empty, 0, 0, OK );
1231 TMII_GMII ( MIIM_SUBMENU, 80,
1232 0, 0, 0, submenu, 0, 0, 0, string, 80, 0,
1233 init, OK, ER );
1234 TMII_DONE
1235 /* menu with submenu, without MIIM_SUBMENU the submenufield is cleared */
1236 TMII_INSMI( MIIM_SUBMENU|MIIM_FTYPE, MFT_STRING, -1, -1, submenu, 0, 0, -1, txt, 0, 0, OK );
1237 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1238 MFT_STRING|MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 0, 0,
1239 empty, OK, ER );
1240 TMII_GMII ( MIIM_SUBMENU|MIIM_FTYPE, 80,
1241 MFT_SEPARATOR, 0, 0, submenu, 0, 0, 0, string, 80, 0,
1242 empty, OK, ER );
1243 TMII_DONE
1244 /* menu with invalid submenu */
1245 TMII_INSMI( MIIM_SUBMENU|MIIM_FTYPE, MFT_STRING, -1, -1, (HMENU)999, 0, 0, -1, txt, 0, 0, ER );
1246 TMII_DONE
1247 /* Separator */
1248 TMII_INSMI( MIIM_TYPE, MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, txt, 0, 0, OK );
1249 TMII_GMII ( MIIM_TYPE, 80,
1250 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1251 NULL, OK, ER );
1252 TMII_DONE
1253 TMII_INSMI( MIIM_TYPE, MFT_BITMAP|MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, hbm, 6, 0, OK );
1254 TMII_GMII ( MIIM_TYPE, 80,
1255 MFT_BITMAP|MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1256 NULL, OK, ER );
1257 TMII_DONE
1258 /* SEPARATOR and STRING go well together */
1259 /* BITMAP and STRING go well together */
1260 TMII_INSMI( MIIM_STRING|MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1261 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, 80,
1262 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, hbm,
1263 txt, OK, OK );
1264 TMII_DONE
1265 /* BITMAP, SEPARATOR and STRING go well together */
1266 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1267 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, 80,
1268 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 4, hbm,
1269 txt, OK, OK );
1270 TMII_DONE
1271 /* last two tests, but use MIIM_TYPE to retrieve info */
1272 TMII_INSMI( MIIM_FTYPE|MIIM_STRING, MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1273 TMII_GMII ( MIIM_TYPE, 80,
1274 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1275 NULL, OK, OK );
1276 TMII_DONE
1277 TMII_INSMI( MIIM_STRING|MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1278 TMII_GMII ( MIIM_TYPE, 80,
1279 MFT_BITMAP, 0, 0, 0, 0, 0, 0, hbm, 4, hbm,
1280 NULL, OK, OK );
1281 TMII_DONE
1282 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1283 TMII_GMII ( MIIM_TYPE, 80,
1284 MFT_SEPARATOR|MFT_BITMAP, 0, 0, 0, 0, 0, 0, hbm, 4, hbm,
1285 NULL, OK, OK );
1286 TMII_DONE
1287 /* same three with MFT_OWNERDRAW */
1288 TMII_INSMI( MIIM_FTYPE|MIIM_STRING, MFT_SEPARATOR|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, 0, OK );
1289 TMII_GMII ( MIIM_TYPE, 80,
1290 MFT_SEPARATOR|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1291 NULL, OK, OK );
1292 TMII_DONE
1293 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1294 TMII_GMII ( MIIM_TYPE, 80,
1295 MFT_BITMAP|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, hbm, 4, hbm,
1296 NULL, OK, OK );
1297 TMII_DONE
1298 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1299 TMII_GMII ( MIIM_TYPE, 80,
1300 MFT_SEPARATOR|MFT_BITMAP|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, hbm, 4, hbm,
1301 NULL, OK, OK );
1302 TMII_DONE
1303
1304 TMII_INSMI( MIIM_STRING|MIIM_FTYPE|MIIM_ID, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1305 TMII_GMII ( MIIM_TYPE, 80,
1306 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1307 NULL, OK, OK );
1308 TMII_DONE
1309 /* test with modifymenu: string is preserved after setting OWNERDRAW */
1310 TMII_INSMI( MIIM_STRING, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1311 TMII_MODM( MFT_OWNERDRAW, -1, (void*)787 );
1312 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_DATA, 80,
1313 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 787, string, 4, 0,
1314 txt, OK, OK );
1315 TMII_DONE
1316 /* same with bitmap: now the text is cleared */
1317 TMII_INSMI( MIIM_STRING, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1318 TMII_MODM( MFT_BITMAP, 545, hbm );
1319 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_ID, 80,
1320 MFT_BITMAP, 0, 545, 0, 0, 0, 0, string, 0, hbm,
1321 empty, OK, ER );
1322 TMII_DONE
1323 /* start with bitmap: now setting text clears it (though he flag is raised) */
1324 TMII_INSMI( MIIM_BITMAP, MFT_STRING, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1325 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_ID, 80,
1326 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 0, hbm,
1327 empty, OK, ER );
1328 TMII_MODM( MFT_STRING, 545, txt );
1329 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_ID, 80,
1330 MFT_STRING, 0, 545, 0, 0, 0, 0, string, 4, 0,
1331 txt, OK, OK );
1332 TMII_DONE
1333 /*repeat with text NULL */
1334 TMII_INSMI( MIIM_BITMAP, MFT_STRING, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1335 TMII_MODM( MFT_STRING, 545, NULL );
1336 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_ID, 80,
1337 MFT_SEPARATOR, 0, 545, 0, 0, 0, 0, string, 0, 0,
1338 empty, OK, ER );
1339 TMII_DONE
1340 /* repeat with text "" */
1341 TMII_INSMI( MIIM_BITMAP, -1 , -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1342 TMII_MODM( MFT_STRING, 545, empty );
1343 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_ID, 80,
1344 MFT_STRING, 0, 545, 0, 0, 0, 0, string, 0, 0,
1345 empty, OK, ER );
1346 TMII_DONE
1347 /* start with bitmap: set ownerdraw */
1348 TMII_INSMI( MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, 0, -1, hbm, OK );
1349 TMII_MODM( MFT_OWNERDRAW, -1, (void *)232 );
1350 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP|MIIM_DATA, 80,
1351 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 232, string, 0, hbm,
1352 empty, OK, ER );
1353 TMII_DONE
1354 /* ask nothing */
1355 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, txt, 6, hbm, OK );
1356 TMII_GMII ( 0, 80,
1357 0, 0, 0, 0, 0, 0, 0, string, 80, 0,
1358 init, OK, OK );
1359 TMII_DONE
1360 /* some tests with small cbSize: the hbmpItem is to be ignored */
1361 TMII_INSMI( MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, 0, -1, dummy_hbm, OK );
1362 TMII_GMII ( MIIM_TYPE, 80,
1363 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, NULL, 0, NULL,
1364 NULL, OK, ER );
1365 TMII_DONE
1366 TMII_INSMI( MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, 0, -1, dummy_hbm, OK );
1367 TMII_GMII ( MIIM_BITMAP|MIIM_FTYPE, 80,
1368 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 80, NULL,
1369 init, OK, ER );
1370 TMII_DONE
1371 TMII_INSMI( MIIM_STRING|MIIM_BITMAP, -1, -1, -1, 0, 0, 0, -1, txt, 6, dummy_hbm, OK );
1372 TMII_GMII ( MIIM_TYPE, 80,
1373 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, NULL,
1374 txt, OK, OK );
1375 TMII_DONE
1376 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR, -1, -1, 0, 0, 0, -1, txt, 6, dummy_hbm, OK );
1377 TMII_GMII ( MIIM_TYPE, 80,
1378 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1379 NULL, OK, OK );
1380 TMII_DONE
1381 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, dummy_hbm, OK );
1382 TMII_GMII ( MIIM_TYPE, 80,
1383 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1384 NULL, OK, OK );
1385 TMII_DONE
1386 TMII_INSMI( MIIM_FTYPE|MIIM_STRING|MIIM_BITMAP, MFT_SEPARATOR|MFT_OWNERDRAW, -1, -1, 0, 0, 0, -1, txt, 6, dummy_hbm, OK );
1387 TMII_GMII ( MIIM_TYPE, 80,
1388 MFT_SEPARATOR|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 4, NULL,
1389 NULL, OK, OK );
1390 TMII_DONE
1391 /* MIIM_TYPE by itself does not get/set the dwItemData for OwnerDrawn menus */
1392 TMII_INSMI( MIIM_TYPE|MIIM_DATA, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, 343, txt, 0, 0, OK );
1393 TMII_GMII ( MIIM_TYPE|MIIM_DATA, 80,
1394 MFT_STRING|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 343, 0, 0, 0,
1395 NULL, OK, ER );
1396 TMII_DONE
1397 TMII_INSMI( MIIM_TYPE|MIIM_DATA, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, 343, txt, 0, 0, OK );
1398 TMII_GMII ( MIIM_TYPE, 80,
1399 MFT_STRING|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1400 NULL, OK, ER );
1401 TMII_DONE
1402 TMII_INSMI( MIIM_TYPE, MFT_STRING|MFT_OWNERDRAW, -1, -1, 0, 0, 0, 343, txt, 0, 0, OK );
1403 TMII_GMII ( MIIM_TYPE|MIIM_DATA, 80,
1404 MFT_STRING|MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1405 NULL, OK, ER );
1406 TMII_DONE
1407 /* set a string menu to ownerdraw with MIIM_TYPE */
1408 TMII_INSMI( MIIM_TYPE, MFT_STRING, -2, -2, 0, 0, 0, -2, txt, -2, 0, OK );
1409 TMII_SMII ( MIIM_TYPE, MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
1410 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1411 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, string, 4, 0,
1412 txt, OK, OK );
1413 TMII_DONE
1414 /* test with modifymenu add submenu */
1415 TMII_INSMI( MIIM_STRING, MFT_STRING, -1, -1, 0, 0, 0, -1, txt, 0, 0, OK );
1416 TMII_MODM( MF_POPUP, (UINT_PTR)submenu, txt );
1417 TMII_GMII ( MIIM_FTYPE|MIIM_STRING|MIIM_SUBMENU, 80,
1418 MFT_STRING, 0, 0, submenu, 0, 0, 0, string, 4, 0,
1419 txt, OK, OK );
1420 TMII_GMII ( MIIM_TYPE, 80,
1421 MFT_STRING, 0, 0, 0, 0, 0, 0, string, 4, 0,
1422 txt, OK, OK );
1423 TMII_DONE
1424 /* MFT_SEPARATOR bit is kept when the text is added */
1425 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1426 TMII_SMII( MIIM_STRING, 0, 0, 0, 0, 0, 0, 0, txt, 0, 0 );
1427 TMII_GMII ( MIIM_STRING|MIIM_FTYPE, 80,
1428 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 4, 0,
1429 txt, OK, OK );
1430 TMII_DONE
1431 /* MFT_SEPARATOR bit is kept when bitmap is added */
1432 TMII_INSMI( MIIM_STRING|MIIM_FTYPE, MFT_STRING, -1, -1, 0, 0, 0, -1, NULL, 0, 0, OK );
1433 TMII_SMII( MIIM_BITMAP, 0, 0, 0, 0, 0, 0, 0, 0, 0, hbm );
1434 TMII_GMII ( MIIM_BITMAP|MIIM_FTYPE, 80,
1435 MFT_SEPARATOR, 0, 0, 0, 0, 0, 0, string, 80, hbm,
1436 init, OK, ER );
1437 TMII_DONE
1438 /* Bitmaps inserted with MIIM_TYPE and MFT_BITMAP:
1439 Only the low word of the dwTypeData is used.
1440 Use a magic bitmap here (Word 95 uses this to create its MDI menu buttons) */
1441 TMII_INSMI( MIIM_TYPE, MFT_BITMAP | MFT_RIGHTJUSTIFY, -1, -1, 0, 0, 0, -1,
1442 (HMENU)MAKELONG(HBMMENU_MBAR_CLOSE, 0x1234), -1, 0, OK );
1443 TMII_GMII ( MIIM_TYPE, 80,
1444 MFT_BITMAP | MFT_RIGHTJUSTIFY, 0, 0, 0, 0, 0, 0, HBMMENU_MBAR_CLOSE, 0, HBMMENU_MBAR_CLOSE,
1445 NULL, OK, OK );
1446 TMII_DONE
1447 /* Type flags */
1448 TMII_INSMI( MIIM_TYPE, MFT_BITMAP | MFT_MENUBARBREAK | MFT_RADIOCHECK | MFT_RIGHTJUSTIFY | MFT_RIGHTORDER, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1449 TMII_GMII ( MIIM_TYPE, 80,
1450 MFT_BITMAP | MFT_MENUBARBREAK | MFT_RADIOCHECK | MFT_RIGHTJUSTIFY | MFT_RIGHTORDER, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1451 NULL, OK, OK );
1452 TMII_DONE
1453 /* State flags */
1454 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1455 TMII_SMII( MIIM_STATE, -1, MFS_CHECKED | MFS_DEFAULT | MFS_GRAYED | MFS_HILITE, 0, 0, 0, 0, 0, 0, 0, 0 );
1456 TMII_GMII ( MIIM_STATE, 80,
1457 0, MFS_CHECKED | MFS_DEFAULT | MFS_GRAYED | MFS_HILITE, 0, 0, 0, 0, 0, 0, 80, 0,
1458 NULL, OK, OK );
1459 TMII_DONE
1460 /* The style MFT_RADIOCHECK cannot be set with MIIM_CHECKMARKS only */
1461 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1462 TMII_SMII( MIIM_CHECKMARKS, MFT_RADIOCHECK, 0, 0, 0, hbm, hbm, 0, 0, 0, 0 );
1463 TMII_GMII ( MIIM_CHECKMARKS | MIIM_TYPE, 80,
1464 MFT_BITMAP, 0, 0, 0, hbm, hbm, 0, hbm, 0, hbm,
1465 NULL, OK, OK );
1466 TMII_DONE
1467 /* MFT_BITMAP is added automatically by GetMenuItemInfo() for MIIM_TYPE */
1468 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1469 TMII_SMII( MIIM_FTYPE, MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, (HBITMAP)0x1234, 0, 0 );
1470 TMII_GMII ( MIIM_FTYPE, 80,
1471 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 80, 0,
1472 NULL, OK, OK );
1473 TMII_GMII ( MIIM_TYPE, 80,
1474 MFT_BITMAP | MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1475 NULL, OK, OK );
1476 TMII_GMII ( MIIM_FTYPE, 80,
1477 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 80, 0,
1478 NULL, OK, OK );
1479 TMII_SMII( MIIM_BITMAP, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL );
1480 TMII_GMII ( MIIM_TYPE, 80,
1481 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 0, NULL,
1482 NULL, OK, OK );
1483 TMII_DONE
1484 /* Bitmaps inserted with MIIM_TYPE and MFT_BITMAP:
1485 Only the low word of the dwTypeData is used.
1486 Use a magic bitmap here (Word 95 uses this to create its MDI menu buttons) */
1487 TMII_INSMI( MIIM_TYPE, MFT_BITMAP | MFT_RIGHTJUSTIFY, -1, -1, 0, 0, 0, -1,
1488 (HMENU)MAKELONG(HBMMENU_MBAR_CLOSE, 0x1234), -1, 0, OK );
1489 TMII_GMII ( MIIM_TYPE, 80,
1490 MFT_BITMAP | MFT_RIGHTJUSTIFY, 0, 0, 0, 0, 0, 0, HBMMENU_MBAR_CLOSE, 0, HBMMENU_MBAR_CLOSE,
1491 NULL, OK, OK );
1492 TMII_DONE
1493 /* Type flags */
1494 TMII_INSMI( MIIM_TYPE, MFT_BITMAP | MFT_MENUBARBREAK | MFT_RADIOCHECK | MFT_RIGHTJUSTIFY | MFT_RIGHTORDER, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1495 TMII_GMII ( MIIM_TYPE, 80,
1496 MFT_BITMAP | MFT_MENUBARBREAK | MFT_RADIOCHECK | MFT_RIGHTJUSTIFY | MFT_RIGHTORDER, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1497 NULL, OK, OK );
1498 TMII_DONE
1499 /* State flags */
1500 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1501 TMII_SMII( MIIM_STATE, -1, MFS_CHECKED | MFS_DEFAULT | MFS_GRAYED | MFS_HILITE, 0, 0, 0, 0, 0, 0, 0, 0 );
1502 TMII_GMII ( MIIM_STATE, 80,
1503 0, MFS_CHECKED | MFS_DEFAULT | MFS_GRAYED | MFS_HILITE, 0, 0, 0, 0, 0, 0, 80, 0,
1504 NULL, OK, OK );
1505 TMII_DONE
1506 /* The style MFT_RADIOCHECK cannot be set with MIIM_CHECKMARKS only */
1507 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1508 TMII_SMII( MIIM_CHECKMARKS, MFT_RADIOCHECK, 0, 0, 0, hbm, hbm, 0, 0, 0, 0 );
1509 TMII_GMII ( MIIM_CHECKMARKS | MIIM_TYPE, 80,
1510 MFT_BITMAP, 0, 0, 0, hbm, hbm, 0, hbm, 0, hbm,
1511 NULL, OK, OK );
1512 TMII_DONE
1513 /* MFT_BITMAP is added automatically by GetMenuItemInfo() for MIIM_TYPE */
1514 TMII_INSMI( MIIM_TYPE, MFT_BITMAP, -1, -1, 0, 0, 0, -1, hbm, -1, 0, OK );
1515 TMII_SMII( MIIM_FTYPE, MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, (HBITMAP)0x1234, 0, 0 );
1516 TMII_GMII ( MIIM_FTYPE, 80,
1517 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 80, 0,
1518 NULL, OK, OK );
1519 TMII_GMII ( MIIM_TYPE, 80,
1520 MFT_BITMAP | MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, hbm, 0, hbm,
1521 NULL, OK, OK );
1522 TMII_GMII ( MIIM_FTYPE, 80,
1523 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, 0, 80, 0,
1524 NULL, OK, OK );
1525 TMII_SMII( MIIM_BITMAP, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL );
1526 TMII_GMII ( MIIM_TYPE, 80,
1527 MFT_OWNERDRAW, 0, 0, 0, 0, 0, 0, NULL, 0, NULL,
1528 NULL, OK, OK );
1529 TMII_DONE
1530 } while( !(ansi = !ansi) );
1531 DeleteObject( hbm);
1532 }
1533
1534 /*
1535 The following tests try to confirm the algorithm used to return the menu items
1536 when there is a collision between a menu item and a popup menu
1537 */
1538 static void test_menu_search_bycommand( void )
1539 {
1540 HMENU hmenu, hmenuSub, hmenuSub2;
1541 MENUITEMINFO info;
1542 BOOL rc;
1543 UINT id;
1544 char strback[0x80];
1545 char strIn[0x80];
1546 static CHAR menuitem[] = "MenuItem",
1547 menuitem2[] = "MenuItem 2";
1548
1549 /* Case 1: Menu containing a menu item */
1550 hmenu = CreateMenu();
1551
1552 memset( &info, 0, sizeof info );
1553 info.cbSize = sizeof info;
1554 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1555 info.fType = MFT_STRING;
1556 strcpy(strIn, "Case 1 MenuItem");
1557 info.dwTypeData = strIn;
1558 info.wID = (UINT) 0x1234;
1559
1560 rc = InsertMenuItem(hmenu, 0, TRUE, &info );
1561 ok (rc, "Inserting the menuitem failed\n");
1562
1563 id = GetMenuItemID(hmenu, 0);
1564 ok (id == 0x1234, "Getting the menuitem id failed(gave %x)\n", id);
1565
1566 /* Confirm the menuitem was given the id supplied (getting by position) */
1567 memset( &info, 0, sizeof info );
1568 strback[0] = 0x00;
1569 info.cbSize = sizeof(MENUITEMINFO);
1570 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1571 info.dwTypeData = strback;
1572 info.cch = sizeof(strback);
1573
1574 rc = GetMenuItemInfo(hmenu, 0, TRUE, &info); /* Get by position */
1575 ok (rc, "Getting the menu items info failed\n");
1576 ok (info.wID == 0x1234, "IDs differ for the menuitem\n");
1577 ok (!strcmp(info.dwTypeData, "Case 1 MenuItem"), "Returned item has wrong label\n");
1578
1579 /* Search by id - Should return the item */
1580 memset( &info, 0, sizeof info );
1581 strback[0] = 0x00;
1582 info.cbSize = sizeof(MENUITEMINFO);
1583 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1584 info.dwTypeData = strback;
1585 info.cch = sizeof(strback);
1586 rc = GetMenuItemInfo(hmenu, 0x1234, FALSE, &info); /* Get by ID */
1587
1588 ok (rc, "Getting the menu items info failed\n");
1589 ok (info.wID == 0x1234, "IDs differ for the menuitem\n");
1590 ok (!strcmp(info.dwTypeData, "Case 1 MenuItem"), "Returned item has wrong label\n");
1591
1592 DestroyMenu( hmenu );
1593
1594 /* Case 2: Menu containing a popup menu */
1595 hmenu = CreateMenu();
1596 hmenuSub = CreateMenu();
1597
1598 strcpy(strIn, "Case 2 SubMenu");
1599 rc = InsertMenu(hmenu, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)hmenuSub, strIn);
1600 ok (rc, "Inserting the popup menu into the main menu failed\n");
1601
1602 id = GetMenuItemID(hmenu, 0);
1603 ok (id == -1, "Getting the menuitem id unexpectedly worked (gave %x)\n", id);
1604
1605 /* Confirm the menuitem itself was given an id the same as the HMENU, (getting by position) */
1606 memset( &info, 0, sizeof info );
1607 strback[0] = 0x00;
1608 info.cbSize = sizeof(MENUITEMINFO);
1609 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1610 info.dwTypeData = strback;
1611 info.cch = sizeof(strback);
1612 info.wID = 0xdeadbeef;
1613
1614 rc = GetMenuItemInfo(hmenu, 0, TRUE, &info); /* Get by position */
1615 ok (rc, "Getting the menu items info failed\n");
1616 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for the menuitem\n");
1617 ok (!strcmp(info.dwTypeData, "Case 2 SubMenu"), "Returned item has wrong label\n");
1618
1619 /* Search by id - returns the popup menu itself */
1620 memset( &info, 0, sizeof info );
1621 strback[0] = 0x00;
1622 info.cbSize = sizeof(MENUITEMINFO);
1623 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1624 info.dwTypeData = strback;
1625 info.cch = sizeof(strback);
1626 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub, FALSE, &info); /* Get by ID */
1627
1628 ok (rc, "Getting the menu items info failed\n");
1629 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for the popup menu\n");
1630 ok (!strcmp(info.dwTypeData, "Case 2 SubMenu"), "Returned item has wrong label\n");
1631
1632 /*
1633 Now add an item after it with the same id
1634 */
1635 memset( &info, 0, sizeof info );
1636 info.cbSize = sizeof info;
1637 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1638 info.fType = MFT_STRING;
1639 strcpy(strIn, "Case 2 MenuItem 1");
1640 info.dwTypeData = strIn;
1641 info.wID = (UINT_PTR) hmenuSub;
1642 rc = InsertMenuItem(hmenu, -1, TRUE, &info );
1643 ok (rc, "Inserting the menuitem failed\n");
1644
1645 /* Search by id - returns the item which follows the popup menu */
1646 memset( &info, 0, sizeof info );
1647 strback[0] = 0x00;
1648 info.cbSize = sizeof(MENUITEMINFO);
1649 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1650 info.dwTypeData = strback;
1651 info.cch = sizeof(strback);
1652 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub, FALSE, &info); /* Get by ID */
1653
1654 ok (rc, "Getting the menu items info failed\n");
1655 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for the popup menu\n");
1656 ok (!strcmp(info.dwTypeData, "Case 2 MenuItem 1"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1657
1658 /*
1659 Now add an item before the popup (with the same id)
1660 */
1661 memset( &info, 0, sizeof info );
1662 info.cbSize = sizeof info;
1663 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1664 info.fType = MFT_STRING;
1665 strcpy(strIn, "Case 2 MenuItem 2");
1666 info.dwTypeData = strIn;
1667 info.wID = (UINT_PTR) hmenuSub;
1668 rc = InsertMenuItem(hmenu, 0, TRUE, &info );
1669 ok (rc, "Inserting the menuitem failed\n");
1670
1671 /* Search by id - returns the item which precedes the popup menu */
1672 memset( &info, 0, sizeof info );
1673 strback[0] = 0x00;
1674 info.cbSize = sizeof(MENUITEMINFO);
1675 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
1676 info.dwTypeData = strback;
1677 info.cch = sizeof(strback);
1678 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub, FALSE, &info); /* Get by ID */
1679
1680 ok (rc, "Getting the menu items info failed\n");
1681 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for the popup menu\n");
1682 ok (!strcmp(info.dwTypeData, "Case 2 MenuItem 2"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1683
1684 DestroyMenu( hmenu );
1685 DestroyMenu( hmenuSub );
1686
1687 /*
1688 Case 3: Menu containing a popup menu which in turn
1689 contains 2 items with the same id as the popup itself
1690 */
1691
1692 hmenu = CreateMenu();
1693 hmenuSub = CreateMenu();
1694
1695 memset( &info, 0, sizeof info );
1696 info.cbSize = sizeof info;
1697 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1698 info.fType = MFT_STRING;
1699 info.dwTypeData = menuitem;
1700 info.wID = (UINT_PTR) hmenuSub; /* Enforce id collisions with the hmenu of the popup submenu*/
1701
1702 rc = InsertMenu(hmenu, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)hmenuSub, "Submenu");
1703 ok (rc, "Inserting the popup menu into the main menu failed\n");
1704
1705 rc = InsertMenuItem(hmenuSub, 0, TRUE, &info );
1706 ok (rc, "Inserting the sub menu menuitem failed\n");
1707
1708 memset( &info, 0, sizeof info );
1709 info.cbSize = sizeof info;
1710 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1711 info.fType = MFT_STRING;
1712 info.dwTypeData = menuitem2;
1713 info.wID = (UINT_PTR) hmenuSub; /* Enforce id collisions with the hmenu of the popup submenu*/
1714
1715 rc = InsertMenuItem(hmenuSub, 1, TRUE, &info );
1716 ok (rc, "Inserting the sub menu menuitem 2 failed\n");
1717
1718 /* Prove that you can't query the id of a popup directly (By position) */
1719 id = GetMenuItemID(hmenu, 0);
1720 ok (id == -1, "Getting the sub menu id should have failed because its a popup (gave %x)\n", id);
1721
1722 /* Prove getting the item info via ID returns the first item (not the popup or 2nd item)*/
1723 memset( &info, 0, sizeof info );
1724 strback[0] = 0x00;
1725 info.cbSize = sizeof(MENUITEMINFO);
1726 info.fMask = MIIM_STRING | MIIM_ID;
1727 info.dwTypeData = strback;
1728 info.cch = sizeof(strback);
1729
1730 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub, FALSE, &info);
1731 ok (rc, "Getting the menus info failed\n");
1732 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for popup menu\n");
1733 ok (!strcmp(info.dwTypeData, "MenuItem"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1734 DestroyMenu( hmenu );
1735 DestroyMenu( hmenuSub );
1736
1737 /*
1738 Case 4: Menu containing 2 popup menus, the second
1739 contains 2 items with the same id as the first popup menu
1740 */
1741 hmenu = CreateMenu();
1742 hmenuSub = CreateMenu();
1743 hmenuSub2 = CreateMenu();
1744
1745 rc = InsertMenu(hmenu, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)hmenuSub, "Submenu");
1746 ok (rc, "Inserting the popup menu into the main menu failed\n");
1747
1748 rc = InsertMenu(hmenu, 1, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)hmenuSub2, "Submenu2");
1749 ok (rc, "Inserting the popup menu into the main menu failed\n");
1750
1751 memset( &info, 0, sizeof info );
1752 info.cbSize = sizeof info;
1753 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1754 info.fType = MFT_STRING;
1755 info.dwTypeData = menuitem;
1756 info.wID = (UINT_PTR) hmenuSub; /* Enforce id collisions with the hmenu of the popup submenu*/
1757
1758 rc = InsertMenuItem(hmenuSub2, 0, TRUE, &info );
1759 ok (rc, "Inserting the sub menu menuitem failed\n");
1760
1761 memset( &info, 0, sizeof info );
1762 info.cbSize = sizeof info;
1763 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1764 info.fType = MFT_STRING;
1765 info.dwTypeData = menuitem2;
1766 info.wID = (UINT_PTR) hmenuSub; /* Enforce id collisions with the hmenu of the popup submenu*/
1767
1768 rc = InsertMenuItem(hmenuSub2, 1, TRUE, &info );
1769 ok (rc, "Inserting the sub menu menuitem 2 failed\n");
1770
1771 /* Prove getting the item info via ID returns the first item (not the popup or 2nd item)*/
1772 memset( &info, 0, sizeof info );
1773 strback[0] = 0x00;
1774 info.cbSize = sizeof(MENUITEMINFO);
1775 info.fMask = MIIM_STRING | MIIM_ID;
1776 info.dwTypeData = strback;
1777 info.cch = sizeof(strback);
1778
1779 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub, FALSE, &info);
1780 ok (rc, "Getting the menus info failed\n");
1781 ok (info.wID == (UINT_PTR)hmenuSub, "IDs differ for popup menu\n");
1782 ok (!strcmp(info.dwTypeData, "MenuItem"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1783
1784 memset( &info, 0, sizeof info );
1785 strback[0] = 0x00;
1786 info.cbSize = sizeof(MENUITEMINFO);
1787 info.fMask = MIIM_STRING | MIIM_ID;
1788 info.dwTypeData = strback;
1789 info.cch = sizeof(strback);
1790
1791 rc = GetMenuItemInfo(hmenu, (UINT_PTR)hmenuSub2, FALSE, &info);
1792 ok (rc, "Getting the menus info failed\n");
1793 ok (info.wID == (UINT_PTR)hmenuSub2, "IDs differ for popup menu\n");
1794 ok (!strcmp(info.dwTypeData, "Submenu2"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1795
1796 DestroyMenu( hmenu );
1797 DestroyMenu( hmenuSub );
1798 DestroyMenu( hmenuSub2 );
1799
1800
1801 /*
1802 Case 5: Menu containing a popup menu which in turn
1803 contains an item with a different id than the popup menu.
1804 This tests the fallback to a popup menu ID.
1805 */
1806
1807 hmenu = CreateMenu();
1808 hmenuSub = CreateMenu();
1809
1810 rc = AppendMenu(hmenu, MF_POPUP | MF_STRING, (UINT_PTR)hmenuSub, "Submenu");
1811 ok (rc, "Appending the popup menu to the main menu failed\n");
1812
1813 rc = AppendMenu(hmenuSub, MF_STRING, 102, "Item");
1814 ok (rc, "Appending the item to the popup menu failed\n");
1815
1816 /* Set the ID for hmenuSub */
1817 info.cbSize = sizeof(info);
1818 info.fMask = MIIM_ID;
1819 info.wID = 101;
1820
1821 rc = SetMenuItemInfo(hmenu, 0, TRUE, &info);
1822 ok(rc, "Setting the ID for the popup menu failed\n");
1823
1824 /* Check if the ID has been set */
1825 info.wID = 0;
1826 rc = GetMenuItemInfo(hmenu, 0, TRUE, &info);
1827 ok(rc, "Getting the ID for the popup menu failed\n");
1828 ok(info.wID == 101, "The ID for the popup menu has not been set\n");
1829
1830 /* Prove getting the item info via ID returns the popup menu */
1831 memset( &info, 0, sizeof(info));
1832 strback[0] = 0x00;
1833 info.cbSize = sizeof(MENUITEMINFO);
1834 info.fMask = MIIM_STRING | MIIM_ID;
1835 info.dwTypeData = strback;
1836 info.cch = sizeof(strback);
1837
1838 rc = GetMenuItemInfo(hmenu, 101, FALSE, &info);
1839 ok (rc, "Getting the menu info failed\n");
1840 ok (info.wID == 101, "IDs differ\n");
1841 ok (!strcmp(info.dwTypeData, "Submenu"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1842
1843 /* Also look for the menu item */
1844 memset( &info, 0, sizeof(info));
1845 strback[0] = 0x00;
1846 info.cbSize = sizeof(MENUITEMINFO);
1847 info.fMask = MIIM_STRING | MIIM_ID;
1848 info.dwTypeData = strback;
1849 info.cch = sizeof(strback);
1850
1851 rc = GetMenuItemInfo(hmenu, 102, FALSE, &info);
1852 ok (rc, "Getting the menu info failed\n");
1853 ok (info.wID == 102, "IDs differ\n");
1854 ok (!strcmp(info.dwTypeData, "Item"), "Returned item has wrong label (%s)\n", info.dwTypeData);
1855
1856 DestroyMenu(hmenu);
1857 DestroyMenu(hmenuSub);
1858 }
1859
1860 struct menu_item_pair_s {
1861 UINT uMenu; /* 1 - top level menu, [0-Menu 1-Enabled 2-Disabled]
1862 * 2 - 2nd level menu, [0-Popup 1-Enabled 2-Disabled]
1863 * 3 - 3rd level menu, [0-Enabled 1-Disabled] */
1864 UINT uItem;
1865 };
1866
1867 static struct menu_mouse_tests_s {
1868 DWORD type;
1869 struct menu_item_pair_s menu_item_pairs[5]; /* for mousing */
1870 WORD wVk[5]; /* keys */
1871 BOOL bMenuVisible;
1872 BOOL _todo_wine;
1873 } menu_tests[] = {
1874 /* for each test, send keys or clicks and check for menu visibility */
1875 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 0}, TRUE, FALSE }, /* test 0 */
1876 { INPUT_KEYBOARD, {{0}}, {VK_ESCAPE, 0}, FALSE, FALSE },
1877 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 0}, TRUE, FALSE },
1878 { INPUT_KEYBOARD, {{0}}, {'D', 0}, FALSE, FALSE },
1879 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 0}, TRUE, FALSE },
1880 { INPUT_KEYBOARD, {{0}}, {'E', 0}, FALSE, FALSE },
1881 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', 0}, TRUE, FALSE },
1882 { INPUT_KEYBOARD, {{0}}, {VK_ESCAPE, VK_ESCAPE, 0}, FALSE, FALSE },
1883 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', VK_ESCAPE, 0}, TRUE, FALSE },
1884 { INPUT_KEYBOARD, {{0}}, {VK_ESCAPE, 0}, FALSE, FALSE },
1885 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', 0}, TRUE, FALSE },
1886 { INPUT_KEYBOARD, {{0}}, {'D', 0}, FALSE, FALSE },
1887 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', 0}, TRUE, FALSE },
1888 { INPUT_KEYBOARD, {{0}}, {'E', 0}, FALSE, FALSE },
1889 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', 'P', 0}, TRUE, FALSE },
1890 { INPUT_KEYBOARD, {{0}}, {'D', 0}, FALSE, FALSE },
1891 { INPUT_KEYBOARD, {{0}}, {VK_MENU, 'M', 'P', 0}, TRUE, FALSE },
1892 { INPUT_KEYBOARD, {{0}}, {'E', 0}, FALSE, FALSE },
1893 { INPUT_KEYBOARD, {{0}}, {VK_F10, 0}, TRUE, FALSE },
1894 { INPUT_KEYBOARD, {{0}}, {VK_F10, 0}, FALSE, FALSE },
1895
1896 { INPUT_MOUSE, {{1, 2}, {0}}, {0}, TRUE, TRUE }, /* test 20 */
1897 { INPUT_MOUSE, {{1, 1}, {0}}, {0}, FALSE, FALSE },
1898 { INPUT_MOUSE, {{1, 0}, {0}}, {0}, TRUE, TRUE },
1899 { INPUT_MOUSE, {{1, 1}, {0}}, {0}, FALSE, FALSE },
1900 { INPUT_MOUSE, {{1, 0}, {2, 2}, {0}}, {0}, TRUE, TRUE },
1901 { INPUT_MOUSE, {{2, 1}, {0}}, {0}, FALSE, FALSE },
1902 { INPUT_MOUSE, {{1, 0}, {2, 0}, {0}}, {0}, TRUE, TRUE },
1903 { INPUT_MOUSE, {{3, 0}, {0}}, {0}, FALSE, FALSE },
1904 { INPUT_MOUSE, {{1, 0}, {2, 0}, {0}}, {0}, TRUE, TRUE },
1905 { INPUT_MOUSE, {{3, 1}, {0}}, {0}, TRUE, TRUE },
1906 { INPUT_MOUSE, {{1, 1}, {0}}, {0}, FALSE, FALSE },
1907 { -1 }
1908 };
1909
1910 static void send_key(WORD wVk)
1911 {
1912 TEST_INPUT i[2];
1913 memset(i, 0, sizeof(i));
1914 i[0].type = i[1].type = INPUT_KEYBOARD;
1915 i[0].u.ki.wVk = i[1].u.ki.wVk = wVk;
1916 i[1].u.ki.dwFlags = KEYEVENTF_KEYUP;
1917 pSendInput(2, (INPUT *) i, sizeof(INPUT));
1918 }
1919
1920 static BOOL click_menu(HANDLE hWnd, struct menu_item_pair_s *mi)
1921 {
1922 HMENU hMenu = hMenus[mi->uMenu];
1923 TEST_INPUT i[3];
1924 MSG msg;
1925 RECT r;
1926 int screen_w = GetSystemMetrics(SM_CXSCREEN);
1927 int screen_h = GetSystemMetrics(SM_CYSCREEN);
1928 BOOL ret = GetMenuItemRect(mi->uMenu > 2 ? NULL : hWnd, hMenu, mi->uItem, &r);
1929 if(!ret) return FALSE;
1930
1931 memset(i, 0, sizeof(i));
1932 i[0].type = i[1].type = i[2].type = INPUT_MOUSE;
1933 i[0].u.mi.dx = i[1].u.mi.dx = i[2].u.mi.dx
1934 = ((r.left + 5) * 65535) / screen_w;
1935 i[0].u.mi.dy = i[1].u.mi.dy = i[2].u.mi.dy
1936 = ((r.top + 5) * 65535) / screen_h;
1937 i[0].u.mi.dwFlags = i[1].u.mi.dwFlags = i[2].u.mi.dwFlags
1938 = MOUSEEVENTF_ABSOLUTE;
1939 i[0].u.mi.dwFlags |= MOUSEEVENTF_MOVE;
1940 i[1].u.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
1941 i[2].u.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
1942 ret = pSendInput(3, (INPUT *) i, sizeof(INPUT));
1943
1944 /* hack to prevent mouse message buildup in Wine */
1945 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
1946 return ret;
1947 }
1948
1949 static DWORD WINAPI test_menu_input_thread(LPVOID lpParameter)
1950 {
1951 int i, j;
1952 HANDLE hWnd = lpParameter;
1953
1954 Sleep(500);
1955 /* mixed keyboard/mouse test */
1956 for (i = 0; menu_tests[i].type != -1; i++)
1957 {
1958 int ret = TRUE, elapsed = 0;
1959
1960 got_input = i && menu_tests[i-1].bMenuVisible;
1961
1962 if (menu_tests[i].type == INPUT_KEYBOARD)
1963 for (j = 0; menu_tests[i].wVk[j] != 0; j++)
1964 send_key(menu_tests[i].wVk[j]);
1965 else
1966 for (j = 0; menu_tests[i].menu_item_pairs[j].uMenu != 0; j++)
1967 if (!(ret = click_menu(hWnd, &menu_tests[i].menu_item_pairs[j]))) break;
1968
1969 if (!ret)
1970 {
1971 skip( "test %u: failed to send input\n", i );
1972 PostMessage( hWnd, WM_CANCELMODE, 0, 0 );
1973 return 0;
1974 }
1975 while (menu_tests[i].bMenuVisible != bMenuVisible)
1976 {
1977 if (elapsed > 200)
1978 break;
1979 elapsed += 20;
1980 Sleep(20);
1981 }
1982
1983 if (!got_input)
1984 {
1985 skip( "test %u: didn't receive input\n", i );
1986 PostMessage( hWnd, WM_CANCELMODE, 0, 0 );
1987 return 0;
1988 }
1989
1990 if (menu_tests[i]._todo_wine)
1991 {
1992 todo_wine {
1993 ok(menu_tests[i].bMenuVisible == bMenuVisible, "test %d\n", i);
1994 }
1995 }
1996 else
1997 ok(menu_tests[i].bMenuVisible == bMenuVisible, "test %d\n", i);
1998 }
1999 return 0;
2000 }
2001
2002 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam,
2003 LPARAM lParam)
2004 {
2005 switch (msg) {
2006 case WM_ENTERMENULOOP:
2007 bMenuVisible = TRUE;
2008 break;
2009 case WM_EXITMENULOOP:
2010 bMenuVisible = FALSE;
2011 break;
2012 case WM_KEYDOWN:
2013 case WM_SYSKEYDOWN:
2014 case WM_MOUSEMOVE:
2015 case WM_LBUTTONDOWN:
2016 case WM_LBUTTONUP:
2017 case WM_NCMOUSEMOVE:
2018 case WM_NCLBUTTONDOWN:
2019 case WM_NCLBUTTONUP:
2020 got_input = TRUE;
2021 /* fall through */
2022 default:
2023 return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
2024 }
2025 return 0;
2026 }
2027
2028 static void test_menu_input(void) {
2029 MSG msg;
2030 WNDCLASSA wclass;
2031 HINSTANCE hInstance = GetModuleHandleA( NULL );
2032 HANDLE hThread, hWnd;
2033 DWORD tid;
2034
2035 wclass.lpszClassName = "MenuTestClass";
2036 wclass.style = CS_HREDRAW | CS_VREDRAW;
2037 wclass.lpfnWndProc = WndProc;
2038 wclass.hInstance = hInstance;
2039 wclass.hIcon = LoadIconA( 0, IDI_APPLICATION );
2040 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW );
2041 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
2042 wclass.lpszMenuName = 0;
2043 wclass.cbClsExtra = 0;
2044 wclass.cbWndExtra = 0;
2045 assert (RegisterClassA( &wclass ));
2046 assert (hWnd = CreateWindowA( wclass.lpszClassName, "MenuTest",
2047 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
2048 400, 200, NULL, NULL, hInstance, NULL) );
2049
2050 /* fixed menus */
2051 hMenus[3] = CreatePopupMenu();
2052 AppendMenu(hMenus[3], MF_STRING, 0, "&Enabled");
2053 AppendMenu(hMenus[3], MF_STRING|MF_DISABLED, 0, "&Disabled");
2054
2055 hMenus[2] = CreatePopupMenu();
2056 AppendMenu(hMenus[2], MF_STRING|MF_POPUP, (UINT_PTR) hMenus[3], "&Popup");
2057 AppendMenu(hMenus[2], MF_STRING, 0, "&Enabled");
2058 AppendMenu(hMenus[2], MF_STRING|MF_DISABLED, 0, "&Disabled");
2059
2060 hMenus[1] = CreateMenu();
2061 AppendMenu(hMenus[1], MF_STRING|MF_POPUP, (UINT_PTR) hMenus[2], "&Menu");
2062 AppendMenu(hMenus[1], MF_STRING, 0, "&Enabled");
2063 AppendMenu(hMenus[1], MF_STRING|MF_DISABLED, 0, "&Disabled");
2064
2065 SetMenu(hWnd, hMenus[1]);
2066 ShowWindow(hWnd, SW_SHOW);
2067 UpdateWindow(hWnd);
2068
2069 hThread = CreateThread(NULL, 0, test_menu_input_thread, hWnd, 0, &tid);
2070 while(1)
2071 {
2072 if (WAIT_TIMEOUT != WaitForSingleObject(hThread, 50))
2073 break;
2074 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2075 }
2076 DestroyWindow(hWnd);
2077 }
2078
2079 static void test_menu_flags( void )
2080 {
2081 HMENU hMenu, hPopupMenu;
2082
2083 hMenu = CreateMenu();
2084 hPopupMenu = CreatePopupMenu();
2085
2086 AppendMenu(hMenu, MF_POPUP | MF_STRING, (UINT_PTR)hPopupMenu, "Popup");
2087
2088 AppendMenu(hPopupMenu, MF_STRING | MF_HILITE | MF_DEFAULT, 101, "Item 1");
2089 InsertMenu(hPopupMenu, 1, MF_BYPOSITION | MF_STRING | MF_HILITE | MF_DEFAULT, 102, "Item 2");
2090 AppendMenu(hPopupMenu, MF_STRING, 103, "Item 3");
2091 ModifyMenu(hPopupMenu, 2, MF_BYPOSITION | MF_STRING | MF_HILITE | MF_DEFAULT, 103, "Item 3");
2092
2093 ok(GetMenuState(hPopupMenu, 0, MF_BYPOSITION) & MF_HILITE,
2094 "AppendMenu should accept MF_HILITE\n");
2095 ok(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE,
2096 "InsertMenu should accept MF_HILITE\n");
2097 ok(GetMenuState(hPopupMenu, 2, MF_BYPOSITION) & MF_HILITE,
2098 "ModifyMenu should accept MF_HILITE\n");
2099
2100 ok(!(GetMenuState(hPopupMenu, 0, MF_BYPOSITION) & MF_DEFAULT),
2101 "AppendMenu must not accept MF_DEFAULT\n");
2102 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_DEFAULT),
2103 "InsertMenu must not accept MF_DEFAULT\n");
2104 ok(!(GetMenuState(hPopupMenu, 2, MF_BYPOSITION) & MF_DEFAULT),
2105 "ModifyMenu must not accept MF_DEFAULT\n");
2106
2107 DestroyMenu(hMenu);
2108 }
2109
2110 static void test_menu_hilitemenuitem( void )
2111 {
2112 HMENU hMenu, hPopupMenu;
2113 WNDCLASSA wclass;
2114 HWND hWnd;
2115
2116 wclass.lpszClassName = "HiliteMenuTestClass";
2117 wclass.style = CS_HREDRAW | CS_VREDRAW;
2118 wclass.lpfnWndProc = WndProc;
2119 wclass.hInstance = GetModuleHandleA( NULL );
2120 wclass.hIcon = LoadIconA( 0, IDI_APPLICATION );
2121 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW );
2122 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
2123 wclass.lpszMenuName = 0;
2124 wclass.cbClsExtra = 0;
2125 wclass.cbWndExtra = 0;
2126 assert (RegisterClassA( &wclass ));
2127 assert (hWnd = CreateWindowA( wclass.lpszClassName, "HiliteMenuTest",
2128 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
2129 400, 200, NULL, NULL, wclass.hInstance, NULL) );
2130
2131 hMenu = CreateMenu();
2132 hPopupMenu = CreatePopupMenu();
2133
2134 AppendMenu(hMenu, MF_POPUP | MF_STRING, (UINT_PTR)hPopupMenu, "Popup");
2135
2136 AppendMenu(hPopupMenu, MF_STRING, 101, "Item 1");
2137 AppendMenu(hPopupMenu, MF_STRING, 102, "Item 2");
2138 AppendMenu(hPopupMenu, MF_STRING, 103, "Item 3");
2139
2140 SetMenu(hWnd, hMenu);
2141
2142 /* test invalid arguments */
2143
2144 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE),
2145 "HiliteMenuItem: Item 2 is hilited\n");
2146
2147 SetLastError(0xdeadbeef);
2148 todo_wine
2149 {
2150 ok(!HiliteMenuItem(NULL, hPopupMenu, 1, MF_HILITE | MF_BYPOSITION),
2151 "HiliteMenuItem: call should have failed.\n");
2152 }
2153 ok(GetLastError() == 0xdeadbeef || /* 9x */
2154 GetLastError() == ERROR_INVALID_WINDOW_HANDLE /* NT */,
2155 "HiliteMenuItem: expected error ERROR_INVALID_WINDOW_HANDLE, got: %d\n", GetLastError());
2156
2157 SetLastError(0xdeadbeef);
2158 ok(!HiliteMenuItem(hWnd, NULL, 1, MF_HILITE | MF_BYPOSITION),
2159 "HiliteMenuItem: call should have failed.\n");
2160 ok(GetLastError() == 0xdeadbeef || /* 9x */
2161 GetLastError() == ERROR_INVALID_MENU_HANDLE /* NT */,
2162 "HiliteMenuItem: expected error ERROR_INVALID_MENU_HANDLE, got: %d\n", GetLastError());
2163
2164 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE),
2165 "HiliteMenuItem: Item 2 is hilited\n");
2166
2167 /* either MF_HILITE or MF_UNHILITE *and* MF_BYCOMMAND or MF_BYPOSITION need to be set */
2168
2169 SetLastError(0xdeadbeef);
2170 ok(HiliteMenuItem(hWnd, hPopupMenu, 1, MF_BYPOSITION),
2171 "HiliteMenuItem: call should have succeeded.\n");
2172 ok(GetLastError() == 0xdeadbeef,
2173 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2174
2175 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE),
2176 "HiliteMenuItem: Item 2 is hilited\n");
2177
2178 SetLastError(0xdeadbeef);
2179 todo_wine
2180 {
2181 ok(HiliteMenuItem(hWnd, hPopupMenu, 1, MF_HILITE),
2182 "HiliteMenuItem: call should have succeeded.\n");
2183 }
2184 ok(GetLastError() == 0xdeadbeef,
2185 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2186
2187 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE),
2188 "HiliteMenuItem: Item 2 is hilited\n");
2189
2190 /* hilite a menu item (by position) */
2191
2192 SetLastError(0xdeadbeef);
2193 ok(HiliteMenuItem(hWnd, hPopupMenu, 1, MF_HILITE | MF_BYPOSITION),
2194 "HiliteMenuItem: call should not have failed.\n");
2195 ok(GetLastError() == 0xdeadbeef,
2196 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2197
2198 todo_wine
2199 {
2200 ok(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE,
2201 "HiliteMenuItem: Item 2 is not hilited\n");
2202 }
2203
2204 /* unhilite a menu item (by position) */
2205
2206 SetLastError(0xdeadbeef);
2207 ok(HiliteMenuItem(hWnd, hPopupMenu, 1, MF_UNHILITE | MF_BYPOSITION),
2208 "HiliteMenuItem: call should not have failed.\n");
2209 ok(GetLastError() == 0xdeadbeef,
2210 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2211
2212 ok(!(GetMenuState(hPopupMenu, 1, MF_BYPOSITION) & MF_HILITE),
2213 "HiliteMenuItem: Item 2 is hilited\n");
2214
2215 /* hilite a menu item (by command) */
2216
2217 SetLastError(0xdeadbeef);
2218 ok(HiliteMenuItem(hWnd, hPopupMenu, 103, MF_HILITE | MF_BYCOMMAND),
2219 "HiliteMenuItem: call should not have failed.\n");
2220 ok(GetLastError() == 0xdeadbeef,
2221 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2222
2223 todo_wine
2224 {
2225 ok(GetMenuState(hPopupMenu, 2, MF_BYPOSITION) & MF_HILITE,
2226 "HiliteMenuItem: Item 3 is not hilited\n");
2227 }
2228
2229 /* unhilite a menu item (by command) */
2230
2231 SetLastError(0xdeadbeef);
2232 ok(HiliteMenuItem(hWnd, hPopupMenu, 103, MF_UNHILITE | MF_BYCOMMAND),
2233 "HiliteMenuItem: call should not have failed.\n");
2234 ok(GetLastError() == 0xdeadbeef,
2235 "HiliteMenuItem: expected error 0xdeadbeef, got: %d\n", GetLastError());
2236
2237 ok(!(GetMenuState(hPopupMenu, 2, MF_BYPOSITION) & MF_HILITE),
2238 "HiliteMenuItem: Item 3 is hilited\n");
2239
2240 DestroyWindow(hWnd);
2241 }
2242
2243 static void check_menu_items(HMENU hmenu, UINT checked_cmd, UINT checked_type,
2244 UINT checked_state)
2245 {
2246 INT i, count;
2247
2248 count = GetMenuItemCount(hmenu);
2249 ok (count != -1, "GetMenuItemCount returned -1\n");
2250
2251 for (i = 0; i < count; i++)
2252 {
2253 BOOL ret;
2254 MENUITEMINFO mii;
2255
2256 memset(&mii, 0, sizeof(mii));
2257 mii.cbSize = sizeof(mii);
2258 mii.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_SUBMENU;
2259 ret = GetMenuItemInfo(hmenu, i, TRUE, &mii);
2260 ok(ret, "GetMenuItemInfo(%u) failed\n", i);
2261 #if 0
2262 trace("item #%u: fType %04x, fState %04x, wID %u, hSubMenu %p\n",
2263 i, mii.fType, mii.fState, mii.wID, mii.hSubMenu);
2264 #endif
2265 if (mii.hSubMenu)
2266 {
2267 ok(mii.wID == (UINT_PTR)mii.hSubMenu, "id %u: wID should be equal to hSubMenu\n", checked_cmd);
2268 check_menu_items(mii.hSubMenu, checked_cmd, checked_type, checked_state);
2269 }
2270 else
2271 {
2272 if (mii.wID == checked_cmd)
2273 {
2274 ok(mii.fType == checked_type, "id %u: expected fType %04x, got %04x\n", checked_cmd, checked_type, mii.fType);
2275 ok(mii.fState == checked_state, "id %u: expected fState %04x, got %04x\n", checked_cmd, checked_state, mii.fState);
2276 ok(mii.wID != 0, "id %u: not expected wID 0\n", checked_cmd);
2277 }
2278 else
2279 {
2280 ok(mii.fType != MFT_RADIOCHECK, "id %u: not expected fType MFT_RADIOCHECK on cmd %u\n", checked_cmd, mii.wID);
2281
2282 if (mii.fType == MFT_SEPARATOR)
2283 {
2284 ok(mii.fState == MFS_GRAYED, "id %u: expected fState MFS_GRAYED, got %04x\n", checked_cmd, mii.fState);
2285 ok(mii.wID == 0, "id %u: expected wID 0, got %u\n", checked_cmd, mii.wID);
2286 }
2287 else
2288 {
2289 ok(mii.fState == 0, "id %u: expected fState 0, got %04x\n", checked_cmd, mii.fState);
2290 ok(mii.wID != 0, "id %u: not expected wID 0\n", checked_cmd);
2291 }
2292 }
2293 }
2294 }
2295 }
2296
2297 static void clear_ftype_and_state(HMENU hmenu, UINT id, UINT flags)
2298 {
2299 BOOL ret;
2300 MENUITEMINFO mii;
2301
2302 memset(&mii, 0, sizeof(mii));
2303 mii.cbSize = sizeof(mii);
2304 mii.fMask = MIIM_FTYPE | MIIM_STATE;
2305 ret = SetMenuItemInfo(hmenu, id, (flags & MF_BYPOSITION) != 0, &mii);
2306 ok(ret, "SetMenuItemInfo(%u) failed\n", id);
2307 }
2308
2309 static void test_CheckMenuRadioItem(void)
2310 {
2311 BOOL ret;
2312 HMENU hmenu;
2313
2314 hmenu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(1));
2315 assert(hmenu != 0);
2316
2317 check_menu_items(hmenu, -1, 0, 0);
2318
2319 ret = CheckMenuRadioItem(hmenu, 100, 100, 100, MF_BYCOMMAND);
2320 ok(ret, "CheckMenuRadioItem failed\n");
2321 check_menu_items(hmenu, 100, MFT_RADIOCHECK, MFS_CHECKED);
2322
2323 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
2324 ret = CheckMenuRadioItem(hmenu, 100, 100, -1, MF_BYCOMMAND);
2325 ok(!ret, "CheckMenuRadioItem should return FALSE\n");
2326 check_menu_items(hmenu, 100, MFT_RADIOCHECK, 0);
2327
2328 /* clear check */
2329 clear_ftype_and_state(hmenu, 100, MF_BYCOMMAND);
2330 check_menu_items(hmenu, -1, 0, 0);
2331
2332 /* first and checked items are on different menus */
2333 ret = CheckMenuRadioItem(hmenu, 0, 300, 202, MF_BYCOMMAND);
2334 ok(!ret, "CheckMenuRadioItem should return FALSE\n");
2335 check_menu_items(hmenu, -1, 0, 0);
2336
2337 ret = CheckMenuRadioItem(hmenu, 200, 300, 202, MF_BYCOMMAND);
2338 ok(ret, "CheckMenuRadioItem failed\n");
2339 check_menu_items(hmenu, 202, MFT_RADIOCHECK, MFS_CHECKED);
2340
2341 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
2342 ret = CheckMenuRadioItem(hmenu, 202, 202, -1, MF_BYCOMMAND);
2343 ok(!ret, "CheckMenuRadioItem should return FALSE\n");
2344 check_menu_items(hmenu, 202, MFT_RADIOCHECK, 0);
2345
2346 /* clear check */
2347 clear_ftype_and_state(hmenu, 202, MF_BYCOMMAND);
2348 check_menu_items(hmenu, -1, 0, 0);
2349
2350 /* just for fun, try to check separator */
2351 ret = CheckMenuRadioItem(hmenu, 0, 300, 0, MF_BYCOMMAND);
2352 ok(!ret, "CheckMenuRadioItem should return FALSE\n");
2353 check_menu_items(hmenu, -1, 0, 0);
2354 }
2355
2356 static void test_menu_resource_layout(void)
2357 {
2358 static const struct
2359 {
2360 MENUITEMTEMPLATEHEADER mith;
2361 WORD data[14];
2362 } menu_template =
2363 {
2364 { 0, 0 }, /* versionNumber, offset */
2365 {
2366 /* mtOption, mtID, mtString[] '\0' terminated */
2367 MF_STRING, 1, 'F', 0,
2368 MF_STRING, 2, 0,
2369 MF_SEPARATOR, 3, 0,
2370 /* MF_SEPARATOR, 4, 'S', 0, FIXME: Wine ignores 'S' */
2371 MF_STRING|MF_GRAYED|MF_END, 5, 'E', 0
2372 }
2373 };
2374 static const struct
2375 {
2376 UINT type, state, id;
2377 const char *str;
2378 } menu_data[] =
2379 {
2380 { MF_STRING, MF_ENABLED, 1, "F" },
2381 { MF_SEPARATOR, MF_GRAYED|MF_DISABLED, 2, "" },
2382 { MF_SEPARATOR, MF_GRAYED|MF_DISABLED, 3, "" },
2383 /*{ MF_SEPARATOR, MF_GRAYED|MF_DISABLED, 4, "S" }, FIXME: Wine ignores 'S'*/
2384 { MF_STRING, MF_GRAYED, 5, "E" },
2385 { MF_SEPARATOR, MF_GRAYED|MF_DISABLED, 6, "" },
2386 { MF_STRING, MF_ENABLED, 7, "" },
2387 { MF_SEPARATOR, MF_GRAYED|MF_DISABLED, 8, "" }
2388 };
2389 HMENU hmenu;
2390 INT count, i;
2391 BOOL ret;
2392
2393 hmenu = LoadMenuIndirect(&menu_template);
2394 ok(hmenu != 0, "LoadMenuIndirect error %u\n", GetLastError());
2395
2396 ret = AppendMenu(hmenu, MF_STRING, 6, NULL);
2397 ok(ret, "AppendMenu failed\n");
2398 ret = AppendMenu(hmenu, MF_STRING, 7, "\0");
2399 ok(ret, "AppendMenu failed\n");
2400 ret = AppendMenu(hmenu, MF_SEPARATOR, 8, "separator");
2401 ok(ret, "AppendMenu failed\n");
2402
2403 count = GetMenuItemCount(hmenu);
2404 ok(count == sizeof(menu_data)/sizeof(menu_data[0]),
2405 "expected %u menu items, got %u\n",
2406 (UINT)(sizeof(menu_data)/sizeof(menu_data[0])), count);
2407
2408 for (i = 0; i < count; i++)
2409 {
2410 char buf[20];
2411 MENUITEMINFO mii;
2412
2413 memset(&mii, 0, sizeof(mii));
2414 mii.cbSize = sizeof(mii);
2415 mii.dwTypeData = buf;
2416 mii.cch = sizeof(buf);
2417 mii.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_STRING;
2418 ret = GetMenuItemInfo(hmenu, i, TRUE, &mii);
2419 ok(ret, "GetMenuItemInfo(%u) failed\n", i);
2420 #if 0
2421 trace("item #%u: fType %04x, fState %04x, wID %u, dwTypeData %s\n",
2422 i, mii.fType, mii.fState, mii.wID, (LPCSTR)mii.dwTypeData);
2423 #endif
2424 ok(mii.fType == menu_data[i].type,
2425 "%u: expected fType %04x, got %04x\n", i, menu_data[i].type, mii.fType);
2426 ok(mii.fState == menu_data[i].state,
2427 "%u: expected fState %04x, got %04x\n", i, menu_data[i].state, mii.fState);
2428 ok(mii.wID == menu_data[i].id,
2429 "%u: expected wID %04x, got %04x\n", i, menu_data[i].id, mii.wID);
2430 ok(mii.cch == strlen(menu_data[i].str),
2431 "%u: expected cch %u, got %u\n", i, (UINT)strlen(menu_data[i].str), mii.cch);
2432 ok(!strcmp(mii.dwTypeData, menu_data[i].str),
2433 "%u: expected dwTypeData %s, got %s\n", i, menu_data[i].str, (LPCSTR)mii.dwTypeData);
2434 }
2435
2436 DestroyMenu(hmenu);
2437 }
2438
2439 struct menu_data
2440 {
2441 UINT type, id;
2442 const char *str;
2443 };
2444
2445 static HMENU create_menu_from_data(const struct menu_data *item, INT item_count)
2446 {
2447 HMENU hmenu;
2448 INT i;
2449 BOOL ret;
2450
2451 hmenu = CreateMenu();
2452 assert(hmenu != 0);
2453
2454 for (i = 0; i < item_count; i++)
2455 {
2456 SetLastError(0xdeadbeef);
2457 ret = AppendMenu(hmenu, item[i].type, item[i].id, item[i].str);
2458 ok(ret, "%d: AppendMenu(%04x, %04x, %p) error %u\n",
2459 i, item[i].type, item[i].id, item[i].str, GetLastError());
2460 }
2461 return hmenu;
2462 }
2463
2464 /* use InsertMenuItem: does not set the MFT_BITMAP flag,
2465 * and does not accept non-magic bitmaps with invalid
2466 * bitmap handles */
2467 static HMENU create_menuitem_from_data(const struct menu_data *item, INT item_count)
2468 {
2469 HMENU hmenu;
2470 INT i;
2471 BOOL ret;
2472 MENUITEMINFO mii = { sizeof( MENUITEMINFO)};
2473
2474 hmenu = CreateMenu();
2475 assert(hmenu != 0);
2476
2477 for (i = 0; i < item_count; i++)
2478 {
2479 SetLastError(0xdeadbeef);
2480
2481 mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STATE;
2482 mii.fType = 0;
2483 if( item[i].type & MFT_BITMAP)
2484 {
2485 mii.fMask |= MIIM_BITMAP;
2486 mii.hbmpItem = (HBITMAP)item[i].str;
2487 }
2488 else if( item[i].type & MFT_SEPARATOR)
2489 mii.fType = MFT_SEPARATOR;
2490 else
2491 {
2492 mii.fMask |= MIIM_STRING;
2493 mii.dwTypeData = (LPSTR)item[i].str;
2494 mii.cch = strlen( item[i].str);
2495 }
2496 mii.fState = 0;
2497 if( item[i].type & MF_HELP) mii.fType |= MF_HELP;
2498 mii.wID = item[i].id;
2499 ret = InsertMenuItem( hmenu, -1, TRUE, &mii);
2500 ok(ret, "%d: InsertMenuItem(%04x, %04x, %p) error %u\n",
2501 i, item[i].type, item[i].id, item[i].str, GetLastError());
2502 }
2503 return hmenu;
2504 }
2505
2506 static void compare_menu_data(HMENU hmenu, const struct menu_data *item, INT item_count)
2507 {
2508 INT count, i;
2509 BOOL ret;
2510
2511 count = GetMenuItemCount(hmenu);
2512 ok(count == item_count, "expected %d, got %d menu items\n", count, item_count);
2513
2514 for (i = 0; i < count; i++)
2515 {
2516 char buf[20];
2517 MENUITEMINFO mii;
2518
2519 memset(&mii, 0, sizeof(mii));
2520 mii.cbSize = sizeof(mii);
2521 mii.dwTypeData = buf;
2522 mii.cch = sizeof(buf);
2523 mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_BITMAP;
2524 ret = GetMenuItemInfo(hmenu, i, TRUE, &mii);
2525 ok(ret, "GetMenuItemInfo(%u) failed\n", i);
2526 #if 0
2527 trace("item #%u: fType %04x, fState %04x, wID %04x, hbmp %p\n",
2528 i, mii.fType, mii.fState, mii.wID, mii.hbmpItem);
2529 #endif
2530 ok(mii.fType == item[i].type,
2531 "%u: expected fType %04x, got %04x\n", i, item[i].type, mii.fType);
2532 ok(mii.wID == item[i].id,
2533 "%u: expected wID %04x, got %04x\n", i, item[i].id, mii.wID);
2534 if (mii.hbmpItem || !item[i].str)
2535 /* For some reason Windows sets high word to not 0 for
2536 * not "magic" ids.
2537 */
2538 ok(LOWORD(mii.hbmpItem) == LOWORD(item[i].str),
2539 "%u: expected hbmpItem %p, got %p\n", i, item[i].str, mii.hbmpItem);
2540 else
2541 {
2542 ok(mii.cch == strlen(item[i].str),
2543 "%u: expected cch %u, got %u\n", i, (UINT)strlen(item[i].str), mii.cch);
2544 ok(!strcmp(mii.dwTypeData, item[i].str),
2545 "%u: expected dwTypeData %s, got %s\n", i, item[i].str, (LPCSTR)mii.dwTypeData);
2546 }
2547 }
2548 }
2549
2550 static void test_InsertMenu(void)
2551 {
2552 HBITMAP hbm = CreateBitmap(1,1,1,1,NULL);
2553 /* Note: XP treats only bitmap handles 1 - 6 as "magic" ones
2554 * regardless of their id.
2555 */
2556 static const struct menu_data in1[] =
2557 {
2558 { MF_STRING, 1, "File" },
2559 { MF_BITMAP|MF_HELP, SC_CLOSE, MAKEINTRESOURCE(1) },
2560 { MF_STRING|MF_HELP, 2, "Help" }
2561 };
2562 static const struct menu_data out1[] =
2563 {
2564 { MF_STRING, 1, "File" },
2565 { MF_STRING|MF_HELP, 2, "Help" },
2566 { MF_BITMAP|MF_HELP, SC_CLOSE, MAKEINTRESOURCE(1) }
2567 };
2568 static const struct menu_data out1a[] =
2569 {
2570 { MF_STRING, 1, "File" },
2571 { MF_STRING|MF_HELP, 2, "Help" },
2572 { MF_HELP, SC_CLOSE, MAKEINTRESOURCE(1) }
2573 };
2574 const struct menu_data in2[] =
2575 {
2576 { MF_STRING, 1, "File" },
2577 { MF_BITMAP|MF_HELP, SC_CLOSE, (char*)hbm },
2578 { MF_STRING|MF_HELP, 2, "Help" }
2579 };
2580 const struct menu_data out2[] =
2581 {
2582 { MF_STRING, 1, "File" },
2583 { MF_BITMAP|MF_HELP, SC_CLOSE, (char*)hbm },