c9a1c44f4701f0446c1ddffcad58c4cff8ae6319
[reactos.git] / rostests / winetests / comdlg32 / printdlg.c
1 /*
2 * Unit test suite for comdlg32 API functions: printer dialogs
3 *
4 * Copyright 2006-2007 Detlef Riekenberg
5 * Copyright 2013 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 */
22
23 #define COBJMACROS
24 #define CONST_VTABLE
25 #define WIN32_NO_STATUS
26
27 #include <wine/test.h>
28
29 //#include <stdarg.h>
30 #include <stdio.h>
31
32 //#include "windef.h"
33 //#include "winbase.h"
34 //#include "winerror.h"
35 #include <wingdi.h>
36 #include <winuser.h>
37 #include <objbase.h>
38
39 #include <cderr.h>
40 #include <commdlg.h>
41 #include <dlgs.h>
42
43 /* ########################### */
44
45 extern const IID IID_IObjectWithSite;
46
47 static HMODULE hcomdlg32;
48 static HRESULT (WINAPI * pPrintDlgExW)(LPPRINTDLGEXW);
49
50 /* ########################### */
51
52 static const CHAR emptyA[] = "";
53 static const CHAR PrinterPortsA[] = "PrinterPorts";
54
55 static const char *debugstr_guid(const GUID *guid)
56 {
57 static char buf[50];
58
59 if (!guid) return "(null)";
60 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
61 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
62 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
63 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
64 return buf;
65 }
66
67 /* ########################### */
68
69 static void test_PageSetupDlgA(void)
70 {
71 LPPAGESETUPDLGA pDlg;
72 DWORD res;
73
74 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA)) * 2);
75 if (!pDlg) return;
76
77 SetLastError(0xdeadbeef);
78 res = PageSetupDlgA(NULL);
79 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
80 "returned %u with %u and 0x%x (expected '0' and "
81 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
82
83 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
84 pDlg->lStructSize = sizeof(PAGESETUPDLGA) -1;
85 SetLastError(0xdeadbeef);
86 res = PageSetupDlgA(pDlg);
87 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
88 "returned %u with %u and 0x%x (expected '0' and "
89 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
90
91 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
92 pDlg->lStructSize = sizeof(PAGESETUPDLGA) +1;
93 pDlg->Flags = PSD_RETURNDEFAULT;
94 SetLastError(0xdeadbeef);
95 res = PageSetupDlgA(pDlg);
96 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
97 "returned %u with %u and 0x%x (expected '0' and CDERR_STRUCTSIZE)\n",
98 res, GetLastError(), CommDlgExtendedError());
99
100
101 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
102 pDlg->lStructSize = sizeof(PAGESETUPDLGA);
103 pDlg->Flags = PSD_RETURNDEFAULT | PSD_NOWARNING;
104 SetLastError(0xdeadbeef);
105 res = PageSetupDlgA(pDlg);
106 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
107 "returned %u with %u and 0x%x (expected '!= 0' or '0' and "
108 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
109
110 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
111 skip("No printer configured.\n");
112 HeapFree(GetProcessHeap(), 0, pDlg);
113 return;
114 }
115
116 ok( pDlg->hDevMode && pDlg->hDevNames,
117 "got %p and %p (expected '!= NULL' for both)\n",
118 pDlg->hDevMode, pDlg->hDevNames);
119
120 GlobalFree(pDlg->hDevMode);
121 GlobalFree(pDlg->hDevNames);
122
123 HeapFree(GetProcessHeap(), 0, pDlg);
124
125 }
126
127 /* ########################### */
128
129 static UINT_PTR CALLBACK print_hook_proc(HWND hdlg, UINT msg, WPARAM wp, LPARAM lp)
130 {
131 if (msg == WM_INITDIALOG)
132 {
133 /* some driver popup a dialog and hung the test or silently limit the number of copies,
134 when trying to set more than 999 copies */
135 SetDlgItemInt(hdlg, edt3, 123, FALSE);
136 PostMessage(hdlg, WM_COMMAND, IDOK, FALSE);
137 }
138 return 0;
139 }
140
141 static void test_PrintDlgA(void)
142 {
143 DWORD res, n_copies = 0;
144 LPPRINTDLGA pDlg;
145 DEVNAMES *pDevNames;
146 LPCSTR driver;
147 LPCSTR device;
148 LPCSTR port;
149 CHAR buffer[MAX_PATH];
150 LPSTR ptr;
151 DEVMODE *dm;
152
153 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA)) * 2);
154 if (!pDlg) return;
155
156
157 /* will crash with unpatched wine */
158 SetLastError(0xdeadbeef);
159 res = PrintDlgA(NULL);
160 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
161 "returned %d with 0x%x and 0x%x (expected '0' and "
162 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
163
164 ZeroMemory(pDlg, sizeof(PRINTDLGA));
165 pDlg->lStructSize = sizeof(PRINTDLGA) - 1;
166 SetLastError(0xdeadbeef);
167 res = PrintDlgA(pDlg);
168 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
169 "returned %d with 0x%x and 0x%x (expected '0' and "
170 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
171
172 ZeroMemory(pDlg, sizeof(PRINTDLGA));
173 pDlg->lStructSize = sizeof(PRINTDLGA) + 1;
174 pDlg->Flags = PD_RETURNDEFAULT;
175 SetLastError(0xdeadbeef);
176 res = PrintDlgA(pDlg);
177 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
178 "returned %u with %u and 0x%x (expected '0' and "
179 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
180
181
182 ZeroMemory(pDlg, sizeof(PRINTDLGA));
183 pDlg->lStructSize = sizeof(PRINTDLGA);
184 pDlg->Flags = PD_RETURNDEFAULT;
185 SetLastError(0xdeadbeef);
186 res = PrintDlgA(pDlg);
187 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
188 "returned %d with 0x%x and 0x%x (expected '!= 0' or '0' and "
189 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
190
191 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
192 skip("No printer configured.\n");
193 HeapFree(GetProcessHeap(), 0, pDlg);
194 return;
195 }
196
197 ok(pDlg->hDevNames != NULL, "(expected '!= NULL')\n");
198 pDevNames = GlobalLock(pDlg->hDevNames);
199 ok(pDevNames != NULL, "(expected '!= NULL')\n");
200
201 if (pDevNames) {
202 ok(pDevNames->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
203 ok(pDevNames->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
204 ok(pDevNames->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
205 ok(pDevNames->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", pDevNames->wDefault);
206
207 driver = (LPCSTR)pDevNames + pDevNames->wDriverOffset;
208 device = (LPCSTR)pDevNames + pDevNames->wDeviceOffset;
209 port = (LPCSTR)pDevNames + pDevNames->wOutputOffset;
210 trace("driver '%s' device '%s' port '%s'\n", driver, device, port);
211
212 /* The Driver Entry does not include a Path */
213 ptr = strrchr(driver, '\\');
214 ok( ptr == NULL, "got %p for '%s' (expected NULL for a simple name)\n", ptr, driver);
215
216 /* The Driver Entry does not have an extension (fixed to ".drv") */
217 ptr = strrchr(driver, '.');
218 todo_wine {
219 ok( ptr == NULL, "got %p for '%s' (expected NULL for no extension)\n", ptr, driver);
220 }
221
222
223 buffer[0] = '\0';
224 SetLastError(0xdeadbeef);
225 res = GetProfileStringA(PrinterPortsA, device, emptyA, buffer, sizeof(buffer));
226 ptr = strchr(buffer, ',');
227 ok( (res > 1) && (ptr != NULL),
228 "got %u with %u and %p for '%s' (expected '>1' and '!= NULL')\n",
229 res, GetLastError(), ptr, buffer);
230
231 if (ptr) ptr[0] = '\0';
232 ok( lstrcmpiA(driver, buffer) == 0,
233 "got driver '%s' (expected '%s')\n", driver, buffer);
234
235 n_copies = DeviceCapabilities(device, port, DC_COPIES, NULL, NULL);
236 ok(n_copies > 0, "DeviceCapabilities(DC_COPIES) failed\n");
237 }
238
239 GlobalUnlock(pDlg->hDevNames);
240 GlobalFree(pDlg->hDevMode);
241 GlobalFree(pDlg->hDevNames);
242
243 /* if device doesn't support printing of multiple copies then
244 * an attempt to set number of copies > 1 in print dialog would
245 * cause the PrintDlg under Windows display the MessageBox and
246 * the test will hang waiting for user response.
247 */
248 if (n_copies > 1)
249 {
250 ZeroMemory(pDlg, sizeof(*pDlg));
251 pDlg->lStructSize = sizeof(*pDlg);
252 pDlg->Flags = PD_ENABLEPRINTHOOK;
253 pDlg->lpfnPrintHook = print_hook_proc;
254 res = PrintDlg(pDlg);
255 ok(res, "PrintDlg error %#x\n", CommDlgExtendedError());
256 /* Version of Microsoft XPS Document Writer driver shipped before Win7
257 * reports that it can print multiple copies, but returns 1.
258 */
259 ok(pDlg->nCopies == 123 || broken(pDlg->nCopies == 1), "expected nCopies 123, got %d\n", pDlg->nCopies);
260 ok(pDlg->hDevMode != 0, "hDevMode should not be 0\n");
261 dm = GlobalLock(pDlg->hDevMode);
262 /* some broken drivers use always PD_USEDEVMODECOPIES */
263 ok((S1(U1(*dm)).dmCopies == 1) || broken(S1(U1(*dm)).dmCopies == 123),
264 "expected dm->dmCopies 1, got %d\n", S1(U1(*dm)).dmCopies);
265 GlobalUnlock(pDlg->hDevMode);
266 GlobalFree(pDlg->hDevMode);
267 GlobalFree(pDlg->hDevNames);
268
269 ZeroMemory(pDlg, sizeof(*pDlg));
270 pDlg->lStructSize = sizeof(*pDlg);
271 pDlg->Flags = PD_ENABLEPRINTHOOK | PD_USEDEVMODECOPIES;
272 pDlg->lpfnPrintHook = print_hook_proc;
273 res = PrintDlg(pDlg);
274 ok(res, "PrintDlg error %#x\n", CommDlgExtendedError());
275 ok(pDlg->nCopies == 1, "expected nCopies 1, got %d\n", pDlg->nCopies);
276 ok(pDlg->hDevMode != 0, "hDevMode should not be 0\n");
277 dm = GlobalLock(pDlg->hDevMode);
278 ok(S1(U1(*dm)).dmCopies == 123, "expected dm->dmCopies 123, got %d\n", S1(U1(*dm)).dmCopies);
279 GlobalUnlock(pDlg->hDevMode);
280 GlobalFree(pDlg->hDevMode);
281 GlobalFree(pDlg->hDevNames);
282 }
283
284 HeapFree(GetProcessHeap(), 0, pDlg);
285 }
286
287 /* ########################### */
288
289 static HRESULT WINAPI callback_QueryInterface(IPrintDialogCallback *iface,
290 REFIID riid, void **ppv)
291 {
292 ok(0, "callback_QueryInterface(%s): unexpected call\n", debugstr_guid(riid));
293 return E_NOINTERFACE;
294 }
295
296 static ULONG WINAPI callback_AddRef(IPrintDialogCallback *iface)
297 {
298 trace("callback_AddRef\n");
299 return 2;
300 }
301
302 static ULONG WINAPI callback_Release(IPrintDialogCallback *iface)
303 {
304 trace("callback_Release\n");
305 return 1;
306 }
307
308 static HRESULT WINAPI callback_InitDone(IPrintDialogCallback *iface)
309 {
310 trace("callback_InitDone\n");
311 return S_OK;
312 }
313
314 static HRESULT WINAPI callback_SelectionChange(IPrintDialogCallback *iface)
315 {
316 trace("callback_SelectionChange\n");
317 return S_OK;
318 }
319
320 static HRESULT WINAPI callback_HandleMessage(IPrintDialogCallback *iface,
321 HWND hdlg, UINT msg, WPARAM wp, LPARAM lp, LRESULT *res)
322 {
323 trace("callback_HandleMessage %p,%04x,%lx,%lx,%p\n", hdlg, msg, wp, lp, res);
324 /* *res = PD_RESULT_PRINT; */
325 return S_OK;
326 }
327
328 static const IPrintDialogCallbackVtbl callback_Vtbl =
329 {
330 callback_QueryInterface,
331 callback_AddRef,
332 callback_Release,
333 callback_InitDone,
334 callback_SelectionChange,
335 callback_HandleMessage
336 };
337
338 static IPrintDialogCallback callback = { &callback_Vtbl };
339
340 static HRESULT WINAPI unknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
341 {
342 trace("unknown_QueryInterface %s\n", debugstr_guid(riid));
343
344 if (IsEqualGUID(riid, &IID_IPrintDialogCallback))
345 {
346 *ppv = &callback;
347 return S_OK;
348 }
349 else if (IsEqualGUID(riid, &IID_IObjectWithSite))
350 {
351 *ppv = NULL;
352 return E_NOINTERFACE;
353 }
354
355 ok(0, "unexpected IID %s\n", debugstr_guid(riid));
356 *ppv = NULL;
357 return E_NOINTERFACE;
358 }
359
360 static ULONG WINAPI unknown_AddRef(IUnknown *iface)
361 {
362 trace("unknown_AddRef\n");
363 return 2;
364 }
365
366 static ULONG WINAPI unknown_Release(IUnknown *iface)
367 {
368 trace("unknown_Release\n");
369 return 1;
370 }
371
372 static const IUnknownVtbl unknown_Vtbl =
373 {
374 unknown_QueryInterface,
375 unknown_AddRef,
376 unknown_Release
377 };
378
379 static IUnknown unknown = { &unknown_Vtbl };
380
381 static void test_PrintDlgExW(void)
382 {
383 PRINTPAGERANGE pagerange[2];
384 LPPRINTDLGEXW pDlg;
385 DEVNAMES *dn;
386 HRESULT res;
387
388 /* PrintDlgEx not present before w2k */
389 if (!pPrintDlgExW) {
390 skip("PrintDlgExW not available\n");
391 return;
392 }
393
394 /* Set CommDlgExtendedError != 0 */
395 PrintDlg(NULL);
396 SetLastError(0xdeadbeef);
397 res = pPrintDlgExW(NULL);
398 ok( (res == E_INVALIDARG),
399 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
400 res, GetLastError(), CommDlgExtendedError());
401
402
403 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW)) + 8);
404 if (!pDlg) return;
405
406 /* lStructSize must be exact */
407 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
408 pDlg->lStructSize = sizeof(PRINTDLGEXW) - 1;
409 PrintDlg(NULL);
410 SetLastError(0xdeadbeef);
411 res = pPrintDlgExW(pDlg);
412 ok( (res == E_INVALIDARG),
413 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
414 res, GetLastError(), CommDlgExtendedError());
415
416
417 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
418 pDlg->lStructSize = sizeof(PRINTDLGEXW) + 1;
419 PrintDlg(NULL);
420 SetLastError(0xdeadbeef);
421 res = pPrintDlgExW(pDlg);
422 ok( (res == E_INVALIDARG),
423 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
424 res, GetLastError(), CommDlgExtendedError());
425
426
427 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
428 pDlg->lStructSize = sizeof(PRINTDLGEXW);
429 SetLastError(0xdeadbeef);
430 res = pPrintDlgExW(pDlg);
431 ok( (res == E_HANDLE),
432 "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
433 res, GetLastError(), CommDlgExtendedError());
434
435 /* nStartPage must be START_PAGE_GENERAL for the general page or a valid property sheet index */
436 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
437 pDlg->lStructSize = sizeof(PRINTDLGEXW);
438 pDlg->hwndOwner = GetDesktopWindow();
439 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
440 res = pPrintDlgExW(pDlg);
441 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
442
443 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
444 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
445 pDlg->lStructSize = sizeof(PRINTDLGEXW);
446 pDlg->hwndOwner = GetDesktopWindow();
447 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
448 pDlg->nStartPage = START_PAGE_GENERAL;
449 res = pPrintDlgExW(pDlg);
450 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
451
452 /* this is invalid: a valid lpPageRanges with 0 for nMaxPageRanges */
453 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
454 pDlg->lStructSize = sizeof(PRINTDLGEXW);
455 pDlg->hwndOwner = GetDesktopWindow();
456 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
457 pDlg->lpPageRanges = pagerange;
458 pDlg->nStartPage = START_PAGE_GENERAL;
459 res = pPrintDlgExW(pDlg);
460 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
461
462 /* this is invalid: NULL for lpPageRanges with a valid nMaxPageRanges */
463 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
464 pDlg->lStructSize = sizeof(PRINTDLGEXW);
465 pDlg->hwndOwner = GetDesktopWindow();
466 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
467 pDlg->nMaxPageRanges = 1;
468 pDlg->nStartPage = START_PAGE_GENERAL;
469 res = pPrintDlgExW(pDlg);
470 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
471
472 /* this works: lpPageRanges with a valid nMaxPageRanges */
473 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
474 pDlg->lStructSize = sizeof(PRINTDLGEXW);
475 pDlg->hwndOwner = GetDesktopWindow();
476 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
477 pDlg->nMaxPageRanges = 1;
478 pDlg->lpPageRanges = pagerange;
479 pDlg->nStartPage = START_PAGE_GENERAL;
480 res = pPrintDlgExW(pDlg);
481 if (res == E_FAIL)
482 {
483 skip("No printer configured.\n");
484 HeapFree(GetProcessHeap(), 0, pDlg);
485 return;
486 }
487
488 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
489
490 dn = GlobalLock(pDlg->hDevNames);
491 ok(dn != NULL, "expected '!= NULL' for GlobalLock(%p)\n",pDlg->hDevNames);
492 if (dn)
493 {
494 ok(dn->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
495 ok(dn->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
496 ok(dn->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
497 ok(dn->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", dn->wDefault);
498
499 GlobalUnlock(pDlg->hDevNames);
500 }
501 GlobalFree(pDlg->hDevMode);
502 GlobalFree(pDlg->hDevNames);
503
504 /* this works also: PD_NOPAGENUMS */
505 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
506 pDlg->lStructSize = sizeof(PRINTDLGEXW);
507 pDlg->hwndOwner = GetDesktopWindow();
508 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
509 pDlg->nStartPage = START_PAGE_GENERAL;
510 res = pPrintDlgExW(pDlg);
511 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
512 GlobalFree(pDlg->hDevMode);
513 GlobalFree(pDlg->hDevNames);
514
515 /* this works: PD_RETURNDC with PD_RETURNDEFAULT */
516 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
517 pDlg->lStructSize = sizeof(PRINTDLGEXW);
518 pDlg->hwndOwner = GetDesktopWindow();
519 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS | PD_RETURNDC;
520 pDlg->nStartPage = START_PAGE_GENERAL;
521 res = pPrintDlgExW(pDlg);
522 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
523 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNDC\n");
524 GlobalFree(pDlg->hDevMode);
525 GlobalFree(pDlg->hDevNames);
526 DeleteDC(pDlg->hDC);
527
528 /* this works: PD_RETURNIC with PD_RETURNDEFAULT */
529 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
530 pDlg->lStructSize = sizeof(PRINTDLGEXW);
531 pDlg->hwndOwner = GetDesktopWindow();
532 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS | PD_RETURNIC;
533 pDlg->nStartPage = START_PAGE_GENERAL;
534 res = pPrintDlgExW(pDlg);
535 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
536 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNIC\n");
537 GlobalFree(pDlg->hDevMode);
538 GlobalFree(pDlg->hDevNames);
539 DeleteDC(pDlg->hDC);
540
541 /* interactive PrintDlgEx tests */
542
543 if (!winetest_interactive)
544 {
545 skip("interactive PrintDlgEx tests (set WINETEST_INTERACTIVE=1)\n");
546 return;
547 }
548
549 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
550 pDlg->lStructSize = sizeof(PRINTDLGEXW);
551 pDlg->hwndOwner = GetDesktopWindow();
552 pDlg->Flags = PD_NOPAGENUMS | PD_RETURNIC;
553 pDlg->nStartPage = START_PAGE_GENERAL;
554 pDlg->lpCallback = &unknown;
555 pDlg->dwResultAction = S_OK;
556 res = pPrintDlgExW(pDlg);
557 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
558 ok(pDlg->dwResultAction == PD_RESULT_PRINT, "expected PD_RESULT_PRINT, got %#x\n", pDlg->dwResultAction);
559 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNIC\n");
560 GlobalFree(pDlg->hDevMode);
561 GlobalFree(pDlg->hDevNames);
562 DeleteDC(pDlg->hDC);
563
564 HeapFree(GetProcessHeap(), 0, pDlg);
565 }
566
567 static BOOL abort_proc_called = FALSE;
568 static BOOL CALLBACK abort_proc(HDC hdc, int error) { return abort_proc_called = TRUE; }
569 static void test_abort_proc(void)
570 {
571 HDC print_dc;
572 RECT rect = {0, 0, 100, 100};
573 DOCINFOA doc_info = {0};
574 PRINTDLGA pd = {0};
575 char filename[MAX_PATH];
576 int job_id;
577
578 if (!GetTempFileNameA(".", "prn", 0, filename))
579 {
580 skip("Failed to create a temporary file name\n");
581 return;
582 }
583
584 pd.lStructSize = sizeof(pd);
585 pd.Flags = PD_RETURNDEFAULT | PD_ALLPAGES | PD_RETURNDC | PD_PRINTTOFILE;
586 pd.nFromPage = 1;
587 pd.nToPage = 1;
588 pd.nCopies = 1;
589
590 if (!PrintDlgA(&pd))
591 {
592 skip("No default printer available.\n");
593 goto end;
594 }
595 GlobalFree(pd.hDevMode);
596 GlobalFree(pd.hDevNames);
597
598 ok(pd.hDC != NULL, "PrintDlg didn't return a DC.\n");
599 if (!(print_dc = pd.hDC))
600 goto end;
601
602 ok(SetAbortProc(print_dc, abort_proc) > 0, "SetAbortProc failed\n");
603 ok(!abort_proc_called, "AbortProc got called unexpectedly by SetAbortProc.\n");
604 abort_proc_called = FALSE;
605
606 doc_info.cbSize = sizeof(doc_info);
607 doc_info.lpszDocName = "Some document";
608 doc_info.lpszOutput = filename;
609
610 job_id = StartDocA(print_dc, &doc_info);
611
612 ok(job_id > 0 ||
613 GetLastError() == ERROR_SPL_NO_STARTDOC, /* Vista can fail with this error when using the XPS driver */
614 "StartDocA failed ret %d gle %d\n", job_id, GetLastError());
615
616 if(job_id <= 0)
617 {
618 skip("StartDoc failed\n");
619 goto end;
620 }
621
622 /* StartDoc may or may not call abort proc */
623
624 abort_proc_called = FALSE;
625 ok(StartPage(print_dc) > 0, "StartPage failed\n");
626 ok(!abort_proc_called, "AbortProc got called unexpectedly by StartPage.\n");
627 abort_proc_called = FALSE;
628
629 /* following functions sometimes call abort proc too */
630 ok(FillRect(print_dc, &rect, (HBRUSH)(COLOR_BACKGROUND + 1)), "FillRect failed\n");
631 ok(EndPage(print_dc) > 0, "EndPage failed\n");
632 ok(EndDoc(print_dc) > 0, "EndDoc failed\n");
633
634 abort_proc_called = FALSE;
635 ok(DeleteDC(print_dc), "DeleteDC failed\n");
636 ok(!abort_proc_called, "AbortProc got called unexpectedly by DeleteDC.\n");
637 abort_proc_called = FALSE;
638
639 end:
640 SetLastError(0xdeadbeef);
641 if(!DeleteFileA(filename))
642 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
643 }
644
645 /* ########################### */
646
647 START_TEST(printdlg)
648 {
649 hcomdlg32 = GetModuleHandleA("comdlg32.dll");
650 pPrintDlgExW = (void *) GetProcAddress(hcomdlg32, "PrintDlgExW");
651
652 test_PageSetupDlgA();
653 test_PrintDlgA();
654 test_PrintDlgExW();
655 test_abort_proc();
656 }