comdlg32/tests: Fix a test failure on Vista/W2K.
[wine/hacks.git] / dlls / comdlg32 / tests / filedlg.c
blob8c32866f5336e88cf7c44e20bba445d4f3bba392
1 /*
2 * Unit test suite for comdlg32 API functions: file dialogs
4 * Copyright 2007 Google (Lei Zhang)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <windows.h>
23 #include <wine/test.h>
25 #include "initguid.h"
26 #include "shlguid.h"
27 #define COBJMACROS
28 #include "shobjidl.h"
30 /* ##### */
32 static void toolbarcheck( HWND hDlg)
34 /* test toolbar properties */
35 /* bug #10532 */
36 int maxtextrows;
37 HWND ctrl;
38 DWORD ret;
39 char classname[20];
41 for( ctrl = GetWindow( hDlg, GW_CHILD);
42 ctrl ; ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
43 GetClassName( ctrl, classname, 10);
44 classname[7] = '\0';
45 if( !strcmp( classname, "Toolbar")) break;
47 ok( ctrl != NULL, "could not get the toolbar control\n");
48 ret = SendMessage( ctrl, TB_ADDSTRING, 0, (LPARAM)"winetestwinetest\0\0");
49 ok( ret == 0, "addstring returned %d (expected 0)\n", ret);
50 maxtextrows = SendMessage( ctrl, TB_GETTEXTROWS, 0, 0);
51 ok( maxtextrows == 0 || broken(maxtextrows == 1), /* Win2k and below */
52 "Get(Max)TextRows returned %d (expected 0)\n", maxtextrows);
56 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
58 LPNMHDR nmh;
60 if( msg == WM_NOTIFY)
62 nmh = (LPNMHDR) lParam;
63 if( nmh->code == CDN_INITDONE)
65 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
66 } else if (nmh->code == CDN_FOLDERCHANGE )
68 char buf[1024];
69 int ret;
71 memset(buf, 0x66, sizeof(buf));
72 ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
73 ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
74 if (ret > 5)
75 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
76 toolbarcheck( GetParent(hDlg));
80 return 0;
83 /* bug 6829 */
84 static void test_DialogCancel(void)
86 OPENFILENAMEA ofn;
87 BOOL result;
88 char szFileName[MAX_PATH] = "";
89 char szInitialDir[MAX_PATH];
91 GetWindowsDirectory(szInitialDir, MAX_PATH);
93 ZeroMemory(&ofn, sizeof(ofn));
95 ofn.lStructSize = sizeof(ofn);
96 ofn.hwndOwner = NULL;
97 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
98 ofn.lpstrFile = szFileName;
99 ofn.nMaxFile = MAX_PATH;
100 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
101 ofn.lpstrDefExt = "txt";
102 ofn.lpfnHook = OFNHookProc;
103 ofn.lpstrInitialDir = szInitialDir;
105 PrintDlgA(NULL);
106 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
107 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
109 result = GetOpenFileNameA(&ofn);
110 ok(0 == result, "expected 0, got %d\n", result);
111 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
112 CommDlgExtendedError());
114 PrintDlgA(NULL);
115 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
116 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
118 result = GetSaveFileNameA(&ofn);
119 ok(0 == result, "expected 0, got %d\n", result);
120 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
121 CommDlgExtendedError());
123 PrintDlgA(NULL);
124 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
125 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
127 /* Before passing the ofn to Unicode functions, remove the ANSI strings */
128 ofn.lpstrFilter = NULL;
129 ofn.lpstrInitialDir = NULL;
130 ofn.lpstrDefExt = NULL;
132 PrintDlgA(NULL);
133 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
134 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
136 SetLastError(0xdeadbeef);
137 result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
138 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
139 win_skip("GetOpenFileNameW is not implemented\n");
140 else
142 ok(0 == result, "expected 0, got %d\n", result);
143 ok(0 == CommDlgExtendedError() ||
144 broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
145 "expected 0, got %d\n", CommDlgExtendedError());
148 SetLastError(0xdeadbeef);
149 result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
150 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
151 win_skip("GetSaveFileNameW is not implemented\n");
152 else
154 ok(0 == result, "expected 0, got %d\n", result);
155 ok(0 == CommDlgExtendedError() ||
156 broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
157 "expected 0, got %d\n", CommDlgExtendedError());
161 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
163 if (msg == WM_NOTIFY)
165 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
167 IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
168 IShellView *shell_view = NULL;
169 IShellView2 *shell_view2 = NULL;
170 SV2CVW2_PARAMS view_params;
171 FOLDERSETTINGS folder_settings;
172 HRESULT hr;
173 RECT rect = {0, 0, 0, 0};
175 hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
176 ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
177 if (FAILED(hr)) goto cleanup;
179 hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
180 if (hr == E_NOINTERFACE)
182 win_skip("IShellView2 not supported\n");
183 goto cleanup;
185 ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
186 if (FAILED(hr)) goto cleanup;
188 hr = IShellView2_DestroyViewWindow(shell_view2);
189 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
191 folder_settings.ViewMode = FVM_LIST;
192 folder_settings.fFlags = 0;
194 view_params.cbSize = sizeof(view_params);
195 view_params.psvPrev = NULL;
196 view_params.pfs = &folder_settings;
197 view_params.psbOwner = shell_browser;
198 view_params.prcView = &rect;
199 view_params.pvid = NULL;
200 view_params.hwndView = NULL;
202 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
203 if (hr == E_FAIL)
205 win_skip("CreateViewWindow2 is broken on Vista/W2K8\n");
206 goto cleanup;
208 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
209 if (FAILED(hr)) goto cleanup;
211 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
212 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
213 ok(folder_settings.ViewMode == FVM_LIST,
214 "view mode is %d, expected FVM_LIST\n",
215 folder_settings.ViewMode);
217 hr = IShellView2_DestroyViewWindow(shell_view2);
218 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
220 /* XP and W2K3 need this. On Win9x and W2K the call to DestroyWindow() fails and has
221 * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
223 DestroyWindow(view_params.hwndView);
225 view_params.pvid = &VID_Details;
226 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
227 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
228 if (FAILED(hr)) goto cleanup;
230 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
231 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
232 ok(folder_settings.ViewMode == FVM_DETAILS ||
233 broken(folder_settings.ViewMode == FVM_LIST), /* Win9x */
234 "view mode is %d, expected FVM_DETAILS\n",
235 folder_settings.ViewMode);
237 cleanup:
238 if (shell_view2) IShellView2_Release(shell_view2);
239 if (shell_view) IShellView_Release(shell_view);
240 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
243 return 0;
246 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
248 if (msg == WM_INITDIALOG)
250 HWND p,cb;
251 INT sel;
252 p = GetParent(dlg);
253 ok(p!=NULL, "Failed to get parent of template\n");
254 cb = GetDlgItem(p,0x470);
255 ok(cb!=NULL, "Failed to get filter combobox\n");
256 sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
257 ok (sel != -1, "Failed to get selection from filter listbox\n");
259 if (msg == WM_NOTIFY)
261 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
262 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
264 return 0;
267 static void test_create_view_window2(void)
269 OPENFILENAMEA ofn = {0};
270 char filename[1024] = {0};
271 DWORD ret;
273 ofn.lStructSize = sizeof(ofn);
274 ofn.lpstrFile = filename;
275 ofn.nMaxFile = 1024;
276 ofn.lpfnHook = create_view_window2_hook;
277 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
278 ret = GetOpenFileNameA(&ofn);
279 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
280 ret = CommDlgExtendedError();
281 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
284 static void test_create_view_template(void)
286 OPENFILENAMEA ofn = {0};
287 char filename[1024] = {0};
288 DWORD ret;
290 ofn.lStructSize = sizeof(ofn);
291 ofn.lpstrFile = filename;
292 ofn.nMaxFile = 1024;
293 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
294 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
295 ofn.hInstance = GetModuleHandleA(NULL);
296 ofn.lpTemplateName = "template1";
297 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
298 ret = GetOpenFileNameA(&ofn);
299 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
300 ret = CommDlgExtendedError();
301 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
304 /* test cases for resizing of the file dialog */
305 struct {
306 DWORD flags;
307 int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
308 int resize_timer1; /* change in first WM_TIMER handler */
309 int resize_check; /* expected change (in second WM_TIMER handler) */
310 BOOL todo; /* mark that test todo_wine */
311 BOOL testcontrols; /* test resizing and moving of the controls */
312 } resize_testcases[] = {
313 { 0 , 10, 10, 20,FALSE,FALSE}, /* 0 */
314 { 0 ,-10,-10,-20,FALSE,FALSE},
315 { OFN_ENABLESIZING , 0, 0, 0,FALSE,FALSE},
316 { OFN_ENABLESIZING , 0,-10, 0,FALSE,FALSE},
317 { OFN_ENABLESIZING , 0, 10, 10,FALSE, TRUE},
318 { OFN_ENABLESIZING ,-10, 0, 10,FALSE,FALSE}, /* 5 */
319 { OFN_ENABLESIZING , 10, 0, 10,FALSE,FALSE},
320 { OFN_ENABLESIZING , 0, 10, 20,FALSE,FALSE},
321 /* mark the end */
322 { 0xffffffff }
325 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
327 static RECT initrc, rc;
328 static int index, count;
329 static int gotSWP_bottom, gotShowWindow;
330 HWND parent = GetParent( dlg);
331 int resize;
332 #define MAXNRCTRLS 30
333 static RECT ctrlrcs[MAXNRCTRLS];
334 static int ctrlids[MAXNRCTRLS];
335 static HWND ctrls[MAXNRCTRLS];
336 static int nrctrls;
338 switch( msg)
340 case WM_INITDIALOG:
342 DWORD style;
344 index = ((OPENFILENAME*)lParam)->lCustData;
345 count = 0;
346 gotSWP_bottom = gotShowWindow = 0;
347 /* test style */
348 style = GetWindowLong( parent, GWL_STYLE);
349 if( resize_testcases[index].flags & OFN_ENABLESIZING)
350 if( !(style & WS_SIZEBOX)) {
351 win_skip( "OFN_ENABLESIZING flag not supported.\n");
352 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
353 } else
354 ok( style & WS_SIZEBOX,
355 "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
356 else
357 ok( !(style & WS_SIZEBOX),
358 "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
359 break;
361 case WM_NOTIFY:
363 if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
364 GetWindowRect( parent, &initrc);
365 if( (resize = resize_testcases[index].resize_folderchange)){
366 MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
367 initrc.bottom - initrc.top + resize, TRUE);
369 SetTimer( dlg, 0, 100, 0);
371 break;
373 case WM_TIMER:
375 if( count == 0){
376 /* store the control rectangles */
377 if( resize_testcases[index].testcontrols) {
378 HWND ctrl;
379 int i;
380 for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
381 i < MAXNRCTRLS && ctrl;
382 i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
383 ctrlids[i] = GetDlgCtrlID( ctrl);
384 GetWindowRect( ctrl, &ctrlrcs[i]);
385 MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
386 ctrls[i] = ctrl;
388 nrctrls = i;
390 if( (resize = resize_testcases[index].resize_timer1)){
391 GetWindowRect( parent, &rc);
392 MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
393 rc.bottom - rc.top + resize, TRUE);
395 } else if( count == 1){
396 resize = resize_testcases[index].resize_check;
397 GetWindowRect( parent, &rc);
398 if( resize_testcases[index].todo){
399 todo_wine {
400 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
401 "testid %d size-x change %d expected %d\n", index,
402 rc.right - rc.left - initrc.right + initrc.left, resize);
403 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
404 "testid %d size-y change %d expected %d\n", index,
405 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
407 }else{
408 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
409 "testid %d size-x change %d expected %d\n", index,
410 rc.right - rc.left - initrc.right + initrc.left, resize);
411 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
412 "testid %d size-y change %d expected %d\n", index,
413 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
415 if( resize_testcases[index].testcontrols) {
416 int i;
417 RECT rc;
418 for( i = 0; i < nrctrls; i++) {
419 GetWindowRect( ctrls[i], &rc);
420 MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
421 switch( ctrlids[i]){
423 /* test if RECT R1, moved and sized result in R2 */
424 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
425 ((R1).left + (Mx) ==(R2).left \
426 &&(R1).top + (My) ==(R2).top \
427 &&(R1).right + (Mx) + (Sx) == (R2).right \
428 &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
430 /* sized horizontal and moved vertical */
431 case cmb1:
432 case edt1:
433 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0) ||
434 broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
435 "control id %03x should have sized horizontally and moved vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
436 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
437 ctrlrcs[i].right, ctrlrcs[i].bottom,
438 rc.left, rc.top, rc.right, rc.bottom);
439 break;
440 /* sized horizontal and vertical */
441 case lst2:
442 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
443 "control id %03x should have sized horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
444 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
445 ctrlrcs[i].right, ctrlrcs[i].bottom,
446 rc.left, rc.top, rc.right, rc.bottom);
447 break;
448 /* moved horizontal and vertical */
449 case IDCANCEL:
450 case pshHelp:
451 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0) ||
452 broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
453 "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
454 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
455 ctrlrcs[i].right, ctrlrcs[i].bottom,
456 rc.left, rc.top, rc.right, rc.bottom);
457 break;
458 /* moved vertically */
459 case chx1:
460 case stc2:
461 case stc3:
462 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
463 "control id %03x should have moved vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
464 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
465 ctrlrcs[i].right, ctrlrcs[i].bottom,
466 rc.left, rc.top, rc.right, rc.bottom);
467 break;
468 /* resized horizontal */
469 case cmb2: /* aka IDC_LOOKIN */
470 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
471 TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
472 "control id %03x should have resized horizontally, before %d,%d-%d,%d after %d,%d-%d,%d\n",
473 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
474 ctrlrcs[i].right, ctrlrcs[i].bottom,
475 rc.left, rc.top, rc.right, rc.bottom);
476 break;
477 /* non moving non sizing controls */
478 case stc4:
479 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
480 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
481 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
482 ctrlrcs[i].right, ctrlrcs[i].bottom,
483 rc.left, rc.top, rc.right, rc.bottom);
484 break;
485 /* todo_wine: non moving non sizing controls */
486 case lst1:
487 todo_wine
488 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
489 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
490 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
491 ctrlrcs[i].right, ctrlrcs[i].bottom,
492 rc.left, rc.top, rc.right, rc.bottom);
493 break;
494 /* don't test: id is not unique */
495 case IDOK:
496 case stc1:
497 case 0:
498 case -1:
499 break;
500 default:
501 trace("untested control id %03x before %d,%d-%d,%d after %d,%d-%d,%d\n",
502 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
503 ctrlrcs[i].right, ctrlrcs[i].bottom,
504 rc.left, rc.top, rc.right, rc.bottom);
505 #undef TESTRECTS
506 #undef MAXNRCTRLS
510 KillTimer( dlg, 0);
511 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
513 count++;
515 break;
516 case WM_WINDOWPOSCHANGING:
518 WINDOWPOS *pwp = (WINDOWPOS *)lParam;
519 if( !index && pwp->hwndInsertAfter == HWND_BOTTOM){
520 gotSWP_bottom = 1;
521 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
524 break;
525 case WM_SHOWWINDOW:
527 if( !index){
528 gotShowWindow = 1;
529 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
532 break;
534 return 0;
537 static void test_resize(void)
539 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
540 char filename[1024] = {0};
541 DWORD ret;
542 int i;
544 ofn.lpstrFile = filename;
545 ofn.nMaxFile = 1024;
546 ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
547 ofn.hInstance = GetModuleHandle(NULL);
548 ofn.lpTemplateName = "template_sz";
549 for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
550 ofn.lCustData = i;
551 ofn.Flags = resize_testcases[i].flags |
552 OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
553 ret = GetOpenFileName(&ofn);
554 ok(!ret, "GetOpenFileName returned %#x\n", ret);
555 ret = CommDlgExtendedError();
556 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
560 /* test cases for control message IDOK */
561 /* Show case for bug #19079 */
562 static struct {
563 int retval; /* return code of the message handler */
564 BOOL setmsgresult; /* set the result in the DWLP_MSGRESULT */
565 BOOL usemsgokstr; /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
566 BOOL do_subclass; /* subclass the dialog hook procedure */
567 BOOL expclose; /* is the dialog expected to close ? */
568 BOOL actclose; /* has the dialog actually closed ? */
569 } ok_testcases[] = {
570 { 0, FALSE, FALSE, FALSE, TRUE},
571 { 0, TRUE, FALSE, FALSE, TRUE},
572 { 0, FALSE, FALSE, TRUE, TRUE},
573 { 0, TRUE, FALSE, TRUE, TRUE},
574 { 1, FALSE, FALSE, FALSE, TRUE},
575 { 1, TRUE, FALSE, FALSE, FALSE},
576 { 1, FALSE, FALSE, TRUE, FALSE},
577 { 1, TRUE, FALSE, TRUE, FALSE},
578 /* FILEOKSTRING tests */
579 { 1, TRUE, TRUE, FALSE, FALSE},
580 { 1, FALSE, TRUE, TRUE, FALSE},
581 /* mark the end */
582 { -1 }
585 /* test_ok_wndproc can be used as hook procedure or a subclass
586 * window proc for the file dialog */
587 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
589 HWND parent = GetParent( dlg);
590 static int index;
591 static UINT msgFILEOKSTRING;
592 if (msg == WM_INITDIALOG)
594 index = ((OPENFILENAME*)lParam)->lCustData;
595 ok_testcases[index].actclose = TRUE;
596 msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
598 if( msg == WM_NOTIFY) {
599 if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
600 SetTimer( dlg, 0, 100, 0);
601 PostMessage( parent, WM_COMMAND, IDOK, 0);
602 return FALSE;
603 } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
604 if( ok_testcases[index].usemsgokstr)
605 return FALSE;
606 if( ok_testcases[index].setmsgresult)
607 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
608 return ok_testcases[index].retval;
611 if( msg == msgFILEOKSTRING) {
612 if( !ok_testcases[index].usemsgokstr)
613 return FALSE;
614 if( ok_testcases[index].setmsgresult)
615 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
616 return ok_testcases[index].retval;
618 if( msg == WM_TIMER) {
619 /* the dialog did not close automatically */
620 ok_testcases[index].actclose = FALSE;
621 KillTimer( dlg, 0);
622 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
623 return FALSE;
625 if( ok_testcases[index].do_subclass)
626 return DefWindowProc( dlg, msg, wParam, lParam);
627 return FALSE;
630 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
632 if (msg == WM_SETFONT)
633 SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
634 return FALSE;
637 static void test_ok(void)
639 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
640 char filename[1024] = {0};
641 char tmpfilename[ MAX_PATH];
642 char curdir[MAX_PATH];
643 int i;
644 DWORD ret;
646 ok(GetCurrentDirectoryA(sizeof(curdir), curdir) != 0, "Failed to get current dir err %d\n", GetLastError());
647 if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
648 skip("Failed to create a temporary file name\n");
649 return;
651 ofn.lpstrFile = filename;
652 ofn.nMaxFile = 1024;
653 ofn.hInstance = GetModuleHandle(NULL);
654 ofn.lpTemplateName = "template1";
655 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
656 for( i = 0; ok_testcases[i].retval != -1; i++) {
657 strcpy( filename, tmpfilename);
658 ofn.lCustData = i;
659 ofn.lpfnHook = ok_testcases[i].do_subclass
660 ? (LPOFNHOOKPROC) ok_template_hook
661 : (LPOFNHOOKPROC) test_ok_wndproc;
662 ret = GetOpenFileNameA(&ofn);
663 ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
664 "testid %d: Open File dialog should %shave closed.\n", i,
665 ok_testcases[i].expclose ? "" : "NOT ");
666 ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
667 ret = CommDlgExtendedError();
668 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
669 ok(SetCurrentDirectoryA(curdir), "Failed to restore current dir err %d\n", GetLastError());
671 ret = DeleteFileA( tmpfilename);
672 ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
675 /* test arranging with a custom template */
676 typedef struct {
677 int x, y; /* left, top coordinates */
678 int cx, cy; /* width and height */
679 } posz;
680 static struct {
681 int nrcontrols; /* 0: no controls, 1: just the stc32 control 2: with button */
682 posz poszDlg;
683 posz poszStc32;
684 posz poszBtn;
685 DWORD ofnflags;
686 } arrange_tests[] = {
687 /* do not change the first two cases: used to get the uncustomized sizes */
688 { 0, {0},{0},{0},0 },
689 { 0, {0},{0},{0}, OFN_SHOWHELP},
690 /* two tests with just a subdialog, no controls */
691 { 0, {0, 0, 316, 76},{0},{0},0 },
692 { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
693 /* now with a control with id stc32 */
694 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
695 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
696 /* tests with size of the stc32 control higher or wider then the standard dialog */
697 { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
698 { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
699 /* move the stc32 control around */
700 { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
701 /* add control */
702 { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
703 /* enable resizing should make the dialog bigger */
704 { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
705 /* mark the end */
706 { -1 }
709 static LONG_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
711 static int index, fixhelp;
712 static posz posz0[2];
713 static RECT clrcParent, clrcChild, rcStc32;
714 static HWND hwndStc32;
715 HWND dlgParent;
717 dlgParent = GetParent( dlgChild);
718 if (msg == WM_INITDIALOG) {
719 index = ((OPENFILENAME*)lParam)->lCustData;
720 /* get the positions before rearrangement */
721 GetClientRect( dlgParent, &clrcParent);
722 GetClientRect( dlgChild, &clrcChild);
723 hwndStc32 = GetDlgItem( dlgChild, stc32);
724 if( hwndStc32) GetWindowRect( hwndStc32, &rcStc32);
726 if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
727 RECT wrcParent;
729 GetWindowRect( dlgParent, &wrcParent);
730 /* the fist two "tests" just save the dialogs position, with and without
731 * help button */
732 if( index == 0) {
733 posz0[0].x = wrcParent.left;
734 posz0[0].y = wrcParent.top;
735 posz0[0].cx = wrcParent.right - wrcParent.left;
736 posz0[0].cy = wrcParent.bottom - wrcParent.top;
737 } else if( index == 1) {
738 posz0[1].x = wrcParent.left;
739 posz0[1].y = wrcParent.top;
740 posz0[1].cx = wrcParent.right - wrcParent.left;
741 posz0[1].cy = wrcParent.bottom - wrcParent.top;
742 fixhelp = posz0[1].cy - posz0[0].cy;
743 } else {
744 /* the real tests */
745 int withhelp;
746 int expectx, expecty;
747 DWORD style;
749 withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
750 GetWindowRect( dlgParent, &wrcParent);
751 if( !hwndStc32) {
752 /* case with no custom subitem with stc32:
753 * default to all custom controls below the standard */
754 expecty = posz0[withhelp].cy + clrcChild.bottom;
755 expectx = posz0[withhelp].cx;
756 } else {
757 /* special case: there is a control with id stc32 */
758 /* expected height */
759 expecty = posz0[withhelp].cy;
760 if( rcStc32.bottom - rcStc32.top > clrcParent.bottom) {
761 expecty += clrcChild.bottom - clrcParent.bottom;
762 if( !withhelp) expecty += fixhelp;
764 else
765 expecty += clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
766 /* expected width */
767 expectx = posz0[withhelp].cx;
768 if( rcStc32.right - rcStc32.left > clrcParent.right) {
769 expectx += clrcChild.right - clrcParent.right;
771 else
772 expectx += clrcChild.right - ( rcStc32.right - rcStc32.left) ;
774 style = GetWindowLong( dlgParent, GWL_STYLE);
775 if( !(style & WS_SIZEBOX)) {
776 /* without the OFN_ENABLESIZING flag */
777 ok( wrcParent.bottom - wrcParent.top == expecty,
778 "Wrong height of dialog %d, expected %d\n",
779 wrcParent.bottom - wrcParent.top, expecty);
780 ok( wrcParent.right - wrcParent.left == expectx,
781 "Wrong width of dialog %d, expected %d\n",
782 wrcParent.right - wrcParent.left, expectx);
783 } else todo_wine {
784 /* with the OFN_ENABLESIZING flag */
785 ok( wrcParent.bottom - wrcParent.top > expecty,
786 "Wrong height of dialog %d, expected more than %d\n",
787 wrcParent.bottom - wrcParent.top, expecty);
788 ok( wrcParent.right - wrcParent.left > expectx,
789 "Wrong width of dialog %d, expected more than %d\n",
790 wrcParent.right - wrcParent.left, expectx);
794 PostMessage( dlgParent, WM_COMMAND, IDCANCEL, 0);
796 return 0;
799 static void test_arrange(void)
801 OPENFILENAMEA ofn = {0};
802 char filename[1024] = {0};
803 DWORD ret;
804 HRSRC hRes;
805 HANDLE hDlgTmpl;
806 LPBYTE pv;
807 DLGTEMPLATE *template;
808 DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
809 int i;
811 /* load subdialog template into memory */
812 hRes = FindResource( GetModuleHandle(NULL), "template_stc32", (LPSTR)RT_DIALOG);
813 hDlgTmpl = LoadResource( GetModuleHandle(NULL), hRes );
814 /* get pointers to the structures for the dialog and the controls */
815 pv = LockResource( hDlgTmpl );
816 template = (DLGTEMPLATE*)pv;
817 if( template->x != 11111) {
818 win_skip("could not find the dialog template\n");
819 return;
821 /* skip dialog template, menu, class and title */
822 pv += sizeof(DLGTEMPLATE);
823 pv += 3 * sizeof(WORD);
824 /* skip font info */
825 while( *(WORD*)pv)
826 pv += sizeof(WORD);
827 pv += sizeof(WORD);
828 /* align on 32 bit boundaries */
829 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
830 itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
831 if( itemtemplateStc32->x != 22222) {
832 win_skip("could not find the first item template\n");
833 return;
835 /* skip itemtemplate, class, title and creation data */
836 pv += sizeof(DLGITEMTEMPLATE);
837 pv += 4 * sizeof(WORD);
838 /* align on 32 bit boundaries */
839 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
840 itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
841 if( itemtemplateBtn->x != 12345) {
842 win_skip("could not find the second item template\n");
843 return;
846 ofn.lStructSize = sizeof(ofn);
847 ofn.lpstrFile = filename;
848 ofn.nMaxFile = 1024;
849 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook_arrange;
850 ofn.hInstance = hDlgTmpl;
851 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
852 for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
853 ofn.lCustData = i;
854 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
855 arrange_tests[i].ofnflags;
856 template->cdit = arrange_tests[i].nrcontrols;
857 template->x = arrange_tests[i].poszDlg.x;
858 template->y = arrange_tests[i].poszDlg.y;
859 template->cx = arrange_tests[i].poszDlg.cx;
860 template->cy = arrange_tests[i].poszDlg.cy;
861 itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
862 itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
863 itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
864 itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
865 itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
866 itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
867 itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
868 itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
869 ret = GetOpenFileNameA(&ofn);
870 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
871 ret = CommDlgExtendedError();
872 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
876 START_TEST(filedlg)
878 test_DialogCancel();
879 test_create_view_window2();
880 test_create_view_template();
881 test_arrange();
882 test_resize();
883 test_ok();