[USB]
[reactos.git] / rostests / winetests / comdlg32 / filedlg.c
1 /*
2 * Unit test suite for comdlg32 API functions: file dialogs
3 *
4 * Copyright 2007 Google (Lei Zhang)
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22 #include <windows.h>
23 #include <wine/test.h>
24
25 #include "initguid.h"
26 #include "shlguid.h"
27 #define COBJMACROS
28 #include "shobjidl.h"
29
30 /* ##### */
31
32 static int resizesupported = TRUE;
33
34 static void toolbarcheck( HWND hDlg)
35 {
36 /* test toolbar properties */
37 /* bug #10532 */
38 int maxtextrows;
39 HWND ctrl;
40 DWORD ret;
41 char classname[20];
42
43 for( ctrl = GetWindow( hDlg, GW_CHILD);
44 ctrl ; ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
45 GetClassName( ctrl, classname, 10);
46 classname[7] = '\0';
47 if( !strcmp( classname, "Toolbar")) break;
48 }
49 ok( ctrl != NULL, "could not get the toolbar control\n");
50 ret = SendMessage( ctrl, TB_ADDSTRING, 0, (LPARAM)"winetestwinetest\0\0");
51 ok( ret == 0, "addstring returned %d (expected 0)\n", ret);
52 maxtextrows = SendMessage( ctrl, TB_GETTEXTROWS, 0, 0);
53 ok( maxtextrows == 0 || broken(maxtextrows == 1), /* Win2k and below */
54 "Get(Max)TextRows returned %d (expected 0)\n", maxtextrows);
55 }
56
57
58 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
59 {
60 LPNMHDR nmh;
61
62 if( msg == WM_NOTIFY)
63 {
64 nmh = (LPNMHDR) lParam;
65 if( nmh->code == CDN_INITDONE)
66 {
67 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
68 } else if (nmh->code == CDN_FOLDERCHANGE )
69 {
70 char buf[1024];
71 int ret;
72
73 memset(buf, 0x66, sizeof(buf));
74 ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
75 ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
76 if (ret > 5)
77 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
78 toolbarcheck( GetParent(hDlg));
79 }
80 }
81
82 return 0;
83 }
84
85 /* bug 6829 */
86 static void test_DialogCancel(void)
87 {
88 OPENFILENAMEA ofn;
89 BOOL result;
90 char szFileName[MAX_PATH] = "";
91 char szInitialDir[MAX_PATH];
92
93 GetWindowsDirectory(szInitialDir, MAX_PATH);
94
95 ZeroMemory(&ofn, sizeof(ofn));
96
97 ofn.lStructSize = sizeof(ofn);
98 ofn.hwndOwner = NULL;
99 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
100 ofn.lpstrFile = szFileName;
101 ofn.nMaxFile = MAX_PATH;
102 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
103 ofn.lpstrDefExt = "txt";
104 ofn.lpfnHook = OFNHookProc;
105 ofn.lpstrInitialDir = szInitialDir;
106
107 PrintDlgA(NULL);
108 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
109 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
110
111 result = GetOpenFileNameA(&ofn);
112 ok(0 == result, "expected 0, got %d\n", result);
113 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
114 CommDlgExtendedError());
115
116 PrintDlgA(NULL);
117 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
118 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
119
120 result = GetSaveFileNameA(&ofn);
121 ok(0 == result, "expected 0, got %d\n", result);
122 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
123 CommDlgExtendedError());
124
125 PrintDlgA(NULL);
126 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
127 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
128
129 /* Before passing the ofn to Unicode functions, remove the ANSI strings */
130 ofn.lpstrFilter = NULL;
131 ofn.lpstrInitialDir = NULL;
132 ofn.lpstrDefExt = NULL;
133
134 PrintDlgA(NULL);
135 ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
136 "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
137
138 SetLastError(0xdeadbeef);
139 result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
140 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
141 win_skip("GetOpenFileNameW is not implemented\n");
142 else
143 {
144 ok(0 == result, "expected 0, got %d\n", result);
145 ok(0 == CommDlgExtendedError() ||
146 broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
147 "expected 0, got %d\n", CommDlgExtendedError());
148 }
149
150 SetLastError(0xdeadbeef);
151 result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
152 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
153 win_skip("GetSaveFileNameW is not implemented\n");
154 else
155 {
156 ok(0 == result, "expected 0, got %d\n", result);
157 ok(0 == CommDlgExtendedError() ||
158 broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
159 "expected 0, got %d\n", CommDlgExtendedError());
160 }
161 }
162
163 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
164 {
165 if (msg == WM_NOTIFY)
166 {
167 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
168 {
169 IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
170 IShellView *shell_view = NULL;
171 IShellView2 *shell_view2 = NULL;
172 SV2CVW2_PARAMS view_params;
173 FOLDERSETTINGS folder_settings;
174 HRESULT hr;
175 RECT rect = {0, 0, 0, 0};
176
177 hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
178 ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
179 if (FAILED(hr)) goto cleanup;
180
181 hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
182 if (hr == E_NOINTERFACE)
183 {
184 win_skip("IShellView2 not supported\n");
185 goto cleanup;
186 }
187 ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
188 if (FAILED(hr)) goto cleanup;
189
190 hr = IShellView2_DestroyViewWindow(shell_view2);
191 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
192
193 folder_settings.ViewMode = FVM_LIST;
194 folder_settings.fFlags = 0;
195
196 view_params.cbSize = sizeof(view_params);
197 view_params.psvPrev = NULL;
198 view_params.pfs = &folder_settings;
199 view_params.psbOwner = shell_browser;
200 view_params.prcView = &rect;
201 view_params.pvid = NULL;
202 view_params.hwndView = NULL;
203
204 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
205 if (hr == E_FAIL)
206 {
207 win_skip("CreateViewWindow2 is broken on Vista/W2K8\n");
208 goto cleanup;
209 }
210 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
211 if (FAILED(hr)) goto cleanup;
212
213 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
214 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
215 ok(folder_settings.ViewMode == FVM_LIST,
216 "view mode is %d, expected FVM_LIST\n",
217 folder_settings.ViewMode);
218
219 hr = IShellView2_DestroyViewWindow(shell_view2);
220 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
221
222 /* XP and W2K3 need this. On Win9x and W2K the call to DestroyWindow() fails and has
223 * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
224 */
225 DestroyWindow(view_params.hwndView);
226
227 view_params.pvid = &VID_Details;
228 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
229 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
230 if (FAILED(hr)) goto cleanup;
231
232 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
233 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
234 ok(folder_settings.ViewMode == FVM_DETAILS ||
235 broken(folder_settings.ViewMode == FVM_LIST), /* Win9x */
236 "view mode is %d, expected FVM_DETAILS\n",
237 folder_settings.ViewMode);
238
239 cleanup:
240 if (shell_view2) IShellView2_Release(shell_view2);
241 if (shell_view) IShellView_Release(shell_view);
242 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
243 }
244 }
245 return 0;
246 }
247
248 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
249 {
250 if (msg == WM_INITDIALOG)
251 {
252 HWND p,cb;
253 INT sel;
254 p = GetParent(dlg);
255 ok(p!=NULL, "Failed to get parent of template\n");
256 cb = GetDlgItem(p,0x470);
257 ok(cb!=NULL, "Failed to get filter combobox\n");
258 sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
259 ok (sel != -1, "Failed to get selection from filter listbox\n");
260 }
261 if (msg == WM_NOTIFY)
262 {
263 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
264 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
265 }
266 return 0;
267 }
268
269 static void test_create_view_window2(void)
270 {
271 OPENFILENAMEA ofn = {0};
272 char filename[1024] = {0};
273 DWORD ret;
274
275 ofn.lStructSize = sizeof(ofn);
276 ofn.lpstrFile = filename;
277 ofn.nMaxFile = 1024;
278 ofn.lpfnHook = create_view_window2_hook;
279 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
280 ret = GetOpenFileNameA(&ofn);
281 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
282 ret = CommDlgExtendedError();
283 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
284 }
285
286 static void test_create_view_template(void)
287 {
288 OPENFILENAMEA ofn = {0};
289 char filename[1024] = {0};
290 DWORD ret;
291
292 ofn.lStructSize = sizeof(ofn);
293 ofn.lpstrFile = filename;
294 ofn.nMaxFile = 1024;
295 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
296 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
297 ofn.hInstance = GetModuleHandleA(NULL);
298 ofn.lpTemplateName = "template1";
299 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
300 ret = GetOpenFileNameA(&ofn);
301 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
302 ret = CommDlgExtendedError();
303 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
304 }
305
306 /* test cases for resizing of the file dialog */
307 struct {
308 DWORD flags;
309 int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
310 int resize_timer1; /* change in first WM_TIMER handler */
311 int resize_check; /* expected change (in second WM_TIMER handler) */
312 BOOL todo; /* mark that test todo_wine */
313 BOOL testcontrols; /* test resizing and moving of the controls */
314 } resize_testcases[] = {
315 { 0 , 10, 10, 20,FALSE,FALSE}, /* 0 */
316 { 0 ,-10,-10,-20,FALSE,FALSE},
317 { OFN_ENABLESIZING , 0, 0, 0,FALSE,FALSE},
318 { OFN_ENABLESIZING , 0,-10, 0,FALSE,FALSE},
319 { OFN_ENABLESIZING , 0, 10, 10,FALSE, TRUE},
320 { OFN_ENABLESIZING ,-10, 0, 10,FALSE,FALSE}, /* 5 */
321 { OFN_ENABLESIZING , 10, 0, 10,FALSE,FALSE},
322 { OFN_ENABLESIZING , 0, 10, 20,FALSE,FALSE},
323 /* mark the end */
324 { 0xffffffff }
325 };
326
327 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
328 {
329 static RECT initrc, rc;
330 static int index, count;
331 static int gotSWP_bottom, gotShowWindow;
332 HWND parent = GetParent( dlg);
333 int resize;
334 #define MAXNRCTRLS 30
335 static RECT ctrlrcs[MAXNRCTRLS];
336 static int ctrlids[MAXNRCTRLS];
337 static HWND ctrls[MAXNRCTRLS];
338 static int nrctrls;
339
340 switch( msg)
341 {
342 case WM_INITDIALOG:
343 {
344 DWORD style;
345
346 index = ((OPENFILENAME*)lParam)->lCustData;
347 count = 0;
348 gotSWP_bottom = gotShowWindow = 0;
349 /* test style */
350 style = GetWindowLong( parent, GWL_STYLE);
351 if( resize_testcases[index].flags & OFN_ENABLESIZING)
352 if( !(style & WS_SIZEBOX)) {
353 win_skip( "OFN_ENABLESIZING flag not supported.\n");
354 resizesupported = FALSE;
355 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
356 } else
357 ok( style & WS_SIZEBOX,
358 "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
359 else
360 ok( !(style & WS_SIZEBOX),
361 "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
362 break;
363 }
364 case WM_NOTIFY:
365 {
366 if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
367 GetWindowRect( parent, &initrc);
368 if( (resize = resize_testcases[index].resize_folderchange)){
369 MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
370 initrc.bottom - initrc.top + resize, TRUE);
371 }
372 SetTimer( dlg, 0, 100, 0);
373 }
374 break;
375 }
376 case WM_TIMER:
377 {
378 if( count == 0){
379 /* store the control rectangles */
380 if( resize_testcases[index].testcontrols) {
381 HWND ctrl;
382 int i;
383 for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
384 i < MAXNRCTRLS && ctrl;
385 i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
386 ctrlids[i] = GetDlgCtrlID( ctrl);
387 GetWindowRect( ctrl, &ctrlrcs[i]);
388 MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
389 ctrls[i] = ctrl;
390 }
391 nrctrls = i;
392 }
393 if( (resize = resize_testcases[index].resize_timer1)){
394 GetWindowRect( parent, &rc);
395 MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
396 rc.bottom - rc.top + resize, TRUE);
397 }
398 } else if( count == 1){
399 resize = resize_testcases[index].resize_check;
400 GetWindowRect( parent, &rc);
401 if( resize_testcases[index].todo){
402 todo_wine {
403 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
404 "testid %d size-x change %d expected %d\n", index,
405 rc.right - rc.left - initrc.right + initrc.left, resize);
406 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
407 "testid %d size-y change %d expected %d\n", index,
408 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
409 }
410 }else{
411 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
412 "testid %d size-x change %d expected %d\n", index,
413 rc.right - rc.left - initrc.right + initrc.left, resize);
414 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
415 "testid %d size-y change %d expected %d\n", index,
416 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
417 }
418 if( resize_testcases[index].testcontrols) {
419 int i;
420 RECT rc;
421 for( i = 0; i < nrctrls; i++) {
422 GetWindowRect( ctrls[i], &rc);
423 MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
424 switch( ctrlids[i]){
425
426 /* test if RECT R1, moved and sized result in R2 */
427 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
428 ((R1).left + (Mx) ==(R2).left \
429 &&(R1).top + (My) ==(R2).top \
430 &&(R1).right + (Mx) + (Sx) == (R2).right \
431 &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
432
433 /* sized horizontal and moved vertical */
434 case cmb1:
435 case edt1:
436 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0) ||
437 broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
438 "control id %03x should have sized horizontally and moved 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);
442 break;
443 /* sized horizontal and vertical */
444 case lst2:
445 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
446 "control id %03x should have sized horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
447 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
448 ctrlrcs[i].right, ctrlrcs[i].bottom,
449 rc.left, rc.top, rc.right, rc.bottom);
450 break;
451 /* moved horizontal and vertical */
452 case IDCANCEL:
453 case pshHelp:
454 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0) ||
455 broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
456 "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
457 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
458 ctrlrcs[i].right, ctrlrcs[i].bottom,
459 rc.left, rc.top, rc.right, rc.bottom);
460 break;
461 /* moved vertically */
462 case chx1:
463 case stc2:
464 case stc3:
465 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
466 "control id %03x should have moved vertically, 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);
470 break;
471 /* resized horizontal */
472 case cmb2: /* aka IDC_LOOKIN */
473 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
474 TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
475 "control id %03x should have resized horizontally, before %d,%d-%d,%d after %d,%d-%d,%d\n",
476 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
477 ctrlrcs[i].right, ctrlrcs[i].bottom,
478 rc.left, rc.top, rc.right, rc.bottom);
479 break;
480 /* non moving non sizing controls */
481 case stc4:
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);
487 break;
488 /* todo_wine: non moving non sizing controls */
489 case lst1:
490 todo_wine
491 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
492 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
493 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
494 ctrlrcs[i].right, ctrlrcs[i].bottom,
495 rc.left, rc.top, rc.right, rc.bottom);
496 break;
497 /* don't test: id is not unique */
498 case IDOK:
499 case stc1:
500 case 0:
501 case -1:
502 break;
503 default:
504 trace("untested control id %03x before %d,%d-%d,%d after %d,%d-%d,%d\n",
505 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
506 ctrlrcs[i].right, ctrlrcs[i].bottom,
507 rc.left, rc.top, rc.right, rc.bottom);
508 #undef TESTRECTS
509 #undef MAXNRCTRLS
510 }
511 }
512 }
513 KillTimer( dlg, 0);
514 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
515 }
516 count++;
517 }
518 break;
519 case WM_WINDOWPOSCHANGING:
520 {
521 WINDOWPOS *pwp = (WINDOWPOS *)lParam;
522 if( !index && pwp->hwndInsertAfter == HWND_BOTTOM){
523 gotSWP_bottom = 1;
524 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
525 }
526 }
527 break;
528 case WM_SHOWWINDOW:
529 {
530 if( !index){
531 gotShowWindow = 1;
532 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
533 }
534 }
535 break;
536 }
537 return 0;
538 }
539
540 static void test_resize(void)
541 {
542 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
543 char filename[1024] = {0};
544 DWORD ret;
545 int i;
546
547 ofn.lpstrFile = filename;
548 ofn.nMaxFile = 1024;
549 ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
550 ofn.hInstance = GetModuleHandle(NULL);
551 ofn.lpTemplateName = "template_sz";
552 for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
553 ofn.lCustData = i;
554 ofn.Flags = resize_testcases[i].flags |
555 OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
556 ret = GetOpenFileName(&ofn);
557 ok(!ret, "GetOpenFileName returned %#x\n", ret);
558 ret = CommDlgExtendedError();
559 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
560 }
561 }
562
563 /* test cases for control message IDOK */
564 /* Show case for bug #19079 */
565 static struct {
566 int retval; /* return code of the message handler */
567 BOOL setmsgresult; /* set the result in the DWLP_MSGRESULT */
568 BOOL usemsgokstr; /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
569 BOOL do_subclass; /* subclass the dialog hook procedure */
570 BOOL expclose; /* is the dialog expected to close ? */
571 BOOL actclose; /* has the dialog actually closed ? */
572 } ok_testcases[] = {
573 { 0, FALSE, FALSE, FALSE, TRUE},
574 { 0, TRUE, FALSE, FALSE, TRUE},
575 { 0, FALSE, FALSE, TRUE, TRUE},
576 { 0, TRUE, FALSE, TRUE, TRUE},
577 { 1, FALSE, FALSE, FALSE, TRUE},
578 { 1, TRUE, FALSE, FALSE, FALSE},
579 { 1, FALSE, FALSE, TRUE, FALSE},
580 { 1, TRUE, FALSE, TRUE, FALSE},
581 /* FILEOKSTRING tests */
582 { 1, TRUE, TRUE, FALSE, FALSE},
583 { 1, FALSE, TRUE, TRUE, FALSE},
584 /* mark the end */
585 { -1 }
586 };
587
588 /* test_ok_wndproc can be used as hook procedure or a subclass
589 * window proc for the file dialog */
590 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
591 {
592 HWND parent = GetParent( dlg);
593 static int index;
594 static UINT msgFILEOKSTRING;
595 if (msg == WM_INITDIALOG)
596 {
597 index = ((OPENFILENAME*)lParam)->lCustData;
598 ok_testcases[index].actclose = TRUE;
599 msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
600 }
601 if( msg == WM_NOTIFY) {
602 if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
603 SetTimer( dlg, 0, 100, 0);
604 PostMessage( parent, WM_COMMAND, IDOK, 0);
605 return FALSE;
606 } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
607 if( ok_testcases[index].usemsgokstr)
608 return FALSE;
609 if( ok_testcases[index].setmsgresult)
610 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
611 return ok_testcases[index].retval;
612 }
613 }
614 if( msg == msgFILEOKSTRING) {
615 if( !ok_testcases[index].usemsgokstr)
616 return FALSE;
617 if( ok_testcases[index].setmsgresult)
618 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
619 return ok_testcases[index].retval;
620 }
621 if( msg == WM_TIMER) {
622 /* the dialog did not close automatically */
623 ok_testcases[index].actclose = FALSE;
624 KillTimer( dlg, 0);
625 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
626 return FALSE;
627 }
628 if( ok_testcases[index].do_subclass)
629 return DefWindowProc( dlg, msg, wParam, lParam);
630 return FALSE;
631 }
632
633 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
634 {
635 if (msg == WM_SETFONT)
636 SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
637 return FALSE;
638 }
639
640 static void test_ok(void)
641 {
642 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
643 char filename[1024] = {0};
644 char tmpfilename[ MAX_PATH];
645 char curdir[MAX_PATH];
646 int i;
647 DWORD ret;
648
649 ok(GetCurrentDirectoryA(sizeof(curdir), curdir) != 0, "Failed to get current dir err %d\n", GetLastError());
650 if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
651 skip("Failed to create a temporary file name\n");
652 return;
653 }
654 ofn.lpstrFile = filename;
655 ofn.nMaxFile = 1024;
656 ofn.hInstance = GetModuleHandle(NULL);
657 ofn.lpTemplateName = "template1";
658 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
659 for( i = 0; ok_testcases[i].retval != -1; i++) {
660 strcpy( filename, tmpfilename);
661 ofn.lCustData = i;
662 ofn.lpfnHook = ok_testcases[i].do_subclass
663 ? (LPOFNHOOKPROC) ok_template_hook
664 : (LPOFNHOOKPROC) test_ok_wndproc;
665 ret = GetOpenFileNameA(&ofn);
666 ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
667 "testid %d: Open File dialog should %shave closed.\n", i,
668 ok_testcases[i].expclose ? "" : "NOT ");
669 ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
670 ret = CommDlgExtendedError();
671 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
672 ok(SetCurrentDirectoryA(curdir), "Failed to restore current dir err %d\n", GetLastError());
673 }
674 ret = DeleteFileA( tmpfilename);
675 ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
676 }
677
678 /* test arranging with a custom template */
679 typedef struct {
680 int x, y; /* left, top coordinates */
681 int cx, cy; /* width and height */
682 } posz;
683 static struct {
684 int nrcontrols; /* 0: no controls, 1: just the stc32 control 2: with button */
685 posz poszDlg;
686 posz poszStc32;
687 posz poszBtn;
688 DWORD ofnflags;
689 } arrange_tests[] = {
690 /* do not change the first two cases: used to get the uncustomized sizes */
691 { 0, {0},{0},{0},0 },
692 { 0, {0},{0},{0}, OFN_SHOWHELP},
693 /* two tests with just a subdialog, no controls */
694 { 0, {0, 0, 316, 76},{0},{0},0 },
695 { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
696 /* now with a control with id stc32 */
697 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
698 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
699 /* tests with size of the stc32 control higher or wider then the standard dialog */
700 { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
701 { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
702 /* move the stc32 control around */
703 { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
704 /* add control */
705 { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
706 /* enable resizing should make the dialog bigger */
707 { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
708 /* mark the end */
709 { -1 }
710 };
711
712 static LONG_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
713 {
714 static int index, fixhelp;
715 static posz posz0[2];
716 static RECT clrcParent, clrcChild, rcStc32;
717 static HWND hwndStc32;
718 HWND dlgParent;
719
720 dlgParent = GetParent( dlgChild);
721 if (msg == WM_INITDIALOG) {
722 index = ((OPENFILENAME*)lParam)->lCustData;
723 /* get the positions before rearrangement */
724 GetClientRect( dlgParent, &clrcParent);
725 GetClientRect( dlgChild, &clrcChild);
726 hwndStc32 = GetDlgItem( dlgChild, stc32);
727 if( hwndStc32) GetWindowRect( hwndStc32, &rcStc32);
728 }
729 if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
730 RECT wrcParent;
731
732 GetWindowRect( dlgParent, &wrcParent);
733 /* the fist two "tests" just save the dialogs position, with and without
734 * help button */
735 if( index == 0) {
736 posz0[0].x = wrcParent.left;
737 posz0[0].y = wrcParent.top;
738 posz0[0].cx = wrcParent.right - wrcParent.left;
739 posz0[0].cy = wrcParent.bottom - wrcParent.top;
740 } else if( index == 1) {
741 posz0[1].x = wrcParent.left;
742 posz0[1].y = wrcParent.top;
743 posz0[1].cx = wrcParent.right - wrcParent.left;
744 posz0[1].cy = wrcParent.bottom - wrcParent.top;
745 fixhelp = posz0[1].cy - posz0[0].cy;
746 } else {
747 /* the real tests */
748 int withhelp;
749 int expectx, expecty;
750 DWORD style;
751
752 withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
753 GetWindowRect( dlgParent, &wrcParent);
754 if( !hwndStc32) {
755 /* case with no custom subitem with stc32:
756 * default to all custom controls below the standard */
757 expecty = posz0[withhelp].cy + clrcChild.bottom;
758 expectx = posz0[withhelp].cx;
759 } else {
760 /* special case: there is a control with id stc32 */
761 /* expected height */
762 expecty = posz0[withhelp].cy;
763 if( rcStc32.bottom - rcStc32.top > clrcParent.bottom) {
764 expecty += clrcChild.bottom - clrcParent.bottom;
765 if( !withhelp) expecty += fixhelp;
766 }
767 else
768 expecty += clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
769 /* expected width */
770 expectx = posz0[withhelp].cx;
771 if( rcStc32.right - rcStc32.left > clrcParent.right) {
772 expectx += clrcChild.right - clrcParent.right;
773 }
774 else
775 expectx += clrcChild.right - ( rcStc32.right - rcStc32.left) ;
776 }
777 style = GetWindowLong( dlgParent, GWL_STYLE);
778 if( !(style & WS_SIZEBOX)) {
779 /* without the OFN_ENABLESIZING flag */
780 ok( wrcParent.bottom - wrcParent.top == expecty,
781 "Wrong height of dialog %d, expected %d\n",
782 wrcParent.bottom - wrcParent.top, expecty);
783 ok( wrcParent.right - wrcParent.left == expectx,
784 "Wrong width of dialog %d, expected %d\n",
785 wrcParent.right - wrcParent.left, expectx);
786 } else todo_wine {
787 /* with the OFN_ENABLESIZING flag */
788 ok( wrcParent.bottom - wrcParent.top > expecty,
789 "Wrong height of dialog %d, expected more than %d\n",
790 wrcParent.bottom - wrcParent.top, expecty);
791 ok( wrcParent.right - wrcParent.left > expectx,
792 "Wrong width of dialog %d, expected more than %d\n",
793 wrcParent.right - wrcParent.left, expectx);
794 }
795
796 }
797 PostMessage( dlgParent, WM_COMMAND, IDCANCEL, 0);
798 }
799 return 0;
800 }
801
802 static void test_arrange(void)
803 {
804 OPENFILENAMEA ofn = {0};
805 char filename[1024] = {0};
806 DWORD ret;
807 HRSRC hRes;
808 HANDLE hDlgTmpl;
809 LPBYTE pv;
810 DLGTEMPLATE *template;
811 DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
812 int i;
813
814 /* load subdialog template into memory */
815 hRes = FindResource( GetModuleHandle(NULL), "template_stc32", (LPSTR)RT_DIALOG);
816 hDlgTmpl = LoadResource( GetModuleHandle(NULL), hRes );
817 /* get pointers to the structures for the dialog and the controls */
818 pv = LockResource( hDlgTmpl );
819 template = (DLGTEMPLATE*)pv;
820 if( template->x != 11111) {
821 win_skip("could not find the dialog template\n");
822 return;
823 }
824 /* skip dialog template, menu, class and title */
825 pv += sizeof(DLGTEMPLATE);
826 pv += 3 * sizeof(WORD);
827 /* skip font info */
828 while( *(WORD*)pv)
829 pv += sizeof(WORD);
830 pv += sizeof(WORD);
831 /* align on 32 bit boundaries */
832 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
833 itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
834 if( itemtemplateStc32->x != 22222) {
835 win_skip("could not find the first item template\n");
836 return;
837 }
838 /* skip itemtemplate, class, title and creation data */
839 pv += sizeof(DLGITEMTEMPLATE);
840 pv += 4 * sizeof(WORD);
841 /* align on 32 bit boundaries */
842 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
843 itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
844 if( itemtemplateBtn->x != 12345) {
845 win_skip("could not find the second item template\n");
846 return;
847 }
848
849 ofn.lStructSize = sizeof(ofn);
850 ofn.lpstrFile = filename;
851 ofn.nMaxFile = 1024;
852 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook_arrange;
853 ofn.hInstance = hDlgTmpl;
854 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
855 for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
856 ofn.lCustData = i;
857 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
858 arrange_tests[i].ofnflags;
859 template->cdit = arrange_tests[i].nrcontrols;
860 template->x = arrange_tests[i].poszDlg.x;
861 template->y = arrange_tests[i].poszDlg.y;
862 template->cx = arrange_tests[i].poszDlg.cx;
863 template->cy = arrange_tests[i].poszDlg.cy;
864 itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
865 itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
866 itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
867 itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
868 itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
869 itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
870 itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
871 itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
872 ret = GetOpenFileNameA(&ofn);
873 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
874 ret = CommDlgExtendedError();
875 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
876 }
877 }
878
879 static CHAR SYSDIR[MAX_PATH];
880
881 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
882 {
883 LPNMHDR nmh;
884
885 if( msg == WM_NOTIFY)
886 {
887 nmh = (LPNMHDR) lParam;
888 if( nmh->code == CDN_INITDONE)
889 {
890 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
891 }
892 else if ( nmh->code == CDN_FOLDERCHANGE)
893 {
894 char buf[1024];
895 int ret;
896
897 memset(buf, 0x66, sizeof(buf));
898 ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
899 ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
900 ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
901 }
902 }
903
904 return 0;
905 }
906
907 static void test_getfolderpath(void)
908 {
909 OPENFILENAMEA ofn;
910 BOOL result;
911 char szFileName[MAX_PATH] = "";
912 char szInitialDir[MAX_PATH];
913
914 /* We need to pick a different directory as the other tests because of new
915 * Windows 7 behavior.
916 */
917 GetSystemDirectory(szInitialDir, MAX_PATH);
918 lstrcpyA(SYSDIR, szInitialDir);
919
920 ZeroMemory(&ofn, sizeof(ofn));
921
922 ofn.lStructSize = sizeof(ofn);
923 ofn.hwndOwner = NULL;
924 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
925 ofn.lpstrFile = szFileName;
926 ofn.nMaxFile = MAX_PATH;
927 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
928 ofn.lpstrDefExt = "txt";
929 ofn.lpfnHook = path_hook_proc;
930 ofn.lpstrInitialDir = szInitialDir;
931
932 result = GetOpenFileNameA(&ofn);
933 ok(0 == result, "expected 0, got %d\n", result);
934 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
935 CommDlgExtendedError());
936
937 result = GetSaveFileNameA(&ofn);
938 ok(0 == result, "expected 0, got %d\n", result);
939 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
940 CommDlgExtendedError());
941 }
942
943 static void test_resizable2(void)
944 {
945 OPENFILENAMEA ofn = {0};
946 char filename[1024] = "pls press Enter if sizable, Esc otherwise";
947 DWORD ret;
948
949 /* interactive because there is no hook function */
950 if( !winetest_interactive) {
951 skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
952 return;
953 }
954 ofn.lStructSize = sizeof(ofn);
955 ofn.lpstrFile = filename;
956 ofn.nMaxFile = 1024;
957 ofn.lpfnHook = NULL;
958 ofn.hInstance = GetModuleHandleA(NULL);
959 ofn.lpTemplateName = "template1";
960 ofn.Flags = OFN_EXPLORER;
961 #define ISSIZABLE 1
962 ret = GetOpenFileNameA(&ofn);
963 ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
964 ret = CommDlgExtendedError();
965 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
966 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
967 ret = GetOpenFileNameA(&ofn);
968 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
969 ret = CommDlgExtendedError();
970 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
971 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
972 ofn.hInstance = LoadResource( GetModuleHandle(NULL), FindResource( GetModuleHandle(NULL), "template1", (LPSTR)RT_DIALOG));
973 ofn.lpTemplateName = NULL;
974 ret = GetOpenFileNameA(&ofn);
975 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
976 ret = CommDlgExtendedError();
977 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
978 ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
979 ret = GetOpenFileNameA(&ofn);
980 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
981 ret = CommDlgExtendedError();
982 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
983 #undef ISSIZABLE
984 }
985
986 START_TEST(filedlg)
987 {
988 test_DialogCancel();
989 test_create_view_window2();
990 test_create_view_template();
991 test_arrange();
992 test_resize();
993 test_ok();
994 test_getfolderpath();
995 if( resizesupported) test_resizable2();
996 }