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
23 #include <wine/test.h>
31 static int resizesupported = TRUE;
33 static void toolbarcheck( HWND hDlg)
35 /* test toolbar properties */
42 for( ctrl = GetWindow( hDlg, GW_CHILD);
43 ctrl ; ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
44 GetClassName( ctrl, classname, 10);
46 if( !strcmp( classname, "Toolbar")) break;
48 ok( ctrl != NULL, "could not get the toolbar control\n");
49 ret = SendMessage( ctrl, TB_ADDSTRING, 0, (LPARAM)"winetestwinetest\0\0");
50 ok( ret == 0, "addstring returned %d (expected 0)\n", ret);
51 maxtextrows = SendMessage( ctrl, TB_GETTEXTROWS, 0, 0);
52 ok( maxtextrows == 0 || broken(maxtextrows == 1), /* Win2k and below */
53 "Get(Max)TextRows returned %d (expected 0)\n", maxtextrows);
57 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
63 nmh = (LPNMHDR) lParam;
64 if( nmh->code == CDN_INITDONE)
66 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
67 } else if (nmh->code == CDN_FOLDERCHANGE )
72 memset(buf, 0x66, sizeof(buf));
73 ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
74 ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
76 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
77 toolbarcheck( GetParent(hDlg));
85 static void test_DialogCancel(void)
89 char szFileName[MAX_PATH] = "";
90 char szInitialDir[MAX_PATH];
92 GetWindowsDirectory(szInitialDir, MAX_PATH);
94 ZeroMemory(&ofn, sizeof(ofn));
96 ofn.lStructSize = sizeof(ofn);
98 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
99 ofn.lpstrFile = szFileName;
100 ofn.nMaxFile = MAX_PATH;
101 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
102 ofn.lpstrDefExt = "txt";
103 ofn.lpfnHook = OFNHookProc;
104 ofn.lpstrInitialDir = szInitialDir;
107 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
108 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
110 result = GetOpenFileNameA(&ofn);
111 ok(0 == result, "expected 0, got %d\n", result);
112 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
113 CommDlgExtendedError());
116 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
117 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
119 result = GetSaveFileNameA(&ofn);
120 ok(0 == result, "expected 0, got %d\n", result);
121 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
122 CommDlgExtendedError());
125 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
126 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
128 /* Before passing the ofn to Unicode functions, remove the ANSI strings */
129 ofn.lpstrFilter = NULL;
130 ofn.lpstrInitialDir = NULL;
131 ofn.lpstrDefExt = NULL;
134 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
135 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
137 SetLastError(0xdeadbeef);
138 result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
139 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
140 win_skip("GetOpenFileNameW is not implemented\n");
143 ok(0 == result, "expected 0, got %d\n", result);
144 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n", CommDlgExtendedError());
147 SetLastError(0xdeadbeef);
148 result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
149 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
150 win_skip("GetSaveFileNameW is not implemented\n");
153 ok(0 == result, "expected 0, got %d\n", result);
154 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n", CommDlgExtendedError());
158 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
160 if (msg == WM_NOTIFY)
162 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
164 IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
165 IShellView *shell_view = NULL;
166 IShellView2 *shell_view2 = NULL;
167 SV2CVW2_PARAMS view_params;
168 FOLDERSETTINGS folder_settings;
170 RECT rect = {0, 0, 0, 0};
172 hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
173 ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
174 if (FAILED(hr)) goto cleanup;
176 hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
177 if (hr == E_NOINTERFACE)
179 win_skip("IShellView2 not supported\n");
182 ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
183 if (FAILED(hr)) goto cleanup;
185 hr = IShellView2_DestroyViewWindow(shell_view2);
186 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
188 folder_settings.ViewMode = FVM_LIST;
189 folder_settings.fFlags = 0;
191 view_params.cbSize = sizeof(view_params);
192 view_params.psvPrev = NULL;
193 view_params.pfs = &folder_settings;
194 view_params.psbOwner = shell_browser;
195 view_params.prcView = ▭
196 view_params.pvid = NULL;
197 view_params.hwndView = NULL;
199 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
202 win_skip("CreateViewWindow2 is broken on Vista/W2K8\n");
205 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
206 if (FAILED(hr)) goto cleanup;
208 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
209 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
210 ok(folder_settings.ViewMode == FVM_LIST,
211 "view mode is %d, expected FVM_LIST\n",
212 folder_settings.ViewMode);
214 hr = IShellView2_DestroyViewWindow(shell_view2);
215 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
217 /* XP and W2K3 need this. On W2K the call to DestroyWindow() fails and has
218 * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
220 DestroyWindow(view_params.hwndView);
222 view_params.pvid = &VID_Details;
223 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
224 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
225 if (FAILED(hr)) goto cleanup;
227 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
228 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
229 ok(folder_settings.ViewMode == FVM_DETAILS, "view mode is %d, expected FVM_DETAILS\n",
230 folder_settings.ViewMode);
233 if (shell_view2) IShellView2_Release(shell_view2);
234 if (shell_view) IShellView_Release(shell_view);
235 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
241 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
243 if (msg == WM_INITDIALOG)
248 ok(p!=NULL, "Failed to get parent of template\n");
249 cb = GetDlgItem(p,0x470);
250 ok(cb!=NULL, "Failed to get filter combobox\n");
251 sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
252 ok (sel != -1, "Failed to get selection from filter listbox\n");
254 if (msg == WM_NOTIFY)
256 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
257 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
262 static void test_create_view_window2(void)
264 OPENFILENAMEA ofn = {0};
265 char filename[1024] = {0};
268 ofn.lStructSize = sizeof(ofn);
269 ofn.lpstrFile = filename;
271 ofn.lpfnHook = create_view_window2_hook;
272 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
273 ret = GetOpenFileNameA(&ofn);
274 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
275 ret = CommDlgExtendedError();
276 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
279 static void test_create_view_template(void)
281 OPENFILENAMEA ofn = {0};
282 char filename[1024] = {0};
285 ofn.lStructSize = sizeof(ofn);
286 ofn.lpstrFile = filename;
288 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
289 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
290 ofn.hInstance = GetModuleHandleA(NULL);
291 ofn.lpTemplateName = "template1";
292 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
293 ret = GetOpenFileNameA(&ofn);
294 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
295 ret = CommDlgExtendedError();
296 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
299 /* test cases for resizing of the file dialog */
300 static const struct {
302 int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
303 int resize_timer1; /* change in first WM_TIMER handler */
304 int resize_check; /* expected change (in second WM_TIMER handler) */
305 BOOL todo; /* mark that test todo_wine */
306 BOOL testcontrols; /* test resizing and moving of the controls */
307 } resize_testcases[] = {
308 { 0 , 10, 10, 20,FALSE,FALSE}, /* 0 */
309 { 0 ,-10,-10,-20,FALSE,FALSE},
310 { OFN_ENABLESIZING , 0, 0, 0,FALSE,FALSE},
311 { OFN_ENABLESIZING , 0,-10, 0,FALSE,FALSE},
312 { OFN_ENABLESIZING , 0, 10, 10,FALSE, TRUE},
313 { OFN_ENABLESIZING ,-10, 0, 10,FALSE,FALSE}, /* 5 */
314 { OFN_ENABLESIZING , 10, 0, 10,FALSE,FALSE},
315 { OFN_ENABLESIZING , 0, 10, 20,FALSE,FALSE},
320 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
322 static RECT initrc, rc;
323 static int index, count;
324 static int gotSWP_bottom, gotShowWindow;
325 HWND parent = GetParent( dlg);
327 #define MAXNRCTRLS 30
328 static RECT ctrlrcs[MAXNRCTRLS];
329 static int ctrlids[MAXNRCTRLS];
330 static HWND ctrls[MAXNRCTRLS];
339 index = ((OPENFILENAME*)lParam)->lCustData;
341 gotSWP_bottom = gotShowWindow = 0;
343 style = GetWindowLong( parent, GWL_STYLE);
344 if( resize_testcases[index].flags & OFN_ENABLESIZING)
345 if( !(style & WS_SIZEBOX)) {
346 win_skip( "OFN_ENABLESIZING flag not supported.\n");
347 resizesupported = FALSE;
348 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
350 ok( style & WS_SIZEBOX,
351 "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
353 ok( !(style & WS_SIZEBOX),
354 "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
359 if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
360 GetWindowRect( parent, &initrc);
361 if( (resize = resize_testcases[index].resize_folderchange)){
362 MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
363 initrc.bottom - initrc.top + resize, TRUE);
365 SetTimer( dlg, 0, 100, 0);
372 /* store the control rectangles */
373 if( resize_testcases[index].testcontrols) {
376 for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
377 i < MAXNRCTRLS && ctrl;
378 i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
379 ctrlids[i] = GetDlgCtrlID( ctrl);
380 GetWindowRect( ctrl, &ctrlrcs[i]);
381 MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
386 if( (resize = resize_testcases[index].resize_timer1)){
387 GetWindowRect( parent, &rc);
388 MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
389 rc.bottom - rc.top + resize, TRUE);
391 } else if( count == 1){
392 resize = resize_testcases[index].resize_check;
393 GetWindowRect( parent, &rc);
394 if( resize_testcases[index].todo){
396 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
397 "testid %d size-x change %d expected %d\n", index,
398 rc.right - rc.left - initrc.right + initrc.left, resize);
399 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
400 "testid %d size-y change %d expected %d\n", index,
401 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
404 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
405 "testid %d size-x change %d expected %d\n", index,
406 rc.right - rc.left - initrc.right + initrc.left, resize);
407 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
408 "testid %d size-y change %d expected %d\n", index,
409 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
411 if( resize_testcases[index].testcontrols) {
414 for( i = 0; i < nrctrls; i++) {
415 GetWindowRect( ctrls[i], &rc);
416 MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
419 /* test if RECT R1, moved and sized result in R2 */
420 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
421 ((R1).left + (Mx) ==(R2).left \
422 &&(R1).top + (My) ==(R2).top \
423 &&(R1).right + (Mx) + (Sx) == (R2).right \
424 &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
426 /* sized horizontal and moved vertical */
429 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0),
430 "control id %03x should have sized horizontally and moved vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
431 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
432 ctrlrcs[i].right, ctrlrcs[i].bottom,
433 rc.left, rc.top, rc.right, rc.bottom);
435 /* sized horizontal and vertical */
437 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
438 "control id %03x should have sized horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
439 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
440 ctrlrcs[i].right, ctrlrcs[i].bottom,
441 rc.left, rc.top, rc.right, rc.bottom);
443 /* moved horizontal and vertical */
446 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0),
447 "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
448 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
449 ctrlrcs[i].right, ctrlrcs[i].bottom,
450 rc.left, rc.top, rc.right, rc.bottom);
452 /* moved vertically */
456 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
457 "control id %03x should have moved vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
458 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
459 ctrlrcs[i].right, ctrlrcs[i].bottom,
460 rc.left, rc.top, rc.right, rc.bottom);
462 /* resized horizontal */
463 case cmb2: /* aka IDC_LOOKIN */
464 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
465 TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
466 "control id %03x should have resized horizontally, before %d,%d-%d,%d after %d,%d-%d,%d\n",
467 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
468 ctrlrcs[i].right, ctrlrcs[i].bottom,
469 rc.left, rc.top, rc.right, rc.bottom);
471 /* non moving non sizing controls */
473 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
474 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
475 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
476 ctrlrcs[i].right, ctrlrcs[i].bottom,
477 rc.left, rc.top, rc.right, rc.bottom);
479 /* todo_wine: non moving non sizing controls */
482 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
483 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
484 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
485 ctrlrcs[i].right, ctrlrcs[i].bottom,
486 rc.left, rc.top, rc.right, rc.bottom);
488 /* don't test: id is not unique */
495 trace("untested control id %03x before %d,%d-%d,%d after %d,%d-%d,%d\n",
496 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
497 ctrlrcs[i].right, ctrlrcs[i].bottom,
498 rc.left, rc.top, rc.right, rc.bottom);
505 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
510 case WM_WINDOWPOSCHANGING:
512 WINDOWPOS *pwp = (WINDOWPOS *)lParam;
513 if( !index && pwp->hwndInsertAfter == HWND_BOTTOM){
515 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
523 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
531 static void test_resize(void)
533 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
534 char filename[1024] = {0};
538 ofn.lpstrFile = filename;
540 ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
541 ofn.hInstance = GetModuleHandle(NULL);
542 ofn.lpTemplateName = "template_sz";
543 for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
545 ofn.Flags = resize_testcases[i].flags |
546 OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
547 ret = GetOpenFileName(&ofn);
548 ok(!ret, "GetOpenFileName returned %#x\n", ret);
549 ret = CommDlgExtendedError();
550 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
554 /* test cases for control message IDOK */
555 /* Show case for bug #19079 */
557 int retval; /* return code of the message handler */
558 BOOL setmsgresult; /* set the result in the DWLP_MSGRESULT */
559 BOOL usemsgokstr; /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
560 BOOL do_subclass; /* subclass the dialog hook procedure */
561 BOOL expclose; /* is the dialog expected to close ? */
562 BOOL actclose; /* has the dialog actually closed ? */
563 } ok_wndproc_testcase;
565 static ok_wndproc_testcase ok_testcases[] = {
566 { 0, FALSE, FALSE, FALSE, TRUE},
567 { 0, TRUE, FALSE, FALSE, TRUE},
568 { 0, FALSE, FALSE, TRUE, TRUE},
569 { 0, TRUE, FALSE, TRUE, TRUE},
570 { 1, FALSE, FALSE, FALSE, TRUE},
571 { 1, TRUE, FALSE, FALSE, FALSE},
572 { 1, FALSE, FALSE, TRUE, FALSE},
573 { 1, TRUE, FALSE, TRUE, FALSE},
574 /* FILEOKSTRING tests */
575 { 1, TRUE, TRUE, FALSE, FALSE},
576 { 1, FALSE, TRUE, TRUE, FALSE},
581 /* test_ok_wndproc can be used as hook procedure or a subclass
582 * window proc for the file dialog */
583 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
585 HWND parent = GetParent( dlg);
586 static ok_wndproc_testcase *testcase = NULL;
587 static UINT msgFILEOKSTRING;
588 if (msg == WM_INITDIALOG)
590 testcase = (ok_wndproc_testcase*)((OPENFILENAME*)lParam)->lCustData;
591 testcase->actclose = TRUE;
592 msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
594 if( msg == WM_NOTIFY) {
595 if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
596 SetTimer( dlg, 0, 100, 0);
597 PostMessage( parent, WM_COMMAND, IDOK, 0);
599 } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
600 if( testcase->usemsgokstr)
602 if( testcase->setmsgresult)
603 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
604 return testcase->retval;
607 if( msg == msgFILEOKSTRING) {
608 if( !testcase->usemsgokstr)
610 if( testcase->setmsgresult)
611 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
612 return testcase->retval;
614 if( msg == WM_TIMER) {
615 /* the dialog did not close automatically */
616 testcase->actclose = FALSE;
618 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
621 if( testcase && testcase->do_subclass)
622 return DefWindowProc( dlg, msg, wParam, lParam);
626 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
628 if (msg == WM_SETFONT)
629 SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
633 static void test_ok(void)
635 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
636 char filename[1024] = {0};
637 char tmpfilename[ MAX_PATH];
638 char curdir[MAX_PATH];
643 cdret = GetCurrentDirectoryA(sizeof(curdir), curdir);
644 ok(cdret, "Failed to get current dir err %d\n", GetLastError());
645 if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
646 skip("Failed to create a temporary file name\n");
649 ofn.lpstrFile = filename;
651 ofn.hInstance = GetModuleHandle(NULL);
652 ofn.lpTemplateName = "template1";
653 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
654 for( i = 0; ok_testcases[i].retval != -1; i++) {
655 strcpy( filename, tmpfilename);
656 ofn.lCustData = (LPARAM)(ok_testcases + i);
657 ofn.lpfnHook = ok_testcases[i].do_subclass
658 ? (LPOFNHOOKPROC) ok_template_hook
659 : (LPOFNHOOKPROC) test_ok_wndproc;
660 ret = GetOpenFileNameA(&ofn);
661 ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
662 "testid %d: Open File dialog should %shave closed.\n", i,
663 ok_testcases[i].expclose ? "" : "NOT ");
664 ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
665 ret = CommDlgExtendedError();
666 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
667 cdret = SetCurrentDirectoryA(curdir);
668 ok(cdret, "Failed to restore current dir err %d\n", GetLastError());
670 ret = DeleteFileA( tmpfilename);
671 ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
674 /* test arranging with a custom template */
676 int x, y; /* left, top coordinates */
677 int cx, cy; /* width and height */
680 int nrcontrols; /* 0: no controls, 1: just the stc32 control 2: with button */
685 } arrange_tests[] = {
686 /* do not change the first two cases: used to get the uncustomized sizes */
687 { 0, {0},{0},{0},0 },
688 { 0, {0},{0},{0}, OFN_SHOWHELP},
689 /* two tests with just a subdialog, no controls */
690 { 0, {0, 0, 316, 76},{0},{0},0 },
691 { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
692 /* now with a control with id stc32 */
693 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
694 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
695 /* tests with size of the stc32 control higher or wider then the standard dialog */
696 { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
697 { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
698 /* move the stc32 control around */
699 { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
701 { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
702 /* enable resizing should make the dialog bigger */
703 { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
708 static LONG_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
710 static int index, fixhelp;
711 static posz posz0[2];
712 static RECT clrcParent, clrcChild, rcStc32;
713 static HWND hwndStc32;
716 dlgParent = GetParent( dlgChild);
717 if (msg == WM_INITDIALOG) {
718 index = ((OPENFILENAME*)lParam)->lCustData;
719 /* get the positions before rearrangement */
720 GetClientRect( dlgParent, &clrcParent);
721 GetClientRect( dlgChild, &clrcChild);
722 hwndStc32 = GetDlgItem( dlgChild, stc32);
723 if( hwndStc32) GetWindowRect( hwndStc32, &rcStc32);
725 if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
728 GetWindowRect( dlgParent, &wrcParent);
729 /* the fist two "tests" just save the dialogs position, with and without
732 posz0[0].x = wrcParent.left;
733 posz0[0].y = wrcParent.top;
734 posz0[0].cx = wrcParent.right - wrcParent.left;
735 posz0[0].cy = wrcParent.bottom - wrcParent.top;
736 } else if( index == 1) {
737 posz0[1].x = wrcParent.left;
738 posz0[1].y = wrcParent.top;
739 posz0[1].cx = wrcParent.right - wrcParent.left;
740 posz0[1].cy = wrcParent.bottom - wrcParent.top;
741 fixhelp = posz0[1].cy - posz0[0].cy;
745 int expectx, expecty;
748 withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
749 GetWindowRect( dlgParent, &wrcParent);
751 /* case with no custom subitem with stc32:
752 * default to all custom controls below the standard */
753 expecty = posz0[withhelp].cy + clrcChild.bottom;
754 expectx = posz0[withhelp].cx;
756 /* special case: there is a control with id stc32 */
757 /* expected height */
758 expecty = posz0[withhelp].cy;
759 if( rcStc32.bottom - rcStc32.top + (withhelp ? 0 : fixhelp) > clrcParent.bottom) {
760 expecty += clrcChild.bottom - clrcParent.bottom;
761 if( !withhelp) expecty += fixhelp;
764 expecty += clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
766 expectx = posz0[withhelp].cx;
767 if( rcStc32.right - rcStc32.left > clrcParent.right) {
768 expectx += clrcChild.right - clrcParent.right;
771 expectx += clrcChild.right - ( rcStc32.right - rcStc32.left) ;
773 style = GetWindowLong( dlgParent, GWL_STYLE);
774 if( !(style & WS_SIZEBOX)) {
775 /* without the OFN_ENABLESIZING flag */
776 ok( wrcParent.bottom - wrcParent.top == expecty,
777 "Wrong height of dialog %d, expected %d\n",
778 wrcParent.bottom - wrcParent.top, expecty);
779 ok( wrcParent.right - wrcParent.left == expectx,
780 "Wrong width of dialog %d, expected %d\n",
781 wrcParent.right - wrcParent.left, expectx);
783 /* with the OFN_ENABLESIZING flag */
784 ok( wrcParent.bottom - wrcParent.top > expecty,
785 "Wrong height of dialog %d, expected more than %d\n",
786 wrcParent.bottom - wrcParent.top, expecty);
787 ok( wrcParent.right - wrcParent.left > expectx,
788 "Wrong width of dialog %d, expected more than %d\n",
789 wrcParent.right - wrcParent.left, expectx);
793 PostMessage( dlgParent, WM_COMMAND, IDCANCEL, 0);
798 static void test_arrange(void)
800 OPENFILENAMEA ofn = {0};
801 char filename[1024] = {0};
806 DLGTEMPLATE *template;
807 DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
810 /* load subdialog template into memory */
811 hRes = FindResource( GetModuleHandle(NULL), "template_stc32", (LPSTR)RT_DIALOG);
812 hDlgTmpl = LoadResource( GetModuleHandle(NULL), hRes );
813 /* get pointers to the structures for the dialog and the controls */
814 pv = LockResource( hDlgTmpl );
815 template = (DLGTEMPLATE*)pv;
816 if( template->x != 11111) {
817 win_skip("could not find the dialog template\n");
820 /* skip dialog template, menu, class and title */
821 pv += sizeof(DLGTEMPLATE);
822 pv += 3 * sizeof(WORD);
827 /* align on 32 bit boundaries */
828 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
829 itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
830 if( itemtemplateStc32->x != 22222) {
831 win_skip("could not find the first item template\n");
834 /* skip itemtemplate, class, title and creation data */
835 pv += sizeof(DLGITEMTEMPLATE);
836 pv += 4 * sizeof(WORD);
837 /* align on 32 bit boundaries */
838 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
839 itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
840 if( itemtemplateBtn->x != 12345) {
841 win_skip("could not find the second item template\n");
845 ofn.lStructSize = sizeof(ofn);
846 ofn.lpstrFile = filename;
848 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook_arrange;
849 ofn.hInstance = hDlgTmpl;
850 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
851 for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
853 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
854 arrange_tests[i].ofnflags;
855 template->cdit = arrange_tests[i].nrcontrols;
856 template->x = arrange_tests[i].poszDlg.x;
857 template->y = arrange_tests[i].poszDlg.y;
858 template->cx = arrange_tests[i].poszDlg.cx;
859 template->cy = arrange_tests[i].poszDlg.cy;
860 itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
861 itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
862 itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
863 itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
864 itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
865 itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
866 itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
867 itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
868 ret = GetOpenFileNameA(&ofn);
869 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
870 ret = CommDlgExtendedError();
871 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
875 static CHAR SYSDIR[MAX_PATH];
877 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
881 if( msg == WM_NOTIFY)
883 nmh = (LPNMHDR) lParam;
884 if( nmh->code == CDN_INITDONE)
886 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
888 else if ( nmh->code == CDN_FOLDERCHANGE)
893 memset(buf, 0x66, sizeof(buf));
894 ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
895 ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
896 ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
903 static void test_getfolderpath(void)
907 char szFileName[MAX_PATH] = "";
908 char szInitialDir[MAX_PATH];
910 /* We need to pick a different directory as the other tests because of new
911 * Windows 7 behavior.
913 GetSystemDirectory(szInitialDir, MAX_PATH);
914 lstrcpyA(SYSDIR, szInitialDir);
916 ZeroMemory(&ofn, sizeof(ofn));
918 ofn.lStructSize = sizeof(ofn);
919 ofn.hwndOwner = NULL;
920 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
921 ofn.lpstrFile = szFileName;
922 ofn.nMaxFile = MAX_PATH;
923 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
924 ofn.lpstrDefExt = "txt";
925 ofn.lpfnHook = path_hook_proc;
926 ofn.lpstrInitialDir = szInitialDir;
928 result = GetOpenFileNameA(&ofn);
929 ok(0 == result, "expected 0, got %d\n", result);
930 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
931 CommDlgExtendedError());
933 result = GetSaveFileNameA(&ofn);
934 ok(0 == result, "expected 0, got %d\n", result);
935 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
936 CommDlgExtendedError());
939 static void test_resizable2(void)
941 OPENFILENAMEA ofn = {0};
942 char filename[1024] = "pls press Enter if sizable, Esc otherwise";
945 /* interactive because there is no hook function */
946 if( !winetest_interactive) {
947 skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
950 ofn.lStructSize = sizeof(ofn);
951 ofn.lpstrFile = filename;
954 ofn.hInstance = GetModuleHandleA(NULL);
955 ofn.lpTemplateName = "template1";
956 ofn.Flags = OFN_EXPLORER;
958 ret = GetOpenFileNameA(&ofn);
959 ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
960 ret = CommDlgExtendedError();
961 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
962 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
963 ret = GetOpenFileNameA(&ofn);
964 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
965 ret = CommDlgExtendedError();
966 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
967 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
968 ofn.hInstance = LoadResource( GetModuleHandle(NULL), FindResource( GetModuleHandle(NULL), "template1", (LPSTR)RT_DIALOG));
969 ofn.lpTemplateName = NULL;
970 ret = GetOpenFileNameA(&ofn);
971 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
972 ret = CommDlgExtendedError();
973 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
974 ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
975 ret = GetOpenFileNameA(&ofn);
976 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
977 ret = CommDlgExtendedError();
978 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
982 static void test_mru(void)
984 ok_wndproc_testcase testcase = {0};
985 OPENFILENAME ofn = {sizeof(OPENFILENAME)};
986 const char *test_dir_name = "C:\\mru_test";
987 const char *test_file_name = "test.txt";
988 const char *test_full_path = "C:\\mru_test\\test.txt";
989 char filename_buf[MAX_PATH];
992 ofn.lpstrFile = filename_buf;
993 ofn.nMaxFile = sizeof(filename_buf);
994 ofn.lpTemplateName = "template1";
995 ofn.hInstance = GetModuleHandle(NULL);
996 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_NOCHANGEDIR;
997 ofn.lCustData = (LPARAM)&testcase;
998 ofn.lpfnHook = (LPOFNHOOKPROC)test_ok_wndproc;
1000 SetLastError(0xdeadbeef);
1001 ret = CreateDirectoryA(test_dir_name, NULL);
1002 ok(ret == TRUE, "CreateDirectoryA should have succeeded: %d\n", GetLastError());
1004 /* "teach" comdlg32 about this directory */
1005 strcpy(filename_buf, test_full_path);
1006 SetLastError(0xdeadbeef);
1007 ret = GetOpenFileNameA(&ofn);
1008 ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1009 ret = CommDlgExtendedError();
1010 ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1011 ok(testcase.actclose, "Open File dialog should have closed.\n");
1012 ok(!strcmp(ofn.lpstrFile, test_full_path), "Expected to get %s, got %s\n", test_full_path, ofn.lpstrFile);
1014 /* get a filename without a full path. it should return the file in
1015 * test_dir_name, not in the CWD */
1016 strcpy(filename_buf, test_file_name);
1017 SetLastError(0xdeadbeef);
1018 ret = GetOpenFileNameA(&ofn);
1019 ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1020 ret = CommDlgExtendedError();
1021 ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1022 ok(testcase.actclose, "Open File dialog should have closed.\n");
1023 if(strcmp(ofn.lpstrFile, test_full_path) != 0)
1024 win_skip("Platform doesn't save MRU data\n");
1026 SetLastError(0xdeadbeef);
1027 ret = RemoveDirectoryA(test_dir_name);
1028 ok(ret == TRUE, "RemoveDirectoryA should have succeeded: %d\n", GetLastError());
1031 static UINT_PTR WINAPI test_extension_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1033 HWND parent = GetParent( dlg);
1034 if( msg == WM_NOTIFY) {
1035 SetTimer( dlg, 0, 100, 0);
1036 PostMessage( parent, WM_COMMAND, IDOK, 0);
1038 if( msg == WM_TIMER) {
1039 /* the dialog did not close automatically */
1041 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
1046 static const char *defext_filters[] = {
1047 "TestFilter (*.pt*)\0*.pt*\0",
1048 "TestFilter (*.ab?)\0*.ab?\0",
1049 "TestFilter (*.*)\0*.*\0",
1050 NULL /* is a test, not an endmark! */
1053 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
1055 static void test_extension(void)
1057 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
1058 char filename[1024] = {0};
1059 char curdir[MAX_PATH];
1061 const char *test_file_name = "deadbeef";
1062 const char **cur_filter;
1066 boolret = GetCurrentDirectoryA(sizeof(curdir), curdir);
1067 ok(boolret, "Failed to get current dir err %d\n", GetLastError());
1069 /* Ignore .* extension */
1070 ofn.lStructSize = sizeof(ofn);
1071 ofn.hwndOwner = NULL;
1072 ofn.lpstrFile = filename;
1073 ofn.nMaxFile = MAX_PATH;
1074 ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
1075 ofn.lpstrDefExt = NULL;
1076 ofn.lpstrInitialDir = curdir;
1077 ofn.lpfnHook = test_extension_wndproc;
1078 ofn.nFileExtension = 0;
1080 for (cur_filter = defext_filters; cur_filter < defext_filters + ARRAY_SIZE(defext_filters); cur_filter++) {
1081 ofn.lpstrFilter = *cur_filter;
1082 strcpy(filename, test_file_name);
1083 boolret = GetSaveFileNameA(&ofn);
1084 ok(boolret, "expected true\n");
1085 ret = CommDlgExtendedError();
1086 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1087 filename_ptr = ofn.lpstrFile + strlen( ofn.lpstrFile ) - strlen( test_file_name );
1088 ok( strlen(ofn.lpstrFile) >= strlen(test_file_name), "Filename %s is too short\n", ofn.lpstrFile );
1089 ok( strcmp(filename_ptr, test_file_name) == 0,
1090 "Filename is %s, expected %s\n", filename_ptr, test_file_name );
1098 test_DialogCancel();
1099 test_create_view_window2();
1100 test_create_view_template();
1104 test_getfolderpath();
1106 if( resizesupported) test_resizable2();