mfplay: Implement CreateMediaItemFromObject().
[wine.git] / dlls / comdlg32 / printdlg.c
blob8d5ffc0e972172006fea4ea252d8b27af80b9f87
1 /*
2 * COMMDLG - Print Dialog
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1999 Klaas van Gend
7 * Copyright 2000 Huw D M Davies
8 * Copyright 2010 Vitaly Perov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winspool.h"
39 #include "winerror.h"
40 #include "objbase.h"
41 #include "commdlg.h"
43 #include "wine/debug.h"
45 #include "dlgs.h"
46 #include "cderr.h"
47 #include "cdlg.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
51 /* Yes these constants are the same, but we're just copying win98 */
52 #define UPDOWN_ID 0x270f
53 #define MAX_COPIES 9999
55 /* This PRINTDLGA internal structure stores
56 * pointers to several throughout useful structures.
59 typedef struct
61 LPDEVMODEA lpDevMode;
62 LPPRINTDLGA lpPrintDlg;
63 LPPRINTER_INFO_2A lpPrinterInfo;
64 LPDRIVER_INFO_3A lpDriverInfo;
65 UINT HelpMessageID;
66 HICON hCollateIcon; /* PrintDlg only */
67 HICON hNoCollateIcon; /* PrintDlg only */
68 HICON hPortraitIcon; /* PrintSetupDlg only */
69 HICON hLandscapeIcon; /* PrintSetupDlg only */
70 HWND hwndUpDown;
71 } PRINT_PTRA;
73 typedef struct
75 LPDEVMODEW lpDevMode;
76 LPPRINTDLGW lpPrintDlg;
77 LPPRINTER_INFO_2W lpPrinterInfo;
78 LPDRIVER_INFO_3W lpDriverInfo;
79 UINT HelpMessageID;
80 HICON hCollateIcon; /* PrintDlg only */
81 HICON hNoCollateIcon; /* PrintDlg only */
82 HICON hPortraitIcon; /* PrintSetupDlg only */
83 HICON hLandscapeIcon; /* PrintSetupDlg only */
84 HWND hwndUpDown;
85 } PRINT_PTRW;
87 /* Debugging info */
88 struct pd_flags
90 DWORD flag;
91 LPCSTR name;
94 static const struct pd_flags psd_flags[] = {
95 {PSD_MINMARGINS,"PSD_MINMARGINS"},
96 {PSD_MARGINS,"PSD_MARGINS"},
97 {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
98 {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
99 {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
100 {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
101 {PSD_NOWARNING,"PSD_NOWARNING"},
102 {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
103 {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
104 {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
105 {PSD_SHOWHELP,"PSD_SHOWHELP"},
106 {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
107 {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
108 {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
109 {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
110 {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
111 {-1, NULL}
114 static const struct pd_flags pd_flags[] = {
115 {PD_SELECTION, "PD_SELECTION "},
116 {PD_PAGENUMS, "PD_PAGENUMS "},
117 {PD_NOSELECTION, "PD_NOSELECTION "},
118 {PD_NOPAGENUMS, "PD_NOPAGENUMS "},
119 {PD_COLLATE, "PD_COLLATE "},
120 {PD_PRINTTOFILE, "PD_PRINTTOFILE "},
121 {PD_PRINTSETUP, "PD_PRINTSETUP "},
122 {PD_NOWARNING, "PD_NOWARNING "},
123 {PD_RETURNDC, "PD_RETURNDC "},
124 {PD_RETURNIC, "PD_RETURNIC "},
125 {PD_RETURNDEFAULT, "PD_RETURNDEFAULT "},
126 {PD_SHOWHELP, "PD_SHOWHELP "},
127 {PD_ENABLEPRINTHOOK, "PD_ENABLEPRINTHOOK "},
128 {PD_ENABLESETUPHOOK, "PD_ENABLESETUPHOOK "},
129 {PD_ENABLEPRINTTEMPLATE, "PD_ENABLEPRINTTEMPLATE "},
130 {PD_ENABLESETUPTEMPLATE, "PD_ENABLESETUPTEMPLATE "},
131 {PD_ENABLEPRINTTEMPLATEHANDLE, "PD_ENABLEPRINTTEMPLATEHANDLE "},
132 {PD_ENABLESETUPTEMPLATEHANDLE, "PD_ENABLESETUPTEMPLATEHANDLE "},
133 {PD_USEDEVMODECOPIES, "PD_USEDEVMODECOPIES[ANDCOLLATE] "},
134 {PD_DISABLEPRINTTOFILE, "PD_DISABLEPRINTTOFILE "},
135 {PD_HIDEPRINTTOFILE, "PD_HIDEPRINTTOFILE "},
136 {PD_NONETWORKBUTTON, "PD_NONETWORKBUTTON "},
137 {-1, NULL}
139 /* address of wndproc for subclassed Static control */
140 static WNDPROC lpfnStaticWndProc;
141 static WNDPROC edit_wndproc;
142 /* the text of the fake document to render for the Page Setup dialog */
143 static WCHAR wszFakeDocumentText[1024];
144 static const WCHAR printdlg_prop[] = L"__WINE_PRINTDLGDATA";
145 static const WCHAR pagesetupdlg_prop[] = L"__WINE_PAGESETUPDLGDATA";
148 static LPWSTR strdupW(LPCWSTR p)
150 LPWSTR ret;
151 DWORD len;
153 if(!p) return NULL;
154 len = (lstrlenW(p) + 1) * sizeof(WCHAR);
155 ret = HeapAlloc(GetProcessHeap(), 0, len);
156 memcpy(ret, p, len);
157 return ret;
160 /***********************************************************************
161 * get_driver_info [internal]
163 * get DRIVER_INFO_3W for the current printer handle,
164 * alloc the buffer, when needed
166 static DRIVER_INFO_3W * get_driver_infoW(HANDLE hprn)
168 DRIVER_INFO_3W *di3 = NULL;
169 DWORD needed = 0;
170 BOOL res;
172 res = GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
173 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
174 di3 = HeapAlloc(GetProcessHeap(), 0, needed);
175 res = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)di3, needed, &needed);
178 if (res)
179 return di3;
181 TRACE("GetPrinterDriverW failed with %u\n", GetLastError());
182 HeapFree(GetProcessHeap(), 0, di3);
183 return NULL;
186 static DRIVER_INFO_3A * get_driver_infoA(HANDLE hprn)
188 DRIVER_INFO_3A *di3 = NULL;
189 DWORD needed = 0;
190 BOOL res;
192 res = GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
193 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
194 di3 = HeapAlloc(GetProcessHeap(), 0, needed);
195 res = GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)di3, needed, &needed);
198 if (res)
199 return di3;
201 TRACE("GetPrinterDriverA failed with %u\n", GetLastError());
202 HeapFree(GetProcessHeap(), 0, di3);
203 return NULL;
207 /***********************************************************************
208 * get_printer_info [internal]
210 * get PRINTER_INFO_2W for the current printer handle,
211 * alloc the buffer, when needed
213 static PRINTER_INFO_2W * get_printer_infoW(HANDLE hprn)
215 PRINTER_INFO_2W *pi2 = NULL;
216 DWORD needed = 0;
217 BOOL res;
219 res = GetPrinterW(hprn, 2, NULL, 0, &needed);
220 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
221 pi2 = HeapAlloc(GetProcessHeap(), 0, needed);
222 res = GetPrinterW(hprn, 2, (LPBYTE)pi2, needed, &needed);
225 if (res)
226 return pi2;
228 TRACE("GetPrinterW failed with %u\n", GetLastError());
229 HeapFree(GetProcessHeap(), 0, pi2);
230 return NULL;
233 static PRINTER_INFO_2A * get_printer_infoA(HANDLE hprn)
235 PRINTER_INFO_2A *pi2 = NULL;
236 DWORD needed = 0;
237 BOOL res;
239 res = GetPrinterA(hprn, 2, NULL, 0, &needed);
240 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
241 pi2 = HeapAlloc(GetProcessHeap(), 0, needed);
242 res = GetPrinterA(hprn, 2, (LPBYTE)pi2, needed, &needed);
245 if (res)
246 return pi2;
248 TRACE("GetPrinterA failed with %u\n", GetLastError());
249 HeapFree(GetProcessHeap(), 0, pi2);
250 return NULL;
254 /***********************************************************************
255 * update_devmode_handle [internal]
257 * update a devmode handle for the given DEVMODE, alloc the buffer, when needed
259 static HGLOBAL update_devmode_handleW(HGLOBAL hdm, DEVMODEW *dm)
261 SIZE_T size = GlobalSize(hdm);
262 LPVOID ptr;
264 /* Increase / alloc the global memory block, when needed */
265 if ((dm->dmSize + dm->dmDriverExtra) > size) {
266 if (hdm)
267 hdm = GlobalReAlloc(hdm, dm->dmSize + dm->dmDriverExtra, 0);
268 else
269 hdm = GlobalAlloc(GMEM_MOVEABLE, dm->dmSize + dm->dmDriverExtra);
272 if (hdm) {
273 ptr = GlobalLock(hdm);
274 if (ptr) {
275 memcpy(ptr, dm, dm->dmSize + dm->dmDriverExtra);
276 GlobalUnlock(hdm);
278 else
280 GlobalFree(hdm);
281 hdm = NULL;
284 return hdm;
287 static HGLOBAL update_devmode_handleA(HGLOBAL hdm, DEVMODEA *dm)
289 SIZE_T size = GlobalSize(hdm);
290 LPVOID ptr;
292 /* Increase / alloc the global memory block, when needed */
293 if ((dm->dmSize + dm->dmDriverExtra) > size) {
294 if (hdm)
295 hdm = GlobalReAlloc(hdm, dm->dmSize + dm->dmDriverExtra, 0);
296 else
297 hdm = GlobalAlloc(GMEM_MOVEABLE, dm->dmSize + dm->dmDriverExtra);
300 if (hdm) {
301 ptr = GlobalLock(hdm);
302 if (ptr) {
303 memcpy(ptr, dm, dm->dmSize + dm->dmDriverExtra);
304 GlobalUnlock(hdm);
306 else
308 GlobalFree(hdm);
309 hdm = NULL;
312 return hdm;
315 /***********************************************************
316 * convert_to_devmodeA
318 * Creates an ansi copy of supplied devmode
320 static DEVMODEA *convert_to_devmodeA(const DEVMODEW *dmW)
322 DEVMODEA *dmA;
323 DWORD size;
325 if (!dmW) return NULL;
326 size = dmW->dmSize - CCHDEVICENAME -
327 ((dmW->dmSize > FIELD_OFFSET(DEVMODEW, dmFormName)) ? CCHFORMNAME : 0);
329 dmA = HeapAlloc(GetProcessHeap(), 0, size + dmW->dmDriverExtra);
330 if (!dmA) return NULL;
332 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1,
333 (LPSTR)dmA->dmDeviceName, CCHDEVICENAME, NULL, NULL);
335 if (FIELD_OFFSET(DEVMODEW, dmFormName) >= dmW->dmSize)
337 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
338 dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
340 else
342 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
343 FIELD_OFFSET(DEVMODEW, dmFormName) - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
344 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1,
345 (LPSTR)dmA->dmFormName, CCHFORMNAME, NULL, NULL);
347 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmLogPixels));
350 dmA->dmSize = size;
351 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize, dmW->dmDriverExtra);
352 return dmA;
355 /***********************************************************************
356 * PRINTDLG_OpenDefaultPrinter
358 * Returns a winspool printer handle to the default printer in *hprn
359 * Caller must call ClosePrinter on the handle
361 * Returns TRUE on success else FALSE
363 static BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
365 WCHAR buf[260];
366 DWORD dwBufLen = ARRAY_SIZE(buf);
367 BOOL res;
368 if(!GetDefaultPrinterW(buf, &dwBufLen))
369 return FALSE;
370 res = OpenPrinterW(buf, hprn, NULL);
371 if (!res)
372 WARN("Could not open printer %s\n", debugstr_w(buf));
373 return res;
376 /***********************************************************************
377 * PRINTDLG_SetUpPrinterListCombo
379 * Initializes printer list combox.
380 * hDlg: HWND of dialog
381 * id: Control id of combo
382 * name: Name of printer to select
384 * Initializes combo with list of available printers. Selects printer 'name'
385 * If name is NULL or does not exist select the default printer.
387 * Returns number of printers added to list.
389 static INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
391 DWORD needed, num;
392 INT i;
393 LPPRINTER_INFO_2A pi;
394 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
395 pi = HeapAlloc(GetProcessHeap(), 0, needed);
396 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
397 &num);
399 SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
401 for(i = 0; i < num; i++) {
402 SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
403 (LPARAM)pi[i].pPrinterName );
405 HeapFree(GetProcessHeap(), 0, pi);
406 if(!name ||
407 (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
408 (LPARAM)name)) == CB_ERR) {
410 char buf[260];
411 DWORD dwBufLen = ARRAY_SIZE(buf);
412 if (name != NULL)
413 WARN("Can't find %s in printer list so trying to find default\n",
414 debugstr_a(name));
415 if(!GetDefaultPrinterA(buf, &dwBufLen))
416 return num;
417 i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
418 if(i == CB_ERR)
419 FIXME("Can't find default printer in printer list\n");
421 SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
422 return num;
425 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
427 DWORD needed, num;
428 INT i;
429 LPPRINTER_INFO_2W pi;
430 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
431 pi = HeapAlloc(GetProcessHeap(), 0, needed);
432 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
433 &num);
435 for(i = 0; i < num; i++) {
436 SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
437 (LPARAM)pi[i].pPrinterName );
439 HeapFree(GetProcessHeap(), 0, pi);
440 if(!name ||
441 (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
442 (LPARAM)name)) == CB_ERR) {
443 WCHAR buf[260];
444 DWORD dwBufLen = ARRAY_SIZE(buf);
445 if (name != NULL)
446 WARN("Can't find %s in printer list so trying to find default\n",
447 debugstr_w(name));
448 if(!GetDefaultPrinterW(buf, &dwBufLen))
449 return num;
450 i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
451 if(i == CB_ERR)
452 TRACE("Can't find default printer in printer list\n");
454 SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
455 return num;
458 /***********************************************************************
459 * PRINTDLG_CreateDevNames [internal]
462 * creates a DevNames structure.
464 * (NB. when we handle unicode the offsets will be in wchars).
466 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
467 const char* DeviceName, const char* OutputPort)
469 long size;
470 char* pDevNamesSpace;
471 char* pTempPtr;
472 LPDEVNAMES lpDevNames;
473 char buf[260];
474 DWORD dwBufLen = ARRAY_SIZE(buf);
475 const char *p;
477 p = strrchr( DeviceDriverName, '\\' );
478 if (p) DeviceDriverName = p + 1;
480 size = strlen(DeviceDriverName) + 1
481 + strlen(DeviceName) + 1
482 + strlen(OutputPort) + 1
483 + sizeof(DEVNAMES);
485 if(*hmem)
486 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
487 else
488 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
489 if (*hmem == 0)
490 return FALSE;
492 pDevNamesSpace = GlobalLock(*hmem);
493 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
495 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
496 strcpy(pTempPtr, DeviceDriverName);
497 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
499 pTempPtr += strlen(DeviceDriverName) + 1;
500 strcpy(pTempPtr, DeviceName);
501 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
503 pTempPtr += strlen(DeviceName) + 1;
504 strcpy(pTempPtr, OutputPort);
505 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
507 GetDefaultPrinterA(buf, &dwBufLen);
508 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
509 GlobalUnlock(*hmem);
510 return TRUE;
513 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
514 LPCWSTR DeviceName, LPCWSTR OutputPort)
516 long size;
517 LPWSTR pDevNamesSpace;
518 LPWSTR pTempPtr;
519 LPDEVNAMES lpDevNames;
520 WCHAR bufW[260];
521 DWORD dwBufLen = ARRAY_SIZE(bufW);
522 const WCHAR *p;
524 p = wcsrchr( DeviceDriverName, '\\' );
525 if (p) DeviceDriverName = p + 1;
527 size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
528 + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
529 + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
530 + sizeof(DEVNAMES);
532 if(*hmem)
533 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
534 else
535 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
536 if (*hmem == 0)
537 return FALSE;
539 pDevNamesSpace = GlobalLock(*hmem);
540 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
542 pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
543 lstrcpyW(pTempPtr, DeviceDriverName);
544 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
546 pTempPtr += lstrlenW(DeviceDriverName) + 1;
547 lstrcpyW(pTempPtr, DeviceName);
548 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
550 pTempPtr += lstrlenW(DeviceName) + 1;
551 lstrcpyW(pTempPtr, OutputPort);
552 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
554 GetDefaultPrinterW(bufW, &dwBufLen);
555 lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
556 GlobalUnlock(*hmem);
557 return TRUE;
560 /***********************************************************************
561 * PRINTDLG_UpdatePrintDlg [internal]
564 * updates the PrintDlg structure for return values.
566 * RETURNS
567 * FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
568 * TRUE if successful.
570 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
571 PRINT_PTRA* PrintStructures)
573 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
574 PDEVMODEA lpdm = PrintStructures->lpDevMode;
575 LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
578 if(!lpdm) {
579 FIXME("No lpdm ptr?\n");
580 return FALSE;
584 if(!(lppd->Flags & PD_PRINTSETUP)) {
585 /* check whether nFromPage and nToPage are within range defined by
586 * nMinPage and nMaxPage
588 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
589 WORD nToPage;
590 WORD nFromPage;
591 BOOL translated;
592 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
593 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
595 /* if no ToPage value is entered, use the FromPage value */
596 if(!translated) nToPage = nFromPage;
598 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
599 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
600 WCHAR resourcestr[256];
601 WCHAR resultstr[256];
602 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
603 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
604 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
605 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
606 return FALSE;
608 lppd->nFromPage = nFromPage;
609 lppd->nToPage = nToPage;
610 lppd->Flags |= PD_PAGENUMS;
612 else
613 lppd->Flags &= ~PD_PAGENUMS;
615 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
616 lppd->Flags |= PD_SELECTION;
617 else
618 lppd->Flags &= ~PD_SELECTION;
620 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
621 static char file[] = "FILE:";
622 lppd->Flags |= PD_PRINTTOFILE;
623 pi->pPortName = file;
626 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
627 FIXME("Collate lppd not yet implemented as output\n");
630 /* set PD_Collate and nCopies */
631 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
632 /* The application doesn't support multiple copies or collate...
634 lppd->Flags &= ~PD_COLLATE;
635 lppd->nCopies = 1;
636 /* if the printer driver supports it... store info there
637 * otherwise no collate & multiple copies !
639 if (lpdm->dmFields & DM_COLLATE)
640 lpdm->dmCollate =
641 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
642 if (lpdm->dmFields & DM_COPIES)
643 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
644 } else {
645 /* Application is responsible for multiple copies */
646 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
647 lppd->Flags |= PD_COLLATE;
648 else
649 lppd->Flags &= ~PD_COLLATE;
650 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
651 /* multiple copies already included in the document. Driver must print only one copy */
652 lpdm->u1.s1.dmCopies = 1;
655 /* Print quality, PrintDlg16 */
656 if(GetDlgItem(hDlg, cmb1))
658 HWND hQuality = GetDlgItem(hDlg, cmb1);
659 int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
661 if(Sel != CB_ERR)
663 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
664 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
665 lpdm->u1.s1.dmPrintQuality = LOWORD(dpi);
666 lpdm->dmYResolution = HIWORD(dpi);
670 return TRUE;
673 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
674 PRINT_PTRW* PrintStructures)
676 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
677 PDEVMODEW lpdm = PrintStructures->lpDevMode;
678 LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
681 if(!lpdm) {
682 FIXME("No lpdm ptr?\n");
683 return FALSE;
687 if(!(lppd->Flags & PD_PRINTSETUP)) {
688 /* check whether nFromPage and nToPage are within range defined by
689 * nMinPage and nMaxPage
691 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
692 WORD nToPage;
693 WORD nFromPage;
694 BOOL translated;
695 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
696 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
698 /* if no ToPage value is entered, use the FromPage value */
699 if(!translated) nToPage = nFromPage;
701 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
702 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
703 WCHAR resourcestr[256];
704 WCHAR resultstr[256];
705 DWORD_PTR args[2];
706 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
707 resourcestr, 255);
708 args[0] = lppd->nMinPage;
709 args[1] = lppd->nMaxPage;
710 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
711 resourcestr, 0, 0, resultstr,
712 ARRAY_SIZE(resultstr),
713 (__ms_va_list*)args);
714 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
715 resourcestr, 255);
716 MessageBoxW(hDlg, resultstr, resourcestr,
717 MB_OK | MB_ICONWARNING);
718 return FALSE;
720 lppd->nFromPage = nFromPage;
721 lppd->nToPage = nToPage;
722 lppd->Flags |= PD_PAGENUMS;
724 else
725 lppd->Flags &= ~PD_PAGENUMS;
727 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
728 lppd->Flags |= PD_SELECTION;
729 else
730 lppd->Flags &= ~PD_SELECTION;
732 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
733 static WCHAR file[] = L"FILE:";
734 lppd->Flags |= PD_PRINTTOFILE;
735 pi->pPortName = file;
738 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
739 FIXME("Collate lppd not yet implemented as output\n");
742 /* set PD_Collate and nCopies */
743 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
744 /* The application doesn't support multiple copies or collate...
746 lppd->Flags &= ~PD_COLLATE;
747 lppd->nCopies = 1;
748 /* if the printer driver supports it... store info there
749 * otherwise no collate & multiple copies !
751 if (lpdm->dmFields & DM_COLLATE)
752 lpdm->dmCollate =
753 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
754 if (lpdm->dmFields & DM_COPIES)
755 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
756 } else {
757 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
758 lppd->Flags |= PD_COLLATE;
759 else
760 lppd->Flags &= ~PD_COLLATE;
761 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
764 return TRUE;
767 /************************************************************************
768 * PRINTDLG_SetUpPaperComboBox
770 * Initialize either the papersize or inputslot combos of the Printer Setup
771 * dialog. We store the associated word (eg DMPAPER_A4) as the item data.
772 * We also try to re-select the old selection.
774 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
775 int nIDComboBox,
776 char* PrinterName,
777 char* PortName,
778 LPDEVMODEA dm)
780 int i;
781 int NrOfEntries;
782 char* Names;
783 WORD* Words;
784 DWORD Sel, old_Sel;
785 WORD oldWord = 0, newWord = 0; /* DMPAPER_ and DMBIN_ start at 1 */
786 int NamesSize;
787 int fwCapability_Names;
788 int fwCapability_Words;
790 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
792 /* query the dialog box for the current selected value */
793 Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
794 if(Sel != CB_ERR) {
795 /* we enter here only if a different printer is selected after
796 * the Print Setup dialog is opened. The current settings are
797 * stored into the newly selected printer.
799 oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
800 Sel, 0);
801 if(oldWord >= DMPAPER_USER) /* DMPAPER_USER == DMBIN_USER */
802 oldWord = 0; /* There's no point in trying to keep custom
803 paper / bin sizes across printers */
806 if (dm)
807 newWord = (nIDComboBox == cmb2) ? dm->u1.s1.dmPaperSize : dm->u1.s1.dmDefaultSource;
809 if (nIDComboBox == cmb2) {
810 NamesSize = 64;
811 fwCapability_Names = DC_PAPERNAMES;
812 fwCapability_Words = DC_PAPERS;
813 } else {
814 nIDComboBox = cmb3;
815 NamesSize = 24;
816 fwCapability_Names = DC_BINNAMES;
817 fwCapability_Words = DC_BINS;
820 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
821 fwCapability_Names, NULL, dm);
822 if (NrOfEntries == 0)
823 WARN("no Name Entries found!\n");
824 else if (NrOfEntries < 0)
825 return FALSE;
827 if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
828 != NrOfEntries) {
829 ERR("Number of caps is different\n");
830 NrOfEntries = 0;
833 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
834 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
835 DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Names, Names, dm);
836 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
837 fwCapability_Words, (LPSTR)Words, dm);
839 /* reset any current content in the combobox */
840 SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
842 /* store new content */
843 for (i = 0; i < NrOfEntries; i++) {
844 DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
845 (LPARAM)(&Names[i*NamesSize]) );
846 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
847 Words[i]);
850 /* Look for old selection or the new default.
851 Can't do this is previous loop since item order will change as more items are added */
852 Sel = 0;
853 old_Sel = NrOfEntries;
854 for (i = 0; i < NrOfEntries; i++) {
855 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
856 oldWord) {
857 old_Sel = i;
858 break;
860 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) == newWord)
861 Sel = i;
864 if(old_Sel < NrOfEntries)
866 if (dm)
868 if(nIDComboBox == cmb2)
869 dm->u1.s1.dmPaperSize = oldWord;
870 else
871 dm->u1.s1.dmDefaultSource = oldWord;
873 Sel = old_Sel;
876 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
878 HeapFree(GetProcessHeap(),0,Words);
879 HeapFree(GetProcessHeap(),0,Names);
880 return TRUE;
883 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
884 int nIDComboBox,
885 const WCHAR* PrinterName,
886 const WCHAR* PortName,
887 LPDEVMODEW dm)
889 int i;
890 int NrOfEntries;
891 WCHAR* Names;
892 WORD* Words;
893 DWORD Sel, old_Sel;
894 WORD oldWord = 0, newWord = 0; /* DMPAPER_ and DMBIN_ start at 1 */
895 int NamesSize;
896 int fwCapability_Names;
897 int fwCapability_Words;
899 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
901 /* query the dialog box for the current selected value */
902 Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
903 if(Sel != CB_ERR) {
904 /* we enter here only if a different printer is selected after
905 * the Print Setup dialog is opened. The current settings are
906 * stored into the newly selected printer.
908 oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
909 Sel, 0);
911 if(oldWord >= DMPAPER_USER) /* DMPAPER_USER == DMBIN_USER */
912 oldWord = 0; /* There's no point in trying to keep custom
913 paper / bin sizes across printers */
916 if (dm)
917 newWord = (nIDComboBox == cmb2) ? dm->u1.s1.dmPaperSize : dm->u1.s1.dmDefaultSource;
919 if (nIDComboBox == cmb2) {
920 NamesSize = 64;
921 fwCapability_Names = DC_PAPERNAMES;
922 fwCapability_Words = DC_PAPERS;
923 } else {
924 nIDComboBox = cmb3;
925 NamesSize = 24;
926 fwCapability_Names = DC_BINNAMES;
927 fwCapability_Words = DC_BINS;
930 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
931 fwCapability_Names, NULL, dm);
932 if (NrOfEntries == 0)
933 WARN("no Name Entries found!\n");
934 else if (NrOfEntries < 0)
935 return FALSE;
937 if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
938 != NrOfEntries) {
939 ERR("Number of caps is different\n");
940 NrOfEntries = 0;
943 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
944 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
945 DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Names, Names, dm);
946 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
947 fwCapability_Words, Words, dm);
949 /* reset any current content in the combobox */
950 SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
952 /* store new content */
953 for (i = 0; i < NrOfEntries; i++) {
954 DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
955 (LPARAM)(&Names[i*NamesSize]) );
956 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
957 Words[i]);
960 /* Look for old selection or the new default.
961 Can't do this is previous loop since item order will change as more items are added */
962 Sel = 0;
963 old_Sel = NrOfEntries;
964 for (i = 0; i < NrOfEntries; i++) {
965 if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
966 oldWord) {
967 old_Sel = i;
968 break;
970 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) == newWord)
971 Sel = i;
974 if(old_Sel < NrOfEntries)
976 if (dm)
978 if(nIDComboBox == cmb2)
979 dm->u1.s1.dmPaperSize = oldWord;
980 else
981 dm->u1.s1.dmDefaultSource = oldWord;
983 Sel = old_Sel;
986 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
988 HeapFree(GetProcessHeap(),0,Words);
989 HeapFree(GetProcessHeap(),0,Names);
990 return TRUE;
994 /***********************************************************************
995 * PRINTDLG_UpdatePrinterInfoTexts [internal]
997 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
999 char StatusMsg[256];
1000 char ResourceString[256];
1001 int i;
1003 /* Status Message */
1004 StatusMsg[0]='\0';
1006 /* add all status messages */
1007 for (i = 0; i < 25; i++) {
1008 if (pi->Status & (1<<i)) {
1009 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
1010 ResourceString, 255);
1011 strcat(StatusMsg,ResourceString);
1014 /* append "ready" */
1015 /* FIXME: status==ready must only be appended if really so.
1016 but how to detect? */
1017 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
1018 ResourceString, 255);
1019 strcat(StatusMsg,ResourceString);
1020 SetDlgItemTextA(hDlg, stc12, StatusMsg);
1022 /* set all other printer info texts */
1023 SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
1025 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
1026 SetDlgItemTextA(hDlg, stc14, pi->pLocation);
1027 else
1028 SetDlgItemTextA(hDlg, stc14, pi->pPortName);
1029 SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
1030 return;
1033 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
1035 WCHAR StatusMsg[256];
1036 WCHAR ResourceString[256];
1037 int i;
1039 /* Status Message */
1040 StatusMsg[0]='\0';
1042 /* add all status messages */
1043 for (i = 0; i < 25; i++) {
1044 if (pi->Status & (1<<i)) {
1045 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
1046 ResourceString, 255);
1047 lstrcatW(StatusMsg,ResourceString);
1050 /* append "ready" */
1051 /* FIXME: status==ready must only be appended if really so.
1052 but how to detect? */
1053 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
1054 ResourceString, 255);
1055 lstrcatW(StatusMsg,ResourceString);
1056 SetDlgItemTextW(hDlg, stc12, StatusMsg);
1058 /* set all other printer info texts */
1059 SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
1060 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
1061 SetDlgItemTextW(hDlg, stc14, pi->pLocation);
1062 else
1063 SetDlgItemTextW(hDlg, stc14, pi->pPortName);
1064 SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : L"");
1068 /*******************************************************************
1070 * PRINTDLG_ChangePrinter
1073 static BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name, PRINT_PTRA *PrintStructures)
1075 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1076 LPDEVMODEA lpdm = NULL;
1077 LONG dmSize;
1078 DWORD needed;
1079 HANDLE hprn;
1081 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1082 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1083 if(!OpenPrinterA(name, &hprn, NULL)) {
1084 ERR("Can't open printer %s\n", name);
1085 return FALSE;
1087 GetPrinterA(hprn, 2, NULL, 0, &needed);
1088 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
1089 GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1090 &needed);
1091 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1092 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
1093 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1094 needed, &needed)) {
1095 ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
1096 return FALSE;
1098 ClosePrinter(hprn);
1100 PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
1102 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1103 PrintStructures->lpDevMode = NULL;
1105 dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
1106 if(dmSize == -1) {
1107 ERR("DocumentProperties fails on %s\n", debugstr_a(name));
1108 return FALSE;
1110 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1111 dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
1112 DM_OUT_BUFFER);
1113 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1114 !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
1115 (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
1116 /* Supplied devicemode matches current printer so try to use it */
1117 DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
1118 DM_OUT_BUFFER | DM_IN_BUFFER);
1120 if(lpdm)
1121 GlobalUnlock(lppd->hDevMode);
1123 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1125 if(!(lppd->Flags & PD_PRINTSETUP)) {
1126 /* Print range (All/Range/Selection) */
1127 if(lppd->nFromPage != 0xffff)
1128 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1129 if(lppd->nToPage != 0xffff)
1130 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1132 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1133 if (lppd->Flags & PD_NOSELECTION)
1134 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1135 else
1136 if (lppd->Flags & PD_SELECTION)
1137 CheckRadioButton(hDlg, rad1, rad3, rad2);
1138 if (lppd->Flags & PD_NOPAGENUMS) {
1139 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1140 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1141 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1142 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1143 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1144 } else {
1145 if (lppd->Flags & PD_PAGENUMS)
1146 CheckRadioButton(hDlg, rad1, rad3, rad3);
1149 /* Collate pages
1151 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1153 if (lppd->Flags & PD_COLLATE) {
1154 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1155 (LPARAM)PrintStructures->hCollateIcon);
1156 CheckDlgButton(hDlg, chx2, 1);
1157 } else {
1158 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1159 (LPARAM)PrintStructures->hNoCollateIcon);
1160 CheckDlgButton(hDlg, chx2, 0);
1163 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1164 /* if printer doesn't support it: no Collate */
1165 if (!(lpdm->dmFields & DM_COLLATE)) {
1166 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1167 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1171 /* nCopies */
1173 INT copies;
1174 if (lppd->hDevMode == 0)
1175 copies = lppd->nCopies;
1176 else
1177 copies = lpdm->u1.s1.dmCopies;
1178 if(copies == 0) copies = 1;
1179 else if(copies < 0) copies = MAX_COPIES;
1180 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1183 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1184 /* if printer doesn't support it: no nCopies */
1185 if (!(lpdm->dmFields & DM_COPIES)) {
1186 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1187 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1191 /* print to file */
1192 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1193 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1194 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1195 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1196 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1198 /* Fill print quality combo, PrintDlg16 */
1199 if(GetDlgItem(hDlg, cmb1))
1201 DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1202 PrintStructures->lpPrinterInfo->pPortName,
1203 DC_ENUMRESOLUTIONS, NULL, lpdm);
1205 if(numResolutions != -1)
1207 HWND hQuality = GetDlgItem(hDlg, cmb1);
1208 LONG* Resolutions;
1209 char buf[255];
1210 DWORD i;
1211 int dpiX, dpiY;
1212 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
1213 PrintStructures->lpPrinterInfo->pPrinterName,
1214 0, lpdm);
1216 Resolutions = HeapAlloc(GetProcessHeap(), 0, numResolutions*sizeof(LONG)*2);
1217 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1218 PrintStructures->lpPrinterInfo->pPortName,
1219 DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
1221 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
1222 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
1223 DeleteDC(hPrinterDC);
1225 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
1226 for(i = 0; i < (numResolutions * 2); i += 2)
1228 BOOL IsDefault = FALSE;
1229 LRESULT Index;
1231 if(Resolutions[i] == Resolutions[i+1])
1233 if(dpiX == Resolutions[i])
1234 IsDefault = TRUE;
1235 sprintf(buf, "%d dpi", Resolutions[i]);
1236 } else
1238 if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1239 IsDefault = TRUE;
1240 sprintf(buf, "%d dpi x %d dpi", Resolutions[i], Resolutions[i+1]);
1243 Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1245 if(IsDefault)
1246 SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1248 SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1250 HeapFree(GetProcessHeap(), 0, Resolutions);
1253 } else { /* PD_PRINTSETUP */
1254 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1256 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1257 PrintStructures->lpPrinterInfo->pPrinterName,
1258 PrintStructures->lpPrinterInfo->pPortName,
1259 lpdm);
1260 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1261 PrintStructures->lpPrinterInfo->pPrinterName,
1262 PrintStructures->lpPrinterInfo->pPortName,
1263 lpdm);
1264 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1265 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1266 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1267 PrintStructures->hLandscapeIcon));
1271 /* help button */
1272 if ((lppd->Flags & PD_SHOWHELP)==0) {
1273 /* hide if PD_SHOWHELP not specified */
1274 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1276 return TRUE;
1279 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1280 PRINT_PTRW *PrintStructures)
1282 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1283 LPDEVMODEW lpdm = NULL;
1284 LONG dmSize;
1285 DWORD needed;
1286 HANDLE hprn;
1288 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1289 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1290 if(!OpenPrinterW(name, &hprn, NULL)) {
1291 ERR("Can't open printer %s\n", debugstr_w(name));
1292 return FALSE;
1294 GetPrinterW(hprn, 2, NULL, 0, &needed);
1295 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
1296 GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1297 &needed);
1298 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1299 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
1300 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1301 needed, &needed)) {
1302 ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1303 return FALSE;
1305 ClosePrinter(hprn);
1307 PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1309 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1310 PrintStructures->lpDevMode = NULL;
1312 dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1313 if(dmSize == -1) {
1314 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1315 return FALSE;
1317 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1318 dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1319 DM_OUT_BUFFER);
1320 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1321 !lstrcmpW(lpdm->dmDeviceName,
1322 PrintStructures->lpDevMode->dmDeviceName)) {
1323 /* Supplied devicemode matches current printer so try to use it */
1324 DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1325 DM_OUT_BUFFER | DM_IN_BUFFER);
1327 if(lpdm)
1328 GlobalUnlock(lppd->hDevMode);
1330 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1332 if(!(lppd->Flags & PD_PRINTSETUP)) {
1333 /* Print range (All/Range/Selection) */
1334 if(lppd->nFromPage != 0xffff)
1335 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1336 if(lppd->nToPage != 0xffff)
1337 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1339 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1340 if (lppd->Flags & PD_NOSELECTION)
1341 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1342 else
1343 if (lppd->Flags & PD_SELECTION)
1344 CheckRadioButton(hDlg, rad1, rad3, rad2);
1345 if (lppd->Flags & PD_NOPAGENUMS) {
1346 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1347 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1348 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1349 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1350 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1351 } else {
1352 if (lppd->Flags & PD_PAGENUMS)
1353 CheckRadioButton(hDlg, rad1, rad3, rad3);
1356 /* Collate pages
1358 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1360 if (lppd->Flags & PD_COLLATE) {
1361 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1362 (LPARAM)PrintStructures->hCollateIcon);
1363 CheckDlgButton(hDlg, chx2, 1);
1364 } else {
1365 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1366 (LPARAM)PrintStructures->hNoCollateIcon);
1367 CheckDlgButton(hDlg, chx2, 0);
1370 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1371 /* if printer doesn't support it: no Collate */
1372 if (!(lpdm->dmFields & DM_COLLATE)) {
1373 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1374 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1378 /* nCopies */
1380 INT copies;
1381 if (lppd->hDevMode == 0)
1382 copies = lppd->nCopies;
1383 else
1384 copies = lpdm->u1.s1.dmCopies;
1385 if(copies == 0) copies = 1;
1386 else if(copies < 0) copies = MAX_COPIES;
1387 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1390 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1391 /* if printer doesn't support it: no nCopies */
1392 if (!(lpdm->dmFields & DM_COPIES)) {
1393 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1394 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1398 /* print to file */
1399 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1400 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1401 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1402 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1403 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1405 } else { /* PD_PRINTSETUP */
1406 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1408 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1409 PrintStructures->lpPrinterInfo->pPrinterName,
1410 PrintStructures->lpPrinterInfo->pPortName,
1411 lpdm);
1412 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1413 PrintStructures->lpPrinterInfo->pPrinterName,
1414 PrintStructures->lpPrinterInfo->pPortName,
1415 lpdm);
1416 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1417 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1418 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1419 PrintStructures->hLandscapeIcon));
1423 /* help button */
1424 if ((lppd->Flags & PD_SHOWHELP)==0) {
1425 /* hide if PD_SHOWHELP not specified */
1426 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1428 return TRUE;
1431 /***********************************************************************
1432 * check_printer_setup [internal]
1434 static LRESULT check_printer_setup(HWND hDlg)
1436 DWORD needed,num;
1437 WCHAR resourcestr[256],resultstr[256];
1439 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1440 if(needed == 0)
1442 EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1444 if(needed > 0)
1445 return TRUE;
1446 else
1448 LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1449 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1450 MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1451 return FALSE;
1455 /***********************************************************************
1456 * PRINTDLG_WMInitDialog [internal]
1458 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg,
1459 PRINT_PTRA* PrintStructures)
1461 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1462 DEVNAMES *pdn;
1463 DEVMODEA *pdm;
1464 char *name = NULL;
1465 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1467 /* load Collate ICONs */
1468 /* We load these with LoadImage because they are not a standard
1469 size and we don't want them rescaled */
1470 PrintStructures->hCollateIcon =
1471 LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1472 PrintStructures->hNoCollateIcon =
1473 LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1475 /* These can be done with LoadIcon */
1476 PrintStructures->hPortraitIcon =
1477 LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1478 PrintStructures->hLandscapeIcon =
1479 LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1481 /* display the collate/no_collate icon */
1482 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1483 (LPARAM)PrintStructures->hNoCollateIcon);
1485 if(PrintStructures->hCollateIcon == 0 ||
1486 PrintStructures->hNoCollateIcon == 0 ||
1487 PrintStructures->hPortraitIcon == 0 ||
1488 PrintStructures->hLandscapeIcon == 0) {
1489 ERR("no icon in resource file\n");
1490 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1491 EndDialog(hDlg, FALSE);
1495 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1496 * must be registered and the Help button must be shown.
1498 if (lppd->Flags & PD_SHOWHELP) {
1499 if((PrintStructures->HelpMessageID =
1500 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1501 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1502 return FALSE;
1504 } else
1505 PrintStructures->HelpMessageID = 0;
1507 if(!(lppd->Flags &PD_PRINTSETUP)) {
1508 PrintStructures->hwndUpDown =
1509 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1510 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1511 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1512 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1513 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1516 /* FIXME: I allow more freedom than either Win95 or WinNT,
1517 * which do not agree on what errors should be thrown or not
1518 * in case nToPage or nFromPage is out-of-range.
1520 if (lppd->nMaxPage < lppd->nMinPage)
1521 lppd->nMaxPage = lppd->nMinPage;
1522 if (lppd->nMinPage == lppd->nMaxPage)
1523 lppd->Flags |= PD_NOPAGENUMS;
1524 if (lppd->nToPage < lppd->nMinPage)
1525 lppd->nToPage = lppd->nMinPage;
1526 if (lppd->nToPage > lppd->nMaxPage)
1527 lppd->nToPage = lppd->nMaxPage;
1528 if (lppd->nFromPage < lppd->nMinPage)
1529 lppd->nFromPage = lppd->nMinPage;
1530 if (lppd->nFromPage > lppd->nMaxPage)
1531 lppd->nFromPage = lppd->nMaxPage;
1533 /* if we have the combo box, fill it */
1534 if (GetDlgItem(hDlg,comboID)) {
1535 /* Fill Combobox
1537 pdn = GlobalLock(lppd->hDevNames);
1538 pdm = GlobalLock(lppd->hDevMode);
1539 if(pdn)
1540 name = (char*)pdn + pdn->wDeviceOffset;
1541 else if(pdm)
1542 name = (char*)pdm->dmDeviceName;
1543 PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1544 if(pdm) GlobalUnlock(lppd->hDevMode);
1545 if(pdn) GlobalUnlock(lppd->hDevNames);
1547 /* Now find selected printer and update rest of dlg */
1548 name = HeapAlloc(GetProcessHeap(),0,256);
1549 if (GetDlgItemTextA(hDlg, comboID, name, 255))
1550 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1551 HeapFree(GetProcessHeap(),0,name);
1552 } else {
1553 /* else use default printer */
1554 char name[200];
1555 DWORD dwBufLen = ARRAY_SIZE(name);
1556 BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1558 if (ret)
1559 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1560 else
1561 FIXME("No default printer found, expect problems!\n");
1563 return TRUE;
1566 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg,
1567 PRINT_PTRW* PrintStructures)
1569 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1570 DEVNAMES *pdn;
1571 DEVMODEW *pdm;
1572 WCHAR *name = NULL;
1573 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1575 /* load Collate ICONs */
1576 /* We load these with LoadImage because they are not a standard
1577 size and we don't want them rescaled */
1578 PrintStructures->hCollateIcon =
1579 LoadImageW(COMDLG32_hInstance, L"PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1580 PrintStructures->hNoCollateIcon =
1581 LoadImageW(COMDLG32_hInstance, L"PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1583 /* These can be done with LoadIcon */
1584 PrintStructures->hPortraitIcon =
1585 LoadIconW(COMDLG32_hInstance, L"PD32_PORTRAIT");
1586 PrintStructures->hLandscapeIcon =
1587 LoadIconW(COMDLG32_hInstance, L"PD32_LANDSCAPE");
1589 /* display the collate/no_collate icon */
1590 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1591 (LPARAM)PrintStructures->hNoCollateIcon);
1593 if(PrintStructures->hCollateIcon == 0 ||
1594 PrintStructures->hNoCollateIcon == 0 ||
1595 PrintStructures->hPortraitIcon == 0 ||
1596 PrintStructures->hLandscapeIcon == 0) {
1597 ERR("no icon in resource file\n");
1598 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1599 EndDialog(hDlg, FALSE);
1603 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1604 * must be registered and the Help button must be shown.
1606 if (lppd->Flags & PD_SHOWHELP) {
1607 if((PrintStructures->HelpMessageID =
1608 RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1609 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1610 return FALSE;
1612 } else
1613 PrintStructures->HelpMessageID = 0;
1615 if(!(lppd->Flags &PD_PRINTSETUP)) {
1616 PrintStructures->hwndUpDown =
1617 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1618 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1619 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1620 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1621 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1624 /* FIXME: I allow more freedom than either Win95 or WinNT,
1625 * which do not agree to what errors should be thrown or not
1626 * in case nToPage or nFromPage is out-of-range.
1628 if (lppd->nMaxPage < lppd->nMinPage)
1629 lppd->nMaxPage = lppd->nMinPage;
1630 if (lppd->nMinPage == lppd->nMaxPage)
1631 lppd->Flags |= PD_NOPAGENUMS;
1632 if (lppd->nToPage < lppd->nMinPage)
1633 lppd->nToPage = lppd->nMinPage;
1634 if (lppd->nToPage > lppd->nMaxPage)
1635 lppd->nToPage = lppd->nMaxPage;
1636 if (lppd->nFromPage < lppd->nMinPage)
1637 lppd->nFromPage = lppd->nMinPage;
1638 if (lppd->nFromPage > lppd->nMaxPage)
1639 lppd->nFromPage = lppd->nMaxPage;
1641 /* if we have the combo box, fill it */
1642 if (GetDlgItem(hDlg,comboID)) {
1643 /* Fill Combobox
1645 pdn = GlobalLock(lppd->hDevNames);
1646 pdm = GlobalLock(lppd->hDevMode);
1647 if(pdn)
1648 name = (WCHAR*)pdn + pdn->wDeviceOffset;
1649 else if(pdm)
1650 name = pdm->dmDeviceName;
1651 PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1652 if(pdm) GlobalUnlock(lppd->hDevMode);
1653 if(pdn) GlobalUnlock(lppd->hDevNames);
1655 /* Now find selected printer and update rest of dlg */
1656 /* ansi is ok here */
1657 name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1658 if (GetDlgItemTextW(hDlg, comboID, name, 255))
1659 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1660 HeapFree(GetProcessHeap(),0,name);
1661 } else {
1662 /* else use default printer */
1663 WCHAR name[200];
1664 DWORD dwBufLen = ARRAY_SIZE(name);
1665 BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1667 if (ret)
1668 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1669 else
1670 FIXME("No default printer found, expect problems!\n");
1672 return TRUE;
1675 /***********************************************************************
1676 * PRINTDLG_WMCommand [internal]
1678 static LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1679 PRINT_PTRA* PrintStructures)
1681 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1682 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1683 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1685 switch (LOWORD(wParam)) {
1686 case IDOK:
1687 TRACE(" OK button was hit\n");
1688 if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1689 FIXME("Update printdlg was not successful!\n");
1690 return(FALSE);
1692 EndDialog(hDlg, TRUE);
1693 return(TRUE);
1695 case IDCANCEL:
1696 TRACE(" CANCEL button was hit\n");
1697 EndDialog(hDlg, FALSE);
1698 return(FALSE);
1700 case pshHelp:
1701 TRACE(" HELP button was hit\n");
1702 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1703 (WPARAM) hDlg, (LPARAM) lppd);
1704 break;
1706 case chx2: /* collate pages checkbox */
1707 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1708 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1709 (LPARAM)PrintStructures->hCollateIcon);
1710 else
1711 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1712 (LPARAM)PrintStructures->hNoCollateIcon);
1713 break;
1714 case edt1: /* from page nr editbox */
1715 case edt2: /* to page nr editbox */
1716 if (HIWORD(wParam)==EN_CHANGE) {
1717 WORD nToPage;
1718 WORD nFromPage;
1719 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1720 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1721 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1722 CheckRadioButton(hDlg, rad1, rad3, rad3);
1724 break;
1726 case edt3:
1727 if(HIWORD(wParam) == EN_CHANGE) {
1728 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1729 if(copies <= 1)
1730 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1731 else
1732 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1734 break;
1736 case psh2: /* Properties button */
1738 HANDLE hPrinter;
1739 char PrinterName[256];
1741 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1742 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1743 FIXME(" Call to OpenPrinter did not succeed!\n");
1744 break;
1746 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1747 PrintStructures->lpDevMode,
1748 PrintStructures->lpDevMode,
1749 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1750 ClosePrinter(hPrinter);
1751 break;
1754 case rad1: /* Paperorientation */
1755 if (lppd->Flags & PD_PRINTSETUP)
1757 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1758 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1759 (LPARAM)(PrintStructures->hPortraitIcon));
1761 break;
1763 case rad2: /* Paperorientation */
1764 if (lppd->Flags & PD_PRINTSETUP)
1766 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1767 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1768 (LPARAM)(PrintStructures->hLandscapeIcon));
1770 break;
1772 case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1773 if (PrinterComboID != LOWORD(wParam)) {
1774 break;
1776 /* FALLTHROUGH */
1777 case cmb4: /* Printer combobox */
1778 if (HIWORD(wParam)==CBN_SELCHANGE) {
1779 char *PrinterName;
1780 INT index = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETCURSEL, 0, 0);
1781 INT length = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXTLEN, index, 0);
1782 PrinterName = HeapAlloc(GetProcessHeap(),0,length+1);
1783 SendDlgItemMessageA(hDlg, LOWORD(wParam), CB_GETLBTEXT, index, (LPARAM)PrinterName);
1784 PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1785 HeapFree(GetProcessHeap(),0,PrinterName);
1787 break;
1789 case cmb2: /* Papersize */
1791 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1792 if(Sel != CB_ERR) {
1793 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1794 CB_GETITEMDATA,
1795 Sel, 0);
1796 GetDlgItemTextA(hDlg, cmb2, (char *)lpdm->dmFormName, CCHFORMNAME);
1799 break;
1801 case cmb3: /* Bin */
1803 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1804 if(Sel != CB_ERR)
1805 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1806 CB_GETITEMDATA, Sel,
1809 break;
1811 if(lppd->Flags & PD_PRINTSETUP) {
1812 switch (LOWORD(wParam)) {
1813 case rad1: /* orientation */
1814 case rad2:
1815 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1816 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1817 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1818 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1819 (LPARAM)PrintStructures->hPortraitIcon);
1820 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1821 (LPARAM)PrintStructures->hPortraitIcon);
1823 } else {
1824 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1825 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1826 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1827 (LPARAM)PrintStructures->hLandscapeIcon);
1828 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1829 (LPARAM)PrintStructures->hLandscapeIcon);
1832 break;
1835 return FALSE;
1838 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1839 PRINT_PTRW* PrintStructures)
1841 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1842 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1843 LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1845 switch (LOWORD(wParam)) {
1846 case IDOK:
1847 TRACE(" OK button was hit\n");
1848 if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1849 FIXME("Update printdlg was not successful!\n");
1850 return(FALSE);
1852 EndDialog(hDlg, TRUE);
1853 return(TRUE);
1855 case IDCANCEL:
1856 TRACE(" CANCEL button was hit\n");
1857 EndDialog(hDlg, FALSE);
1858 return(FALSE);
1860 case pshHelp:
1861 TRACE(" HELP button was hit\n");
1862 SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1863 (WPARAM) hDlg, (LPARAM) lppd);
1864 break;
1866 case chx2: /* collate pages checkbox */
1867 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1868 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1869 (LPARAM)PrintStructures->hCollateIcon);
1870 else
1871 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1872 (LPARAM)PrintStructures->hNoCollateIcon);
1873 break;
1874 case edt1: /* from page nr editbox */
1875 case edt2: /* to page nr editbox */
1876 if (HIWORD(wParam)==EN_CHANGE) {
1877 WORD nToPage;
1878 WORD nFromPage;
1879 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1880 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1881 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1882 CheckRadioButton(hDlg, rad1, rad3, rad3);
1884 break;
1886 case edt3:
1887 if(HIWORD(wParam) == EN_CHANGE) {
1888 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1889 if(copies <= 1)
1890 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1891 else
1892 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1894 break;
1896 case psh2: /* Properties button */
1898 HANDLE hPrinter;
1899 WCHAR PrinterName[256];
1901 if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1902 if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1903 FIXME(" Call to OpenPrinter did not succeed!\n");
1904 break;
1906 DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1907 PrintStructures->lpDevMode,
1908 PrintStructures->lpDevMode,
1909 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1910 ClosePrinter(hPrinter);
1911 break;
1914 case rad1: /* Paperorientation */
1915 if (lppd->Flags & PD_PRINTSETUP)
1917 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1918 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1919 (LPARAM)(PrintStructures->hPortraitIcon));
1921 break;
1923 case rad2: /* Paperorientation */
1924 if (lppd->Flags & PD_PRINTSETUP)
1926 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1927 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1928 (LPARAM)(PrintStructures->hLandscapeIcon));
1930 break;
1932 case cmb1: /* Printer Combobox in PRINT SETUP */
1933 /* FALLTHROUGH */
1934 case cmb4: /* Printer combobox */
1935 if (HIWORD(wParam)==CBN_SELCHANGE) {
1936 WCHAR *PrinterName;
1937 INT index = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETCURSEL, 0, 0);
1938 INT length = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXTLEN, index, 0);
1940 PrinterName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(length+1));
1941 SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXT, index, (LPARAM)PrinterName);
1942 PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1943 HeapFree(GetProcessHeap(),0,PrinterName);
1945 break;
1947 case cmb2: /* Papersize */
1949 DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1950 if(Sel != CB_ERR) {
1951 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1952 CB_GETITEMDATA,
1953 Sel, 0);
1954 GetDlgItemTextW(hDlg, cmb2, lpdm->dmFormName, CCHFORMNAME);
1957 break;
1959 case cmb3: /* Bin */
1961 DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1962 if(Sel != CB_ERR)
1963 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1964 CB_GETITEMDATA, Sel,
1967 break;
1969 if(lppd->Flags & PD_PRINTSETUP) {
1970 switch (LOWORD(wParam)) {
1971 case rad1: /* orientation */
1972 case rad2:
1973 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1974 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1975 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1976 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1977 (LPARAM)PrintStructures->hPortraitIcon);
1978 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1979 (LPARAM)PrintStructures->hPortraitIcon);
1981 } else {
1982 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1983 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1984 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1985 (LPARAM)PrintStructures->hLandscapeIcon);
1986 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1987 (LPARAM)PrintStructures->hLandscapeIcon);
1990 break;
1993 return FALSE;
1996 /***********************************************************************
1997 * PrintDlgProcA [internal]
1999 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
2000 LPARAM lParam)
2002 PRINT_PTRA* PrintStructures;
2003 INT_PTR res = FALSE;
2005 if (uMsg!=WM_INITDIALOG) {
2006 PrintStructures = GetPropW(hDlg, printdlg_prop);
2007 if (!PrintStructures)
2008 return FALSE;
2009 } else {
2010 PrintStructures = (PRINT_PTRA*) lParam;
2011 SetPropW(hDlg, printdlg_prop, PrintStructures);
2012 if(!check_printer_setup(hDlg))
2014 EndDialog(hDlg,FALSE);
2015 return FALSE;
2017 res = PRINTDLG_WMInitDialog(hDlg, PrintStructures);
2019 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
2020 res = PrintStructures->lpPrintDlg->lpfnPrintHook(
2021 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
2023 return res;
2026 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
2027 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
2028 lParam);
2029 if(res) return res;
2032 switch (uMsg) {
2033 case WM_COMMAND:
2034 return PRINTDLG_WMCommandA(hDlg, wParam, PrintStructures);
2036 case WM_DESTROY:
2037 DestroyIcon(PrintStructures->hCollateIcon);
2038 DestroyIcon(PrintStructures->hNoCollateIcon);
2039 DestroyIcon(PrintStructures->hPortraitIcon);
2040 DestroyIcon(PrintStructures->hLandscapeIcon);
2041 if(PrintStructures->hwndUpDown)
2042 DestroyWindow(PrintStructures->hwndUpDown);
2043 return FALSE;
2045 return res;
2048 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
2049 LPARAM lParam)
2051 PRINT_PTRW* PrintStructures;
2052 INT_PTR res = FALSE;
2054 if (uMsg!=WM_INITDIALOG) {
2055 PrintStructures = GetPropW(hDlg, printdlg_prop);
2056 if (!PrintStructures)
2057 return FALSE;
2058 } else {
2059 PrintStructures = (PRINT_PTRW*) lParam;
2060 SetPropW(hDlg, printdlg_prop, PrintStructures);
2061 if(!check_printer_setup(hDlg))
2063 EndDialog(hDlg,FALSE);
2064 return FALSE;
2066 res = PRINTDLG_WMInitDialogW(hDlg, PrintStructures);
2068 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
2069 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
2070 return res;
2073 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
2074 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
2075 if(res) return res;
2078 switch (uMsg) {
2079 case WM_COMMAND:
2080 return PRINTDLG_WMCommandW(hDlg, wParam, PrintStructures);
2082 case WM_DESTROY:
2083 DestroyIcon(PrintStructures->hCollateIcon);
2084 DestroyIcon(PrintStructures->hNoCollateIcon);
2085 DestroyIcon(PrintStructures->hPortraitIcon);
2086 DestroyIcon(PrintStructures->hLandscapeIcon);
2087 if(PrintStructures->hwndUpDown)
2088 DestroyWindow(PrintStructures->hwndUpDown);
2089 return FALSE;
2091 return res;
2094 /************************************************************
2096 * PRINTDLG_GetDlgTemplate
2099 static HGLOBAL PRINTDLG_GetDlgTemplateA(const PRINTDLGA *lppd)
2101 HRSRC hResInfo;
2102 HGLOBAL hDlgTmpl;
2104 if (lppd->Flags & PD_PRINTSETUP) {
2105 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
2106 hDlgTmpl = lppd->hSetupTemplate;
2107 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
2108 hResInfo = FindResourceA(lppd->hInstance,
2109 lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
2110 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2111 } else {
2112 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
2113 (LPSTR)RT_DIALOG);
2114 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2116 } else {
2117 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
2118 hDlgTmpl = lppd->hPrintTemplate;
2119 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
2120 hResInfo = FindResourceA(lppd->hInstance,
2121 lppd->lpPrintTemplateName,
2122 (LPSTR)RT_DIALOG);
2123 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2124 } else {
2125 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
2126 (LPSTR)RT_DIALOG);
2127 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2130 return hDlgTmpl;
2133 static HGLOBAL PRINTDLG_GetDlgTemplateW(const PRINTDLGW *lppd)
2135 HRSRC hResInfo;
2136 HGLOBAL hDlgTmpl;
2138 if (lppd->Flags & PD_PRINTSETUP) {
2139 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
2140 hDlgTmpl = lppd->hSetupTemplate;
2141 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
2142 hResInfo = FindResourceW(lppd->hInstance,
2143 lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
2144 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2145 } else {
2146 hResInfo = FindResourceW(COMDLG32_hInstance, L"PRINT32_SETUP", (LPWSTR)RT_DIALOG);
2147 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2149 } else {
2150 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
2151 hDlgTmpl = lppd->hPrintTemplate;
2152 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
2153 hResInfo = FindResourceW(lppd->hInstance,
2154 lppd->lpPrintTemplateName,
2155 (LPWSTR)RT_DIALOG);
2156 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2157 } else {
2158 hResInfo = FindResourceW(COMDLG32_hInstance, L"PRINT32", (LPWSTR)RT_DIALOG);
2159 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2162 return hDlgTmpl;
2165 /***********************************************************************
2167 * PRINTDLG_CreateDC
2170 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
2172 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
2173 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
2175 if(lppd->Flags & PD_RETURNDC) {
2176 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
2177 (char*)pdn + pdn->wDeviceOffset,
2178 (char*)pdn + pdn->wOutputOffset,
2179 pdm );
2180 } else if(lppd->Flags & PD_RETURNIC) {
2181 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
2182 (char*)pdn + pdn->wDeviceOffset,
2183 (char*)pdn + pdn->wOutputOffset,
2184 pdm );
2186 GlobalUnlock(lppd->hDevNames);
2187 GlobalUnlock(lppd->hDevMode);
2188 return lppd->hDC != NULL;
2191 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
2193 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
2194 DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
2196 if(lppd->Flags & PD_RETURNDC) {
2197 lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
2198 (WCHAR*)pdn + pdn->wDeviceOffset,
2199 (WCHAR*)pdn + pdn->wOutputOffset,
2200 pdm );
2201 } else if(lppd->Flags & PD_RETURNIC) {
2202 lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
2203 (WCHAR*)pdn + pdn->wDeviceOffset,
2204 (WCHAR*)pdn + pdn->wOutputOffset,
2205 pdm );
2207 GlobalUnlock(lppd->hDevNames);
2208 GlobalUnlock(lppd->hDevMode);
2209 return lppd->hDC != NULL;
2212 /***********************************************************************
2213 * PrintDlgA (COMDLG32.@)
2215 * Displays the PRINT dialog box, which enables the user to specify
2216 * specific properties of the print job.
2218 * PARAMS
2219 * lppd [IO] ptr to PRINTDLG32 struct
2221 * RETURNS
2222 * nonzero if the user pressed the OK button
2223 * zero if the user cancelled the window or an error occurred
2225 * BUGS
2226 * PrintDlg:
2227 * * The Collate Icons do not display, even though they are in the code.
2228 * * The Properties Button(s) should call DocumentPropertiesA().
2231 BOOL WINAPI PrintDlgA(LPPRINTDLGA lppd)
2233 BOOL bRet = FALSE;
2234 LPVOID ptr;
2235 HINSTANCE hInst;
2237 if (!lppd)
2239 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2240 return FALSE;
2243 if(TRACE_ON(commdlg)) {
2244 char flagstr[1000] = "";
2245 const struct pd_flags *pflag = pd_flags;
2246 for( ; pflag->name; pflag++) {
2247 if(lppd->Flags & pflag->flag)
2248 strcat(flagstr, pflag->name);
2250 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2251 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2252 "flags %08x (%s)\n",
2253 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2254 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2255 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2258 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2259 WARN("structure size failure!!!\n");
2260 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2261 return FALSE;
2264 if(lppd->Flags & PD_RETURNDEFAULT) {
2265 PRINTER_INFO_2A *pbuf;
2266 DRIVER_INFO_3A *dbuf;
2267 HANDLE hprn;
2268 DWORD needed;
2270 if(lppd->hDevMode || lppd->hDevNames) {
2271 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2272 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2273 return FALSE;
2275 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2276 WARN("Can't find default printer\n");
2277 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2278 return FALSE;
2281 GetPrinterA(hprn, 2, NULL, 0, &needed);
2282 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2283 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2285 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2286 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2287 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2288 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2289 GetLastError(),pbuf->pPrinterName);
2290 HeapFree(GetProcessHeap(), 0, dbuf);
2291 HeapFree(GetProcessHeap(), 0, pbuf);
2292 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2293 return FALSE;
2295 ClosePrinter(hprn);
2297 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2298 dbuf->pDriverPath,
2299 pbuf->pPrinterName,
2300 pbuf->pPortName);
2301 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2302 pbuf->pDevMode->dmDriverExtra);
2303 ptr = GlobalLock(lppd->hDevMode);
2304 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2305 pbuf->pDevMode->dmDriverExtra);
2306 GlobalUnlock(lppd->hDevMode);
2307 HeapFree(GetProcessHeap(), 0, pbuf);
2308 HeapFree(GetProcessHeap(), 0, dbuf);
2309 bRet = TRUE;
2310 } else {
2311 HGLOBAL hDlgTmpl;
2312 PRINT_PTRA *PrintStructures;
2314 /* load Dialog resources,
2315 * depending on Flags indicates Print32 or Print32_setup dialog
2317 hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2318 if (!hDlgTmpl) {
2319 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2320 return FALSE;
2322 ptr = LockResource( hDlgTmpl );
2323 if (!ptr) {
2324 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2325 return FALSE;
2328 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2329 sizeof(PRINT_PTRA));
2330 PrintStructures->lpPrintDlg = lppd;
2332 /* and create & process the dialog .
2333 * -1 is failure, 0 is broken hwnd, everything else is ok.
2335 hInst = COMDLG32_hInstance;
2336 if (lppd->Flags & (PD_ENABLESETUPTEMPLATE | PD_ENABLEPRINTTEMPLATE)) hInst = lppd->hInstance;
2337 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2338 PrintDlgProcA,
2339 (LPARAM)PrintStructures));
2341 if(bRet) {
2342 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2343 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2344 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2346 if (lppd->hDevMode == 0) {
2347 TRACE(" No hDevMode yet... Need to create my own\n");
2348 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2349 lpdm->dmSize + lpdm->dmDriverExtra);
2350 } else {
2351 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2352 lpdm->dmSize + lpdm->dmDriverExtra,
2353 GMEM_MOVEABLE);
2355 lpdmReturn = GlobalLock(lppd->hDevMode);
2356 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2358 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2359 di->pDriverPath,
2360 pi->pPrinterName,
2361 pi->pPortName
2363 GlobalUnlock(lppd->hDevMode);
2365 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2366 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2367 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2368 HeapFree(GetProcessHeap(), 0, PrintStructures);
2370 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2371 bRet = PRINTDLG_CreateDCA(lppd);
2373 TRACE("exit! (%d)\n", bRet);
2374 return bRet;
2377 /***********************************************************************
2378 * PrintDlgW (COMDLG32.@)
2380 * See PrintDlgA.
2382 BOOL WINAPI PrintDlgW(LPPRINTDLGW lppd)
2384 BOOL bRet = FALSE;
2385 LPVOID ptr;
2386 HINSTANCE hInst;
2388 if (!lppd)
2390 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2391 return FALSE;
2394 if(TRACE_ON(commdlg)) {
2395 char flagstr[1000] = "";
2396 const struct pd_flags *pflag = pd_flags;
2397 for( ; pflag->name; pflag++) {
2398 if(lppd->Flags & pflag->flag)
2399 strcat(flagstr, pflag->name);
2401 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2402 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2403 "flags %08x (%s)\n",
2404 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2405 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2406 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2409 if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2410 WARN("structure size failure!!!\n");
2411 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2412 return FALSE;
2415 if(lppd->Flags & PD_RETURNDEFAULT) {
2416 PRINTER_INFO_2W *pbuf;
2417 DRIVER_INFO_3W *dbuf;
2418 HANDLE hprn;
2419 DWORD needed;
2421 if(lppd->hDevMode || lppd->hDevNames) {
2422 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2423 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2424 return FALSE;
2426 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2427 WARN("Can't find default printer\n");
2428 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2429 return FALSE;
2432 GetPrinterW(hprn, 2, NULL, 0, &needed);
2433 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2434 GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2436 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2437 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2438 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2439 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2440 GetLastError(),debugstr_w(pbuf->pPrinterName));
2441 HeapFree(GetProcessHeap(), 0, dbuf);
2442 HeapFree(GetProcessHeap(), 0, pbuf);
2443 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2444 return FALSE;
2446 ClosePrinter(hprn);
2448 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2449 dbuf->pDriverPath,
2450 pbuf->pPrinterName,
2451 pbuf->pPortName);
2452 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2453 pbuf->pDevMode->dmDriverExtra);
2454 ptr = GlobalLock(lppd->hDevMode);
2455 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2456 pbuf->pDevMode->dmDriverExtra);
2457 GlobalUnlock(lppd->hDevMode);
2458 HeapFree(GetProcessHeap(), 0, pbuf);
2459 HeapFree(GetProcessHeap(), 0, dbuf);
2460 bRet = TRUE;
2461 } else {
2462 HGLOBAL hDlgTmpl;
2463 PRINT_PTRW *PrintStructures;
2465 /* load Dialog resources,
2466 * depending on Flags indicates Print32 or Print32_setup dialog
2468 hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2469 if (!hDlgTmpl) {
2470 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2471 return FALSE;
2473 ptr = LockResource( hDlgTmpl );
2474 if (!ptr) {
2475 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2476 return FALSE;
2479 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2480 sizeof(PRINT_PTRW));
2481 PrintStructures->lpPrintDlg = lppd;
2483 /* and create & process the dialog .
2484 * -1 is failure, 0 is broken hwnd, everything else is ok.
2486 hInst = COMDLG32_hInstance;
2487 if (lppd->Flags & (PD_ENABLESETUPTEMPLATE | PD_ENABLEPRINTTEMPLATE)) hInst = lppd->hInstance;
2488 bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2489 PrintDlgProcW,
2490 (LPARAM)PrintStructures));
2492 if(bRet) {
2493 DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2494 PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2495 DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2497 if (lppd->hDevMode == 0) {
2498 TRACE(" No hDevMode yet... Need to create my own\n");
2499 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2500 lpdm->dmSize + lpdm->dmDriverExtra);
2501 } else {
2502 WORD locks;
2503 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2504 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2505 while(locks--) {
2506 GlobalUnlock(lppd->hDevMode);
2507 TRACE("Now got %d locks\n", locks);
2510 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2511 lpdm->dmSize + lpdm->dmDriverExtra,
2512 GMEM_MOVEABLE);
2514 lpdmReturn = GlobalLock(lppd->hDevMode);
2515 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2517 if (lppd->hDevNames != 0) {
2518 WORD locks;
2519 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2520 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2521 while(locks--)
2522 GlobalUnlock(lppd->hDevNames);
2525 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2526 di->pDriverPath,
2527 pi->pPrinterName,
2528 pi->pPortName
2530 GlobalUnlock(lppd->hDevMode);
2532 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2533 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2534 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2535 HeapFree(GetProcessHeap(), 0, PrintStructures);
2537 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2538 bRet = PRINTDLG_CreateDCW(lppd);
2540 TRACE("exit! (%d)\n", bRet);
2541 return bRet;
2544 /***********************************************************************
2546 * PageSetupDlg
2547 * rad1 - portrait
2548 * rad2 - landscape
2549 * cmb1 - printer select (not in standard dialog template)
2550 * cmb2 - paper size
2551 * cmb3 - source (tray?)
2552 * edt4 - border left
2553 * edt5 - border top
2554 * edt6 - border right
2555 * edt7 - border bottom
2556 * psh3 - "Printer..."
2559 typedef struct
2561 BOOL unicode;
2562 union
2564 LPPAGESETUPDLGA dlga;
2565 LPPAGESETUPDLGW dlgw;
2566 } u;
2567 HWND hDlg; /* Page Setup dialog handle */
2568 RECT rtDrawRect; /* Drawing rect for page */
2569 } pagesetup_data;
2571 static inline DWORD pagesetup_get_flags(const pagesetup_data *data)
2573 return data->u.dlgw->Flags;
2576 static inline BOOL is_metric(const pagesetup_data *data)
2578 return pagesetup_get_flags(data) & PSD_INHUNDREDTHSOFMILLIMETERS;
2581 static inline LONG tenths_mm_to_size(const pagesetup_data *data, LONG size)
2583 if (is_metric(data))
2584 return 10 * size;
2585 else
2586 return 10 * size * 100 / 254;
2589 static inline LONG thousandths_inch_to_size(const pagesetup_data *data, LONG size)
2591 if (is_metric(data))
2592 return size * 254 / 100;
2593 else
2594 return size;
2597 static WCHAR get_decimal_sep(void)
2599 static WCHAR sep;
2601 if(!sep)
2603 WCHAR buf[] = L".";
2604 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, ARRAY_SIZE(buf));
2605 sep = buf[0];
2607 return sep;
2610 static void size2str(const pagesetup_data *data, DWORD size, LPWSTR strout)
2612 /* FIXME use LOCALE_SDECIMAL when the edit parsing code can cope */
2614 if (is_metric(data))
2616 if(size % 100)
2617 wsprintfW(strout, L"%d%c%02d", size / 100, get_decimal_sep(), size % 100);
2618 else
2619 wsprintfW(strout, L"%d", size / 100);
2621 else
2623 if(size % 1000)
2624 wsprintfW(strout, L"%d%c%03d", size / 1000, get_decimal_sep(), size % 1000);
2625 else
2626 wsprintfW(strout, L"%d", size / 1000);
2631 static inline BOOL is_default_metric(void)
2633 DWORD system;
2634 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
2635 (LPWSTR)&system, sizeof(system));
2636 return system == 0;
2639 /**********************************************
2640 * rotate_rect
2641 * Cyclically permute the four members of rc
2642 * If sense is TRUE l -> t -> r -> b
2643 * otherwise l <- t <- r <- b
2645 static inline void rotate_rect(RECT *rc, BOOL sense)
2647 INT tmp;
2648 if(sense)
2650 tmp = rc->bottom;
2651 rc->bottom = rc->right;
2652 rc->right = rc->top;
2653 rc->top = rc->left;
2654 rc->left = tmp;
2656 else
2658 tmp = rc->left;
2659 rc->left = rc->top;
2660 rc->top = rc->right;
2661 rc->right = rc->bottom;
2662 rc->bottom = tmp;
2666 static void pagesetup_set_orientation(pagesetup_data *data, WORD orient)
2668 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2670 assert(orient == DMORIENT_PORTRAIT || orient == DMORIENT_LANDSCAPE);
2672 if(data->unicode)
2673 dm->u1.s1.dmOrientation = orient;
2674 else
2676 DEVMODEA *dmA = (DEVMODEA *)dm;
2677 dmA->u1.s1.dmOrientation = orient;
2679 GlobalUnlock(data->u.dlgw->hDevMode);
2682 static WORD pagesetup_get_orientation(const pagesetup_data *data)
2684 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2685 WORD orient;
2687 if(data->unicode)
2688 orient = dm->u1.s1.dmOrientation;
2689 else
2691 DEVMODEA *dmA = (DEVMODEA *)dm;
2692 orient = dmA->u1.s1.dmOrientation;
2694 GlobalUnlock(data->u.dlgw->hDevMode);
2695 return orient;
2698 static void pagesetup_set_papersize(pagesetup_data *data, WORD paper)
2700 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2702 if(data->unicode)
2703 dm->u1.s1.dmPaperSize = paper;
2704 else
2706 DEVMODEA *dmA = (DEVMODEA *)dm;
2707 dmA->u1.s1.dmPaperSize = paper;
2709 GlobalUnlock(data->u.dlgw->hDevMode);
2712 static WORD pagesetup_get_papersize(const pagesetup_data *data)
2714 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2715 WORD paper;
2717 if(data->unicode)
2718 paper = dm->u1.s1.dmPaperSize;
2719 else
2721 DEVMODEA *dmA = (DEVMODEA *)dm;
2722 paper = dmA->u1.s1.dmPaperSize;
2724 GlobalUnlock(data->u.dlgw->hDevMode);
2725 return paper;
2728 static void pagesetup_set_defaultsource(pagesetup_data *data, WORD source)
2730 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2732 if(data->unicode)
2733 dm->u1.s1.dmDefaultSource = source;
2734 else
2736 DEVMODEA *dmA = (DEVMODEA *)dm;
2737 dmA->u1.s1.dmDefaultSource = source;
2739 GlobalUnlock(data->u.dlgw->hDevMode);
2742 typedef enum
2744 devnames_driver_name,
2745 devnames_device_name,
2746 devnames_output_name
2747 } devnames_name;
2750 static inline WORD get_devname_offset(const DEVNAMES *dn, devnames_name which)
2752 switch(which)
2754 case devnames_driver_name: return dn->wDriverOffset;
2755 case devnames_device_name: return dn->wDeviceOffset;
2756 case devnames_output_name: return dn->wOutputOffset;
2758 ERR("Shouldn't be here\n");
2759 return 0;
2762 static WCHAR *pagesetup_get_a_devname(const pagesetup_data *data, devnames_name which)
2764 DEVNAMES *dn;
2765 WCHAR *name;
2767 dn = GlobalLock(data->u.dlgw->hDevNames);
2768 if(data->unicode)
2769 name = strdupW((WCHAR *)dn + get_devname_offset(dn, which));
2770 else
2772 int len = MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, NULL, 0);
2773 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2774 MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, name, len);
2776 GlobalUnlock(data->u.dlgw->hDevNames);
2777 return name;
2780 static WCHAR *pagesetup_get_drvname(const pagesetup_data *data)
2782 return pagesetup_get_a_devname(data, devnames_driver_name);
2785 static WCHAR *pagesetup_get_devname(const pagesetup_data *data)
2787 return pagesetup_get_a_devname(data, devnames_device_name);
2790 static WCHAR *pagesetup_get_portname(const pagesetup_data *data)
2792 return pagesetup_get_a_devname(data, devnames_output_name);
2795 static void pagesetup_release_a_devname(const pagesetup_data *data, WCHAR *name)
2797 HeapFree(GetProcessHeap(), 0, name);
2800 static void pagesetup_set_devnames(pagesetup_data *data, LPCWSTR drv, LPCWSTR devname, LPCWSTR port)
2802 DEVNAMES *dn;
2803 WCHAR def[256];
2804 DWORD len = sizeof(DEVNAMES), drv_len, dev_len, port_len;
2806 if(data->unicode)
2808 drv_len = (lstrlenW(drv) + 1) * sizeof(WCHAR);
2809 dev_len = (lstrlenW(devname) + 1) * sizeof(WCHAR);
2810 port_len = (lstrlenW(port) + 1) * sizeof(WCHAR);
2812 else
2814 drv_len = WideCharToMultiByte(CP_ACP, 0, drv, -1, NULL, 0, NULL, NULL);
2815 dev_len = WideCharToMultiByte(CP_ACP, 0, devname, -1, NULL, 0, NULL, NULL);
2816 port_len = WideCharToMultiByte(CP_ACP, 0, port, -1, NULL, 0, NULL, NULL);
2818 len += drv_len + dev_len + port_len;
2820 if(data->u.dlgw->hDevNames)
2821 data->u.dlgw->hDevNames = GlobalReAlloc(data->u.dlgw->hDevNames, len, GMEM_MOVEABLE);
2822 else
2823 data->u.dlgw->hDevNames = GlobalAlloc(GMEM_MOVEABLE, len);
2825 dn = GlobalLock(data->u.dlgw->hDevNames);
2827 if(data->unicode)
2829 WCHAR *ptr = (WCHAR *)(dn + 1);
2830 len = sizeof(DEVNAMES) / sizeof(WCHAR);
2831 dn->wDriverOffset = len;
2832 lstrcpyW(ptr, drv);
2833 ptr += drv_len / sizeof(WCHAR);
2834 len += drv_len / sizeof(WCHAR);
2835 dn->wDeviceOffset = len;
2836 lstrcpyW(ptr, devname);
2837 ptr += dev_len / sizeof(WCHAR);
2838 len += dev_len / sizeof(WCHAR);
2839 dn->wOutputOffset = len;
2840 lstrcpyW(ptr, port);
2842 else
2844 char *ptr = (char *)(dn + 1);
2845 len = sizeof(DEVNAMES);
2846 dn->wDriverOffset = len;
2847 WideCharToMultiByte(CP_ACP, 0, drv, -1, ptr, drv_len, NULL, NULL);
2848 ptr += drv_len;
2849 len += drv_len;
2850 dn->wDeviceOffset = len;
2851 WideCharToMultiByte(CP_ACP, 0, devname, -1, ptr, dev_len, NULL, NULL);
2852 ptr += dev_len;
2853 len += dev_len;
2854 dn->wOutputOffset = len;
2855 WideCharToMultiByte(CP_ACP, 0, port, -1, ptr, port_len, NULL, NULL);
2858 dn->wDefault = 0;
2859 len = ARRAY_SIZE(def);
2860 GetDefaultPrinterW(def, &len);
2861 if(!lstrcmpW(def, devname))
2862 dn->wDefault = 1;
2864 GlobalUnlock(data->u.dlgw->hDevNames);
2867 static DEVMODEW *pagesetup_get_devmode(const pagesetup_data *data)
2869 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2870 DEVMODEW *ret;
2872 if(data->unicode)
2874 /* We make a copy even in the unicode case because the ptr
2875 may get passed back to us in pagesetup_set_devmode. */
2876 ret = HeapAlloc(GetProcessHeap(), 0, dm->dmSize + dm->dmDriverExtra);
2877 memcpy(ret, dm, dm->dmSize + dm->dmDriverExtra);
2879 else
2880 ret = GdiConvertToDevmodeW((DEVMODEA *)dm);
2882 GlobalUnlock(data->u.dlgw->hDevMode);
2883 return ret;
2886 static void pagesetup_release_devmode(const pagesetup_data *data, DEVMODEW *dm)
2888 HeapFree(GetProcessHeap(), 0, dm);
2891 static void pagesetup_set_devmode(pagesetup_data *data, DEVMODEW *dm)
2893 DEVMODEA *dmA = NULL;
2894 void *src, *dst;
2895 DWORD size;
2897 if(data->unicode)
2899 size = dm->dmSize + dm->dmDriverExtra;
2900 src = dm;
2902 else
2904 dmA = convert_to_devmodeA(dm);
2905 size = dmA->dmSize + dmA->dmDriverExtra;
2906 src = dmA;
2909 if(data->u.dlgw->hDevMode)
2910 data->u.dlgw->hDevMode = GlobalReAlloc(data->u.dlgw->hDevMode, size,
2911 GMEM_MOVEABLE);
2912 else
2913 data->u.dlgw->hDevMode = GlobalAlloc(GMEM_MOVEABLE, size);
2915 dst = GlobalLock(data->u.dlgw->hDevMode);
2916 memcpy(dst, src, size);
2917 GlobalUnlock(data->u.dlgw->hDevMode);
2918 HeapFree(GetProcessHeap(), 0, dmA);
2921 static inline POINT *pagesetup_get_papersize_pt(const pagesetup_data *data)
2923 return &data->u.dlgw->ptPaperSize;
2926 static inline RECT *pagesetup_get_margin_rect(const pagesetup_data *data)
2928 return &data->u.dlgw->rtMargin;
2931 typedef enum
2933 page_setup_hook,
2934 page_paint_hook
2935 } hook_type;
2937 static inline LPPAGESETUPHOOK pagesetup_get_hook(const pagesetup_data *data, hook_type which)
2939 switch(which)
2941 case page_setup_hook: return data->u.dlgw->lpfnPageSetupHook;
2942 case page_paint_hook: return data->u.dlgw->lpfnPagePaintHook;
2944 return NULL;
2947 /* This should only be used in calls to hook procs so we return the ptr
2948 already cast to LPARAM */
2949 static inline LPARAM pagesetup_get_dlg_struct(const pagesetup_data *data)
2951 return (LPARAM)data->u.dlgw;
2954 static inline void swap_point(POINT *pt)
2956 LONG tmp = pt->x;
2957 pt->x = pt->y;
2958 pt->y = tmp;
2961 static BOOL pagesetup_update_papersize(pagesetup_data *data)
2963 DEVMODEW *dm;
2964 LPWSTR devname, portname;
2965 int i, num;
2966 WORD *words = NULL, paperword;
2967 POINT *points = NULL;
2968 BOOL retval = FALSE;
2970 dm = pagesetup_get_devmode(data);
2971 devname = pagesetup_get_devname(data);
2972 portname = pagesetup_get_portname(data);
2974 num = DeviceCapabilitiesW(devname, portname, DC_PAPERS, NULL, dm);
2975 if (num <= 0)
2977 FIXME("No papernames found for %s/%s\n", debugstr_w(devname), debugstr_w(portname));
2978 goto end;
2981 words = HeapAlloc(GetProcessHeap(), 0, num * sizeof(WORD));
2982 points = HeapAlloc(GetProcessHeap(), 0, num * sizeof(POINT));
2984 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERS, (LPWSTR)words, dm))
2986 FIXME("Number of returned words is not %d\n", num);
2987 goto end;
2990 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERSIZE, (LPWSTR)points, dm))
2992 FIXME("Number of returned sizes is not %d\n", num);
2993 goto end;
2996 paperword = pagesetup_get_papersize(data);
2998 for (i = 0; i < num; i++)
2999 if (words[i] == paperword)
3000 break;
3002 if (i == num)
3004 FIXME("Papersize %d not found in list?\n", paperword);
3005 goto end;
3008 /* this is _10ths_ of a millimeter */
3009 pagesetup_get_papersize_pt(data)->x = tenths_mm_to_size(data, points[i].x);
3010 pagesetup_get_papersize_pt(data)->y = tenths_mm_to_size(data, points[i].y);
3012 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3013 swap_point(pagesetup_get_papersize_pt(data));
3015 retval = TRUE;
3017 end:
3018 HeapFree(GetProcessHeap(), 0, words);
3019 HeapFree(GetProcessHeap(), 0, points);
3020 pagesetup_release_a_devname(data, portname);
3021 pagesetup_release_a_devname(data, devname);
3022 pagesetup_release_devmode(data, dm);
3024 return retval;
3027 /**********************************************************************************************
3028 * pagesetup_change_printer
3030 * Redefines hDevMode and hDevNames HANDLES and initialises it.
3033 static BOOL pagesetup_change_printer(LPWSTR name, pagesetup_data *data)
3035 HANDLE hprn;
3036 DWORD needed;
3037 PRINTER_INFO_2W *prn_info = NULL;
3038 DRIVER_INFO_3W *drv_info = NULL;
3039 DEVMODEW *dm = NULL;
3040 BOOL retval = FALSE;
3042 if(!OpenPrinterW(name, &hprn, NULL))
3044 ERR("Can't open printer %s\n", debugstr_w(name));
3045 goto end;
3048 GetPrinterW(hprn, 2, NULL, 0, &needed);
3049 prn_info = HeapAlloc(GetProcessHeap(), 0, needed);
3050 GetPrinterW(hprn, 2, (LPBYTE)prn_info, needed, &needed);
3051 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
3052 drv_info = HeapAlloc(GetProcessHeap(), 0, needed);
3053 if(!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)drv_info, needed, &needed))
3055 ERR("GetPrinterDriverA failed for %s, fix your config!\n", debugstr_w(prn_info->pPrinterName));
3056 goto end;
3058 ClosePrinter(hprn);
3060 needed = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
3061 if(needed == -1)
3063 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
3064 goto end;
3067 dm = HeapAlloc(GetProcessHeap(), 0, needed);
3068 DocumentPropertiesW(0, 0, name, dm, NULL, DM_OUT_BUFFER);
3070 pagesetup_set_devmode(data, dm);
3071 pagesetup_set_devnames(data, drv_info->pDriverPath, prn_info->pPrinterName,
3072 prn_info->pPortName);
3074 retval = TRUE;
3075 end:
3076 HeapFree(GetProcessHeap(), 0, dm);
3077 HeapFree(GetProcessHeap(), 0, prn_info);
3078 HeapFree(GetProcessHeap(), 0, drv_info);
3079 return retval;
3082 /****************************************************************************************
3083 * pagesetup_init_combos
3085 * Fills Printers, Paper and Source combos
3088 static void pagesetup_init_combos(HWND hDlg, pagesetup_data *data)
3090 DEVMODEW *dm;
3091 LPWSTR devname, portname;
3093 dm = pagesetup_get_devmode(data);
3094 devname = pagesetup_get_devname(data);
3095 portname = pagesetup_get_portname(data);
3097 PRINTDLG_SetUpPrinterListComboW(hDlg, cmb1, devname);
3098 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2, devname, portname, dm);
3099 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3, devname, portname, dm);
3101 pagesetup_release_a_devname(data, portname);
3102 pagesetup_release_a_devname(data, devname);
3103 pagesetup_release_devmode(data, dm);
3107 /****************************************************************************************
3108 * pagesetup_change_printer_dialog
3110 * Pops up another dialog that lets the user pick another printer.
3112 * For now we display the PrintDlg, this should display a striped down version of it.
3114 static void pagesetup_change_printer_dialog(HWND hDlg, pagesetup_data *data)
3116 PRINTDLGW prnt;
3117 LPWSTR drvname, devname, portname;
3118 DEVMODEW *tmp_dm, *dm;
3120 memset(&prnt, 0, sizeof(prnt));
3121 prnt.lStructSize = sizeof(prnt);
3122 prnt.Flags = 0;
3123 prnt.hwndOwner = hDlg;
3125 drvname = pagesetup_get_drvname(data);
3126 devname = pagesetup_get_devname(data);
3127 portname = pagesetup_get_portname(data);
3128 prnt.hDevNames = 0;
3129 PRINTDLG_CreateDevNamesW(&prnt.hDevNames, drvname, devname, portname);
3130 pagesetup_release_a_devname(data, portname);
3131 pagesetup_release_a_devname(data, devname);
3132 pagesetup_release_a_devname(data, drvname);
3134 tmp_dm = pagesetup_get_devmode(data);
3135 prnt.hDevMode = GlobalAlloc(GMEM_MOVEABLE, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
3136 dm = GlobalLock(prnt.hDevMode);
3137 memcpy(dm, tmp_dm, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
3138 GlobalUnlock(prnt.hDevMode);
3139 pagesetup_release_devmode(data, tmp_dm);
3141 if (PrintDlgW(&prnt))
3143 DEVMODEW *dm = GlobalLock(prnt.hDevMode);
3144 DEVNAMES *dn = GlobalLock(prnt.hDevNames);
3146 pagesetup_set_devnames(data, (WCHAR*)dn + dn->wDriverOffset,
3147 (WCHAR*)dn + dn->wDeviceOffset, (WCHAR *)dn + dn->wOutputOffset);
3148 pagesetup_set_devmode(data, dm);
3149 GlobalUnlock(prnt.hDevNames);
3150 GlobalUnlock(prnt.hDevMode);
3151 pagesetup_init_combos(hDlg, data);
3154 GlobalFree(prnt.hDevMode);
3155 GlobalFree(prnt.hDevNames);
3159 /******************************************************************************************
3160 * pagesetup_change_preview
3162 * Changes paper preview size / position
3165 static void pagesetup_change_preview(const pagesetup_data *data)
3167 LONG width, height, x, y;
3168 RECT tmp;
3169 const int shadow = 4;
3171 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3173 width = data->rtDrawRect.right - data->rtDrawRect.left;
3174 height = pagesetup_get_papersize_pt(data)->y * width / pagesetup_get_papersize_pt(data)->x;
3176 else
3178 height = data->rtDrawRect.bottom - data->rtDrawRect.top;
3179 width = pagesetup_get_papersize_pt(data)->x * height / pagesetup_get_papersize_pt(data)->y;
3181 x = (data->rtDrawRect.right + data->rtDrawRect.left - width) / 2;
3182 y = (data->rtDrawRect.bottom + data->rtDrawRect.top - height) / 2;
3183 TRACE("draw rect %s x=%d, y=%d, w=%d, h=%d\n",
3184 wine_dbgstr_rect(&data->rtDrawRect), x, y, width, height);
3186 MoveWindow(GetDlgItem(data->hDlg, rct2), x + width, y + shadow, shadow, height, FALSE);
3187 MoveWindow(GetDlgItem(data->hDlg, rct3), x + shadow, y + height, width, shadow, FALSE);
3188 MoveWindow(GetDlgItem(data->hDlg, rct1), x, y, width, height, FALSE);
3190 tmp = data->rtDrawRect;
3191 tmp.right += shadow;
3192 tmp.bottom += shadow;
3193 InvalidateRect(data->hDlg, &tmp, TRUE);
3196 static inline LONG *element_from_margin_id(RECT *rc, WORD id)
3198 switch(id)
3200 case edt4: return &rc->left;
3201 case edt5: return &rc->top;
3202 case edt6: return &rc->right;
3203 case edt7: return &rc->bottom;
3205 return NULL;
3208 static void update_margin_edits(HWND hDlg, const pagesetup_data *data, WORD id)
3210 WCHAR str[100];
3211 WORD idx;
3213 for(idx = edt4; idx <= edt7; idx++)
3215 if(id == 0 || id == idx)
3217 size2str(data, *element_from_margin_id(pagesetup_get_margin_rect(data), idx), str);
3218 SetDlgItemTextW(hDlg, idx, str);
3223 static void margin_edit_notification(HWND hDlg, const pagesetup_data *data, WORD msg, WORD id)
3225 switch (msg)
3227 case EN_CHANGE:
3229 WCHAR buf[10];
3230 LONG val = 0;
3231 LONG *value = element_from_margin_id(pagesetup_get_margin_rect(data), id);
3233 if (GetDlgItemTextW(hDlg, id, buf, ARRAY_SIZE(buf)) != 0)
3235 WCHAR *end;
3236 WCHAR decimal = get_decimal_sep();
3238 val = wcstol(buf, &end, 10);
3239 if(end != buf || *end == decimal)
3241 int mult = is_metric(data) ? 100 : 1000;
3242 val *= mult;
3243 if(*end == decimal)
3245 while(mult > 1)
3247 end++;
3248 mult /= 10;
3249 if(iswdigit(*end))
3250 val += (*end - '0') * mult;
3251 else
3252 break;
3257 *value = val;
3258 return;
3261 case EN_KILLFOCUS:
3262 update_margin_edits(hDlg, data, id);
3263 return;
3267 static void set_margin_groupbox_title(HWND hDlg, const pagesetup_data *data)
3269 WCHAR title[256];
3271 if(LoadStringW(COMDLG32_hInstance, is_metric(data) ? PD32_MARGINS_IN_MILLIMETERS : PD32_MARGINS_IN_INCHES,
3272 title, ARRAY_SIZE(title)))
3273 SetDlgItemTextW(hDlg, grp4, title);
3276 static void pagesetup_update_orientation_buttons(HWND hDlg, const pagesetup_data *data)
3278 if (pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3279 CheckRadioButton(hDlg, rad1, rad2, rad2);
3280 else
3281 CheckRadioButton(hDlg, rad1, rad2, rad1);
3284 /****************************************************************************************
3285 * pagesetup_printer_properties
3287 * Handle invocation of the 'Properties' button (not present in the default template).
3289 static void pagesetup_printer_properties(HWND hDlg, pagesetup_data *data)
3291 HANDLE hprn;
3292 LPWSTR devname;
3293 DEVMODEW *dm;
3294 LRESULT count;
3295 int i;
3297 devname = pagesetup_get_devname(data);
3299 if (!OpenPrinterW(devname, &hprn, NULL))
3301 FIXME("Call to OpenPrinter did not succeed!\n");
3302 pagesetup_release_a_devname(data, devname);
3303 return;
3306 dm = pagesetup_get_devmode(data);
3307 DocumentPropertiesW(hDlg, hprn, devname, dm, dm, DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
3308 pagesetup_set_devmode(data, dm);
3309 pagesetup_release_devmode(data, dm);
3310 pagesetup_release_a_devname(data, devname);
3311 ClosePrinter(hprn);
3313 /* Changing paper */
3314 pagesetup_update_papersize(data);
3315 pagesetup_update_orientation_buttons(hDlg, data);
3317 /* Changing paper preview */
3318 pagesetup_change_preview(data);
3320 /* Selecting paper in combo */
3321 count = SendDlgItemMessageW(hDlg, cmb2, CB_GETCOUNT, 0, 0);
3322 if(count != CB_ERR)
3324 WORD paperword = pagesetup_get_papersize(data);
3325 for(i = 0; i < count; i++)
3327 if(SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, i, 0) == paperword) {
3328 SendDlgItemMessageW(hDlg, cmb2, CB_SETCURSEL, i, 0);
3329 break;
3335 /********************************************************************************
3336 * pagesetup_wm_command
3337 * process WM_COMMAND message for PageSetupDlg
3339 * PARAMS
3340 * hDlg [in] Main dialog HANDLE
3341 * wParam [in] WM_COMMAND wParam
3342 * lParam [in] WM_COMMAND lParam
3343 * pda [in/out] ptr to PageSetupDataA
3346 static BOOL pagesetup_wm_command(HWND hDlg, WPARAM wParam, LPARAM lParam, pagesetup_data *data)
3348 WORD msg = HIWORD(wParam);
3349 WORD id = LOWORD(wParam);
3351 TRACE("loword (lparam) %d, wparam 0x%lx, lparam %08lx\n",
3352 LOWORD(lParam),wParam,lParam);
3353 switch (id) {
3354 case IDOK:
3355 EndDialog(hDlg, TRUE);
3356 return TRUE ;
3358 case IDCANCEL:
3359 EndDialog(hDlg, FALSE);
3360 return FALSE ;
3362 case psh3: /* Printer... */
3363 pagesetup_change_printer_dialog(hDlg, data);
3364 return TRUE;
3366 case rad1: /* Portrait */
3367 case rad2: /* Landscape */
3368 if((id == rad1 && pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE) ||
3369 (id == rad2 && pagesetup_get_orientation(data) == DMORIENT_PORTRAIT))
3371 pagesetup_set_orientation(data, (id == rad1) ? DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE);
3372 pagesetup_update_papersize(data);
3373 rotate_rect(pagesetup_get_margin_rect(data), (id == rad2));
3374 update_margin_edits(hDlg, data, 0);
3375 pagesetup_change_preview(data);
3377 break;
3378 case cmb1: /* Printer combo */
3379 if(msg == CBN_SELCHANGE)
3381 WCHAR *name;
3382 INT index = SendDlgItemMessageW(hDlg, id, CB_GETCURSEL, 0, 0);
3383 INT length = SendDlgItemMessageW(hDlg, id, CB_GETLBTEXTLEN, index, 0);
3384 name = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(length+1));
3385 SendDlgItemMessageW(hDlg, id, CB_GETLBTEXT, index, (LPARAM)name);
3386 pagesetup_change_printer(name, data);
3387 pagesetup_init_combos(hDlg, data);
3388 HeapFree(GetProcessHeap(),0,name);
3390 break;
3391 case cmb2: /* Paper combo */
3392 if(msg == CBN_SELCHANGE)
3394 DWORD paperword = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA,
3395 SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0), 0);
3396 if (paperword != CB_ERR)
3398 pagesetup_set_papersize(data, paperword);
3399 pagesetup_update_papersize(data);
3400 pagesetup_change_preview(data);
3401 } else
3402 FIXME("could not get dialog text for papersize cmbbox?\n");
3404 break;
3405 case cmb3: /* Paper Source */
3406 if(msg == CBN_SELCHANGE)
3408 WORD source = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA,
3409 SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0), 0);
3410 pagesetup_set_defaultsource(data, source);
3412 break;
3413 case psh2: /* Printer Properties button */
3414 pagesetup_printer_properties(hDlg, data);
3415 break;
3416 case edt4:
3417 case edt5:
3418 case edt6:
3419 case edt7:
3420 margin_edit_notification(hDlg, data, msg, id);
3421 break;
3423 InvalidateRect(GetDlgItem(hDlg, rct1), NULL, TRUE);
3424 return FALSE;
3427 /***********************************************************************
3428 * default_page_paint_hook
3429 * Default hook paint procedure that receives WM_PSD_* messages from the dialog box
3430 * whenever the sample page is redrawn.
3432 static UINT_PTR default_page_paint_hook(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam,
3433 const pagesetup_data *data)
3435 LPRECT lprc = (LPRECT) lParam;
3436 HDC hdc = (HDC) wParam;
3437 HPEN hpen, holdpen;
3438 LOGFONTW lf;
3439 HFONT hfont, holdfont;
3440 INT oldbkmode;
3441 TRACE("uMsg: WM_USER+%d\n",uMsg-WM_USER);
3442 /* Call user paint hook if enable */
3443 if (pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK)
3444 if (pagesetup_get_hook(data, page_paint_hook)(hwndDlg, uMsg, wParam, lParam))
3445 return TRUE;
3447 switch (uMsg) {
3448 /* LPPAGESETUPDLG in lParam */
3449 case WM_PSD_PAGESETUPDLG:
3450 /* Inform about the sample page rectangle */
3451 case WM_PSD_FULLPAGERECT:
3452 /* Inform about the margin rectangle */
3453 case WM_PSD_MINMARGINRECT:
3454 return FALSE;
3456 /* Draw dashed rectangle showing margins */
3457 case WM_PSD_MARGINRECT:
3458 hpen = CreatePen(PS_DASH, 1, GetSysColor(COLOR_3DSHADOW));
3459 holdpen = SelectObject(hdc, hpen);
3460 Rectangle(hdc, lprc->left, lprc->top, lprc->right, lprc->bottom);
3461 DeleteObject(SelectObject(hdc, holdpen));
3462 return TRUE;
3463 /* Draw the fake document */
3464 case WM_PSD_GREEKTEXTRECT:
3465 /* select a nice scalable font, because we want the text really small */
3466 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
3467 lf.lfHeight = 6; /* value chosen based on visual effect */
3468 hfont = CreateFontIndirectW(&lf);
3469 holdfont = SelectObject(hdc, hfont);
3471 /* if text not loaded, then do so now */
3472 if (wszFakeDocumentText[0] == '\0')
3473 LoadStringW(COMDLG32_hInstance,
3474 IDS_FAKEDOCTEXT,
3475 wszFakeDocumentText,
3476 ARRAY_SIZE(wszFakeDocumentText));
3478 oldbkmode = SetBkMode(hdc, TRANSPARENT);
3479 DrawTextW(hdc, wszFakeDocumentText, -1, lprc, DT_TOP|DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
3480 SetBkMode(hdc, oldbkmode);
3482 DeleteObject(SelectObject(hdc, holdfont));
3483 return TRUE;
3485 /* Envelope stamp */
3486 case WM_PSD_ENVSTAMPRECT:
3487 /* Return address */
3488 case WM_PSD_YAFULLPAGERECT:
3489 FIXME("envelope/stamp is not implemented\n");
3490 return FALSE;
3491 default:
3492 FIXME("Unknown message %x\n",uMsg);
3493 return FALSE;
3495 return TRUE;
3498 /***********************************************************************
3499 * PagePaintProc
3500 * The main paint procedure for the PageSetupDlg function.
3501 * The Page Setup dialog box includes an image of a sample page that shows how
3502 * the user's selections affect the appearance of the printed output.
3503 * The image consists of a rectangle that represents the selected paper
3504 * or envelope type, with a dotted-line rectangle representing
3505 * the current margins, and partial (Greek text) characters
3506 * to show how text looks on the printed page.
3508 * The following messages in the order sends to user hook procedure:
3509 * WM_PSD_PAGESETUPDLG Draw the contents of the sample page
3510 * WM_PSD_FULLPAGERECT Inform about the bounding rectangle
3511 * WM_PSD_MINMARGINRECT Inform about the margin rectangle (min margin?)
3512 * WM_PSD_MARGINRECT Draw the margin rectangle
3513 * WM_PSD_GREEKTEXTRECT Draw the Greek text inside the margin rectangle
3514 * If any of first three messages returns TRUE, painting done.
3516 * PARAMS:
3517 * hWnd [in] Handle to the Page Setup dialog box
3518 * uMsg [in] Received message
3520 * TODO:
3521 * WM_PSD_ENVSTAMPRECT Draw in the envelope-stamp rectangle (for envelopes only)
3522 * WM_PSD_YAFULLPAGERECT Draw the return address portion (for envelopes and other paper sizes)
3524 * RETURNS:
3525 * FALSE if all done correctly
3530 static LRESULT CALLBACK
3531 PRINTDLG_PagePaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3533 PAINTSTRUCT ps;
3534 RECT rcClient, rcMargin;
3535 HPEN hpen, holdpen;
3536 HDC hdc;
3537 HBRUSH hbrush, holdbrush;
3538 pagesetup_data *data;
3539 int papersize=0, orientation=0; /* FIXME: set these values for the user paint hook */
3540 double scalx, scaly;
3542 if (uMsg != WM_PAINT)
3543 return CallWindowProcA(lpfnStaticWndProc, hWnd, uMsg, wParam, lParam);
3545 /* Processing WM_PAINT message */
3546 data = GetPropW(hWnd, pagesetupdlg_prop);
3547 if (!data) {
3548 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3549 return FALSE;
3551 if (default_page_paint_hook(hWnd, WM_PSD_PAGESETUPDLG, MAKELONG(papersize, orientation),
3552 pagesetup_get_dlg_struct(data), data))
3553 return FALSE;
3555 hdc = BeginPaint(hWnd, &ps);
3556 GetClientRect(hWnd, &rcClient);
3558 scalx = rcClient.right / (double)pagesetup_get_papersize_pt(data)->x;
3559 scaly = rcClient.bottom / (double)pagesetup_get_papersize_pt(data)->y;
3560 rcMargin = rcClient;
3562 rcMargin.left += pagesetup_get_margin_rect(data)->left * scalx;
3563 rcMargin.top += pagesetup_get_margin_rect(data)->top * scaly;
3564 rcMargin.right -= pagesetup_get_margin_rect(data)->right * scalx;
3565 rcMargin.bottom -= pagesetup_get_margin_rect(data)->bottom * scaly;
3567 /* if the space is too small then we make sure to not draw anything */
3568 rcMargin.left = min(rcMargin.left, rcMargin.right);
3569 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3571 if (!default_page_paint_hook(hWnd, WM_PSD_FULLPAGERECT, (WPARAM)hdc, (LPARAM)&rcClient, data) &&
3572 !default_page_paint_hook(hWnd, WM_PSD_MINMARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data) )
3574 /* fill background */
3575 hbrush = GetSysColorBrush(COLOR_3DHIGHLIGHT);
3576 FillRect(hdc, &rcClient, hbrush);
3577 holdbrush = SelectObject(hdc, hbrush);
3579 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
3580 holdpen = SelectObject(hdc, hpen);
3582 /* paint left edge */
3583 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3584 LineTo(hdc, rcClient.left, rcClient.bottom-1);
3586 /* paint top edge */
3587 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3588 LineTo(hdc, rcClient.right, rcClient.top);
3590 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
3591 DeleteObject(SelectObject(hdc, hpen));
3593 /* paint right edge */
3594 MoveToEx(hdc, rcClient.right-1, rcClient.top, NULL);
3595 LineTo(hdc, rcClient.right-1, rcClient.bottom);
3597 /* paint bottom edge */
3598 MoveToEx(hdc, rcClient.left, rcClient.bottom-1, NULL);
3599 LineTo(hdc, rcClient.right, rcClient.bottom-1);
3601 DeleteObject(SelectObject(hdc, holdpen));
3602 DeleteObject(SelectObject(hdc, holdbrush));
3604 default_page_paint_hook(hWnd, WM_PSD_MARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3606 /* give text a bit of a space from the frame */
3607 InflateRect(&rcMargin, -2, -2);
3609 /* if the space is too small then we make sure to not draw anything */
3610 rcMargin.left = min(rcMargin.left, rcMargin.right);
3611 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3613 default_page_paint_hook(hWnd, WM_PSD_GREEKTEXTRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3616 EndPaint(hWnd, &ps);
3617 return FALSE;
3620 /*******************************************************
3621 * The margin edit controls are subclassed to filter
3622 * anything other than numbers and the decimal separator.
3624 static LRESULT CALLBACK pagesetup_margin_editproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3626 if (msg == WM_CHAR)
3628 WCHAR decimal = get_decimal_sep();
3629 WCHAR wc = (WCHAR)wparam;
3630 if(!iswdigit(wc) && wc != decimal && wc != VK_BACK) return 0;
3632 return CallWindowProcW(edit_wndproc, hwnd, msg, wparam, lparam);
3635 static void subclass_margin_edits(HWND hDlg)
3637 int id;
3638 WNDPROC old_proc;
3640 for(id = edt4; id <= edt7; id++)
3642 old_proc = (WNDPROC)SetWindowLongPtrW(GetDlgItem(hDlg, id),
3643 GWLP_WNDPROC,
3644 (ULONG_PTR)pagesetup_margin_editproc);
3645 InterlockedCompareExchangePointer((void**)&edit_wndproc, old_proc, NULL);
3649 /***********************************************************************
3650 * pagesetup_dlg_proc
3652 * Message handler for PageSetupDlg
3654 static INT_PTR CALLBACK pagesetup_dlg_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
3656 pagesetup_data *data;
3657 INT_PTR res = FALSE;
3658 HWND hDrawWnd;
3660 if (uMsg == WM_INITDIALOG) { /*Init dialog*/
3661 data = (pagesetup_data *)lParam;
3662 data->hDlg = hDlg;
3664 hDrawWnd = GetDlgItem(hDlg, rct1);
3665 TRACE("set property to %p\n", data);
3666 SetPropW(hDlg, pagesetupdlg_prop, data);
3667 SetPropW(hDrawWnd, pagesetupdlg_prop, data);
3668 GetWindowRect(hDrawWnd, &data->rtDrawRect); /* Calculating rect in client coordinates where paper draws */
3669 MapWindowPoints( 0, hDlg, (LPPOINT)&data->rtDrawRect, 2 );
3670 lpfnStaticWndProc = (WNDPROC)SetWindowLongPtrW(
3671 hDrawWnd,
3672 GWLP_WNDPROC,
3673 (ULONG_PTR)PRINTDLG_PagePaintProc);
3675 /* FIXME: Paint hook. Must it be at begin of initialization or at end? */
3676 res = TRUE;
3677 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3679 if (!pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam,
3680 pagesetup_get_dlg_struct(data)))
3681 FIXME("Setup page hook failed?\n");
3684 /* if printer button disabled */
3685 if (pagesetup_get_flags(data) & PSD_DISABLEPRINTER)
3686 EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
3687 /* if margin edit boxes disabled */
3688 if (pagesetup_get_flags(data) & PSD_DISABLEMARGINS)
3690 EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
3691 EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
3692 EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
3693 EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
3696 /* Set orientation radiobuttons properly */
3697 pagesetup_update_orientation_buttons(hDlg, data);
3699 /* if orientation disabled */
3700 if (pagesetup_get_flags(data) & PSD_DISABLEORIENTATION)
3702 EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
3703 EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
3706 /* We fill them out enabled or not */
3707 if (!(pagesetup_get_flags(data) & PSD_MARGINS))
3709 /* default is 1 inch */
3710 LONG size = thousandths_inch_to_size(data, 1000);
3711 SetRect(pagesetup_get_margin_rect(data), size, size, size, size);
3713 update_margin_edits(hDlg, data, 0);
3714 subclass_margin_edits(hDlg);
3715 set_margin_groupbox_title(hDlg, data);
3717 /* if paper disabled */
3718 if (pagesetup_get_flags(data) & PSD_DISABLEPAPER)
3720 EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
3721 EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
3724 /* filling combos: printer, paper, source. selecting current printer (from DEVMODEA) */
3725 pagesetup_init_combos(hDlg, data);
3726 pagesetup_update_papersize(data);
3727 pagesetup_set_defaultsource(data, DMBIN_FORMSOURCE); /* FIXME: This is the auto select bin. Is this correct? */
3729 /* Drawing paper prev */
3730 pagesetup_change_preview(data);
3731 return TRUE;
3732 } else {
3733 data = GetPropW(hDlg, pagesetupdlg_prop);
3734 if (!data)
3736 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3737 return FALSE;
3739 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3741 res = pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam, lParam);
3742 if (res) return res;
3745 switch (uMsg) {
3746 case WM_COMMAND:
3747 return pagesetup_wm_command(hDlg, wParam, lParam, data);
3749 return FALSE;
3752 static WCHAR *get_default_printer(void)
3754 WCHAR *name = NULL;
3755 DWORD len = 0;
3757 GetDefaultPrinterW(NULL, &len);
3758 if(len)
3760 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3761 GetDefaultPrinterW(name, &len);
3763 return name;
3766 static void pagesetup_dump_dlg_struct(const pagesetup_data *data)
3768 if(TRACE_ON(commdlg))
3770 char flagstr[1000] = "";
3771 const struct pd_flags *pflag = psd_flags;
3772 for( ; pflag->name; pflag++)
3774 if(pagesetup_get_flags(data) & pflag->flag)
3776 strcat(flagstr, pflag->name);
3777 strcat(flagstr, "|");
3780 TRACE("%s: (%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
3781 "hinst %p, flags %08x (%s)\n",
3782 data->unicode ? "unicode" : "ansi",
3783 data->u.dlgw, data->u.dlgw->hwndOwner, data->u.dlgw->hDevMode,
3784 data->u.dlgw->hDevNames, data->u.dlgw->hInstance,
3785 pagesetup_get_flags(data), flagstr);
3789 static void *pagesetup_get_template(pagesetup_data *data)
3791 HRSRC res;
3792 HGLOBAL tmpl_handle;
3794 if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATEHANDLE)
3796 tmpl_handle = data->u.dlgw->hPageSetupTemplate;
3798 else if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATE)
3800 if(data->unicode)
3801 res = FindResourceW(data->u.dlgw->hInstance,
3802 data->u.dlgw->lpPageSetupTemplateName, (LPWSTR)RT_DIALOG);
3803 else
3804 res = FindResourceA(data->u.dlga->hInstance,
3805 data->u.dlga->lpPageSetupTemplateName, (LPSTR)RT_DIALOG);
3806 tmpl_handle = LoadResource(data->u.dlgw->hInstance, res);
3808 else
3810 res = FindResourceW(COMDLG32_hInstance, MAKEINTRESOURCEW(PAGESETUPDLGORD),
3811 (LPWSTR)RT_DIALOG);
3812 tmpl_handle = LoadResource(COMDLG32_hInstance, res);
3814 return LockResource(tmpl_handle);
3817 static BOOL pagesetup_common(pagesetup_data *data)
3819 BOOL ret;
3820 void *tmpl;
3822 if(!pagesetup_get_dlg_struct(data))
3824 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
3825 return FALSE;
3828 pagesetup_dump_dlg_struct(data);
3830 if(data->u.dlgw->lStructSize != sizeof(PAGESETUPDLGW))
3832 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
3833 return FALSE;
3836 if ((pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK) &&
3837 (pagesetup_get_hook(data, page_paint_hook) == NULL))
3839 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
3840 return FALSE;
3843 if(!(pagesetup_get_flags(data) & (PSD_INTHOUSANDTHSOFINCHES | PSD_INHUNDREDTHSOFMILLIMETERS)))
3844 data->u.dlgw->Flags |= is_default_metric() ?
3845 PSD_INHUNDREDTHSOFMILLIMETERS : PSD_INTHOUSANDTHSOFINCHES;
3847 if (!data->u.dlgw->hDevMode || !data->u.dlgw->hDevNames)
3849 WCHAR *def = get_default_printer();
3850 if(!def)
3852 if (!(pagesetup_get_flags(data) & PSD_NOWARNING))
3854 WCHAR errstr[256];
3855 LoadStringW(COMDLG32_hInstance, PD32_NO_DEFAULT_PRINTER, errstr, 255);
3856 MessageBoxW(data->u.dlgw->hwndOwner, errstr, 0, MB_OK | MB_ICONERROR);
3858 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3859 return FALSE;
3861 pagesetup_change_printer(def, data);
3862 HeapFree(GetProcessHeap(), 0, def);
3865 if (pagesetup_get_flags(data) & PSD_RETURNDEFAULT)
3867 pagesetup_update_papersize(data);
3868 return TRUE;
3871 tmpl = pagesetup_get_template(data);
3873 ret = DialogBoxIndirectParamW(data->u.dlgw->hInstance, tmpl,
3874 data->u.dlgw->hwndOwner,
3875 pagesetup_dlg_proc, (LPARAM)data) > 0;
3876 return ret;
3879 /***********************************************************************
3880 * PageSetupDlgA (COMDLG32.@)
3882 * Displays the PAGE SETUP dialog box, which enables the user to specify
3883 * specific properties of a printed page such as
3884 * size, source, orientation and the width of the page margins.
3886 * PARAMS
3887 * setupdlg [IO] PAGESETUPDLGA struct
3889 * RETURNS
3890 * TRUE if the user pressed the OK button
3891 * FALSE if the user cancelled the window or an error occurred
3893 * NOTES
3894 * The values of hDevMode and hDevNames are filled on output and can be
3895 * changed in PAGESETUPDLG when they are passed in PageSetupDlg.
3898 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg)
3900 pagesetup_data data;
3902 data.unicode = FALSE;
3903 data.u.dlga = setupdlg;
3905 return pagesetup_common(&data);
3908 /***********************************************************************
3909 * PageSetupDlgW (COMDLG32.@)
3911 * See PageSetupDlgA.
3913 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg)
3915 pagesetup_data data;
3917 data.unicode = TRUE;
3918 data.u.dlgw = setupdlg;
3920 return pagesetup_common(&data);
3923 static void pdlgex_to_pdlg(const PRINTDLGEXW *pdlgex, PRINTDLGW *pdlg)
3925 pdlg->lStructSize = sizeof(*pdlg);
3926 pdlg->hwndOwner = pdlgex->hwndOwner;
3927 pdlg->hDevMode = pdlgex->hDevMode;
3928 pdlg->hDevNames = pdlgex->hDevNames;
3929 pdlg->hDC = pdlgex->hDC;
3930 pdlg->Flags = pdlgex->Flags;
3931 if ((pdlgex->Flags & PD_NOPAGENUMS) || !pdlgex->nPageRanges || !pdlgex->lpPageRanges)
3933 pdlg->nFromPage = 0;
3934 pdlg->nToPage = 65534;
3936 else
3938 pdlg->nFromPage = pdlgex->lpPageRanges[0].nFromPage;
3939 pdlg->nToPage = pdlgex->lpPageRanges[0].nToPage;
3941 pdlg->nMinPage = pdlgex->nMinPage;
3942 pdlg->nMaxPage = pdlgex->nMaxPage;
3943 pdlg->nCopies = pdlgex->nCopies;
3944 pdlg->hInstance = pdlgex->hInstance;
3945 pdlg->lCustData = 0;
3946 pdlg->lpfnPrintHook = NULL;
3947 pdlg->lpfnSetupHook = NULL;
3948 pdlg->lpPrintTemplateName = pdlgex->lpPrintTemplateName;
3949 pdlg->lpSetupTemplateName = NULL;
3950 pdlg->hPrintTemplate = NULL;
3951 pdlg->hSetupTemplate = NULL;
3954 /* Only copy fields that are supposed to be changed. */
3955 static void pdlg_to_pdlgex(const PRINTDLGW *pdlg, PRINTDLGEXW *pdlgex)
3957 pdlgex->hDevMode = pdlg->hDevMode;
3958 pdlgex->hDevNames = pdlg->hDevNames;
3959 pdlgex->hDC = pdlg->hDC;
3960 if (!(pdlgex->Flags & PD_NOPAGENUMS) && pdlgex->nPageRanges && pdlgex->lpPageRanges)
3962 pdlgex->lpPageRanges[0].nFromPage = pdlg->nFromPage;
3963 pdlgex->lpPageRanges[0].nToPage = pdlg->nToPage;
3965 pdlgex->nMinPage = pdlg->nMinPage;
3966 pdlgex->nMaxPage = pdlg->nMaxPage;
3967 pdlgex->nCopies = pdlg->nCopies;
3970 struct callback_data
3972 IPrintDialogCallback *callback;
3973 IObjectWithSite *object;
3976 static UINT_PTR CALLBACK pdlgex_hook_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
3978 if (msg == WM_INITDIALOG)
3980 PRINTDLGW *pd = (PRINTDLGW *)lp;
3981 struct callback_data *cb = (struct callback_data *)pd->lCustData;
3983 if (cb->callback)
3985 cb->callback->lpVtbl->SelectionChange(cb->callback);
3986 cb->callback->lpVtbl->InitDone(cb->callback);
3989 else
3991 /* FIXME: store interface pointer somewhere in window properties and call it
3992 HRESULT hres;
3993 cb->callback->lpVtbl->HandleMessage(cb->callback, hwnd, msg, wp, lp, &hres);
3997 return 0;
4000 /***********************************************************************
4001 * PrintDlgExA (COMDLG32.@)
4003 * See PrintDlgExW.
4005 * BUGS
4006 * Only a Stub
4009 HRESULT WINAPI PrintDlgExA(LPPRINTDLGEXA lppd)
4011 PRINTER_INFO_2A *pbuf;
4012 DRIVER_INFO_3A *dbuf;
4013 DEVMODEA *dm;
4014 HRESULT hr = S_OK;
4015 HANDLE hprn;
4017 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXA)))
4018 return E_INVALIDARG;
4020 if (!IsWindow(lppd->hwndOwner))
4021 return E_HANDLE;
4023 if (lppd->nStartPage != START_PAGE_GENERAL)
4025 if (!lppd->nPropertyPages)
4026 return E_INVALIDARG;
4028 FIXME("custom property sheets (%d at %p) not supported\n", lppd->nPropertyPages, lppd->lphPropertyPages);
4031 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
4032 if (!(lppd->Flags & PD_NOPAGENUMS) && (!lppd->nMaxPageRanges || !lppd->lpPageRanges))
4034 return E_INVALIDARG;
4037 if (lppd->Flags & PD_RETURNDEFAULT)
4039 if (lppd->hDevMode || lppd->hDevNames)
4041 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
4042 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4043 return E_INVALIDARG;
4045 if (!PRINTDLG_OpenDefaultPrinter(&hprn))
4047 WARN("Can't find default printer\n");
4048 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
4049 return E_FAIL;
4052 pbuf = get_printer_infoA(hprn);
4053 if (!pbuf)
4055 ClosePrinter(hprn);
4056 return E_FAIL;
4059 dbuf = get_driver_infoA(hprn);
4060 if (!dbuf)
4062 HeapFree(GetProcessHeap(), 0, pbuf);
4063 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4064 ClosePrinter(hprn);
4065 return E_FAIL;
4067 dm = pbuf->pDevMode;
4069 else
4071 PRINTDLGA pdlg;
4072 struct callback_data cb_data = { 0 };
4074 FIXME("(%p) semi-stub\n", lppd);
4076 if (lppd->lpCallback)
4078 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IPrintDialogCallback, (void **)&cb_data.callback);
4079 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IObjectWithSite, (void **)&cb_data.object);
4083 * PRINTDLGEXA/W and PRINTDLGA/W layout is the same for A and W variants.
4085 pdlgex_to_pdlg((const PRINTDLGEXW *)lppd, (PRINTDLGW *)&pdlg);
4086 pdlg.Flags |= PD_ENABLEPRINTHOOK;
4087 pdlg.lpfnPrintHook = pdlgex_hook_proc;
4088 pdlg.lCustData = (LPARAM)&cb_data;
4090 if (PrintDlgA(&pdlg))
4092 pdlg_to_pdlgex((const PRINTDLGW *)&pdlg, (PRINTDLGEXW *)lppd);
4093 lppd->dwResultAction = PD_RESULT_PRINT;
4095 else
4096 lppd->dwResultAction = PD_RESULT_CANCEL;
4098 if (cb_data.callback)
4099 cb_data.callback->lpVtbl->Release(cb_data.callback);
4100 if (cb_data.object)
4101 cb_data.object->lpVtbl->Release(cb_data.object);
4103 return S_OK;
4106 ClosePrinter(hprn);
4108 PRINTDLG_CreateDevNames(&(lppd->hDevNames), dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName);
4109 if (!lppd->hDevNames)
4110 hr = E_FAIL;
4112 lppd->hDevMode = update_devmode_handleA(lppd->hDevMode, dm);
4113 if (hr == S_OK && lppd->hDevMode) {
4114 if (lppd->Flags & PD_RETURNDC) {
4115 lppd->hDC = CreateDCA(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4116 if (!lppd->hDC)
4117 hr = E_FAIL;
4119 else if (lppd->Flags & PD_RETURNIC) {
4120 lppd->hDC = CreateICA(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4121 if (!lppd->hDC)
4122 hr = E_FAIL;
4125 else
4126 hr = E_FAIL;
4128 HeapFree(GetProcessHeap(), 0, pbuf);
4129 HeapFree(GetProcessHeap(), 0, dbuf);
4131 return hr;
4134 /***********************************************************************
4135 * PrintDlgExW (COMDLG32.@)
4137 * Display the property sheet style PRINT dialog box
4139 * PARAMS
4140 * lppd [IO] ptr to PRINTDLGEX struct
4142 * RETURNS
4143 * Success: S_OK
4144 * Failure: One of the following COM error codes:
4145 * E_OUTOFMEMORY Insufficient memory.
4146 * E_INVALIDARG One or more arguments are invalid.
4147 * E_POINTER Invalid pointer.
4148 * E_HANDLE Invalid handle.
4149 * E_FAIL Unspecified error.
4151 * NOTES
4152 * This Dialog enables the user to specify specific properties of the print job.
4153 * The property sheet can also have additional application-specific and
4154 * driver-specific property pages.
4156 * BUGS
4157 * Not fully implemented
4160 HRESULT WINAPI PrintDlgExW(LPPRINTDLGEXW lppd)
4162 PRINTER_INFO_2W *pbuf;
4163 DRIVER_INFO_3W *dbuf;
4164 DEVMODEW *dm;
4165 HRESULT hr = S_OK;
4166 HANDLE hprn;
4168 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXW))) {
4169 return E_INVALIDARG;
4172 if (!IsWindow(lppd->hwndOwner)) {
4173 return E_HANDLE;
4176 if (lppd->nStartPage != START_PAGE_GENERAL)
4178 if (!lppd->nPropertyPages)
4179 return E_INVALIDARG;
4181 FIXME("custom property sheets (%d at %p) not supported\n", lppd->nPropertyPages, lppd->lphPropertyPages);
4184 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
4185 if (!(lppd->Flags & PD_NOPAGENUMS) && (!lppd->nMaxPageRanges || !lppd->lpPageRanges))
4187 return E_INVALIDARG;
4190 if (lppd->Flags & PD_RETURNDEFAULT) {
4192 if (lppd->hDevMode || lppd->hDevNames) {
4193 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
4194 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4195 return E_INVALIDARG;
4197 if (!PRINTDLG_OpenDefaultPrinter(&hprn)) {
4198 WARN("Can't find default printer\n");
4199 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
4200 return E_FAIL;
4203 pbuf = get_printer_infoW(hprn);
4204 if (!pbuf)
4206 ClosePrinter(hprn);
4207 return E_FAIL;
4210 dbuf = get_driver_infoW(hprn);
4211 if (!dbuf)
4213 HeapFree(GetProcessHeap(), 0, pbuf);
4214 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4215 ClosePrinter(hprn);
4216 return E_FAIL;
4218 dm = pbuf->pDevMode;
4220 else
4222 PRINTDLGW pdlg;
4223 struct callback_data cb_data = { 0 };
4225 FIXME("(%p) semi-stub\n", lppd);
4227 if (lppd->lpCallback)
4229 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IPrintDialogCallback, (void **)&cb_data.callback);
4230 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IObjectWithSite, (void **)&cb_data.object);
4233 pdlgex_to_pdlg(lppd, &pdlg);
4234 pdlg.Flags |= PD_ENABLEPRINTHOOK;
4235 pdlg.lpfnPrintHook = pdlgex_hook_proc;
4236 pdlg.lCustData = (LPARAM)&cb_data;
4238 if (PrintDlgW(&pdlg))
4240 pdlg_to_pdlgex(&pdlg, lppd);
4241 lppd->dwResultAction = PD_RESULT_PRINT;
4243 else
4244 lppd->dwResultAction = PD_RESULT_CANCEL;
4246 if (cb_data.callback)
4247 cb_data.callback->lpVtbl->Release(cb_data.callback);
4248 if (cb_data.object)
4249 cb_data.object->lpVtbl->Release(cb_data.object);
4251 return S_OK;
4254 ClosePrinter(hprn);
4256 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames), dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName);
4257 if (!lppd->hDevNames)
4258 hr = E_FAIL;
4260 lppd->hDevMode = update_devmode_handleW(lppd->hDevMode, dm);
4261 if (hr == S_OK && lppd->hDevMode) {
4262 if (lppd->Flags & PD_RETURNDC) {
4263 lppd->hDC = CreateDCW(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4264 if (!lppd->hDC)
4265 hr = E_FAIL;
4267 else if (lppd->Flags & PD_RETURNIC) {
4268 lppd->hDC = CreateICW(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4269 if (!lppd->hDC)
4270 hr = E_FAIL;
4273 else
4274 hr = E_FAIL;
4276 HeapFree(GetProcessHeap(), 0, pbuf);
4277 HeapFree(GetProcessHeap(), 0, dbuf);
4279 return hr;