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