ws2_32/tests: Avoid sizeof in traces.
[wine] / dlls / comdlg32 / printdlg.c
1 /*
2  * COMMDLG - Print Dialog
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  * Copyright 1999 Klaas van Gend
7  * Copyright 2000 Huw D M Davies
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winspool.h"
37 #include "winerror.h"
38
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 #include "commdlg.h"
43 #include "dlgs.h"
44 #include "cderr.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
47
48 #include "cdlg.h"
49 #include "printdlg.h"
50
51 /* Yes these constants are the same, but we're just copying win98 */
52 #define UPDOWN_ID 0x270f
53 #define MAX_COPIES 9999
54
55 /* Debugging info */
56 static const struct pd_flags psd_flags[] = {
57   {PSD_MINMARGINS,"PSD_MINMARGINS"},
58   {PSD_MARGINS,"PSD_MARGINS"},
59   {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
60   {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
61   {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
62   {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
63   {PSD_NOWARNING,"PSD_NOWARNING"},
64   {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
65   {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
66   {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
67   {PSD_SHOWHELP,"PSD_SHOWHELP"},
68   {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
69   {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
70   {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
71   {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
72   {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
73   {-1, NULL}
74 };
75
76 /* address of wndproc for subclassed Static control */
77 static WNDPROC lpfnStaticWndProc;
78 static WNDPROC edit_wndproc;
79 /* the text of the fake document to render for the Page Setup dialog */
80 static WCHAR wszFakeDocumentText[1024];
81 static const WCHAR pd32_collateW[] = { 'P', 'D', '3', '2', '_', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
82 static const WCHAR pd32_nocollateW[] = { 'P', 'D', '3', '2', '_', 'N', 'O', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
83 static const WCHAR pd32_portraitW[] = { 'P', 'D', '3', '2', '_', 'P', 'O', 'R', 'T', 'R', 'A', 'I', 'T', 0 };
84 static const WCHAR pd32_landscapeW[] = { 'P', 'D', '3', '2', '_', 'L', 'A', 'N', 'D', 'S', 'C', 'A', 'P', 'E', 0 };
85 static const WCHAR printdlg_prop[] = {'_','_','W','I','N','E','_','P','R','I','N','T','D','L','G','D','A','T','A',0};
86 static const WCHAR pagesetupdlg_prop[] = { '_', '_', 'W', 'I', 'N', 'E', '_', 'P', 'A', 'G', 'E',
87                                            'S', 'E', 'T', 'U', 'P', 'D', 'L', 'G', 'D', 'A', 'T', 'A', 0 };
88
89
90 static LPWSTR strdupW(LPCWSTR p)
91 {
92     LPWSTR ret;
93     DWORD len;
94
95     if(!p) return NULL;
96     len = (strlenW(p) + 1) * sizeof(WCHAR);
97     ret = HeapAlloc(GetProcessHeap(), 0, len);
98     memcpy(ret, p, len);
99     return ret;
100 }
101
102 /***********************************************************
103  * convert_to_devmodeA
104  *
105  * Creates an ansi copy of supplied devmode
106  */
107 static DEVMODEA *convert_to_devmodeA(const DEVMODEW *dmW)
108 {
109     DEVMODEA *dmA;
110     DWORD size;
111
112     if (!dmW) return NULL;
113     size = dmW->dmSize - CCHDEVICENAME -
114                         ((dmW->dmSize > FIELD_OFFSET(DEVMODEW, dmFormName)) ? CCHFORMNAME : 0);
115
116     dmA = HeapAlloc(GetProcessHeap(), 0, size + dmW->dmDriverExtra);
117     if (!dmA) return NULL;
118
119     WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1,
120                         (LPSTR)dmA->dmDeviceName, CCHDEVICENAME, NULL, NULL);
121
122     if (FIELD_OFFSET(DEVMODEW, dmFormName) >= dmW->dmSize)
123     {
124         memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
125                dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
126     }
127     else
128     {
129         memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
130                FIELD_OFFSET(DEVMODEW, dmFormName) - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
131         WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1,
132                             (LPSTR)dmA->dmFormName, CCHFORMNAME, NULL, NULL);
133
134         memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmLogPixels));
135     }
136
137     dmA->dmSize = size;
138     memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize, dmW->dmDriverExtra);
139     return dmA;
140 }
141
142 /***********************************************************************
143  *    PRINTDLG_OpenDefaultPrinter
144  *
145  * Returns a winspool printer handle to the default printer in *hprn
146  * Caller must call ClosePrinter on the handle
147  *
148  * Returns TRUE on success else FALSE
149  */
150 BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
151 {
152     WCHAR buf[260];
153     DWORD dwBufLen = sizeof(buf) / sizeof(buf[0]);
154     BOOL res;
155     if(!GetDefaultPrinterW(buf, &dwBufLen))
156         return FALSE;
157     res = OpenPrinterW(buf, hprn, NULL);
158     if (!res)
159         WARN("Could not open printer %s\n", debugstr_w(buf));
160     return res;
161 }
162
163 /***********************************************************************
164  *    PRINTDLG_SetUpPrinterListCombo
165  *
166  * Initializes printer list combox.
167  * hDlg:  HWND of dialog
168  * id:    Control id of combo
169  * name:  Name of printer to select
170  *
171  * Initializes combo with list of available printers.  Selects printer 'name'
172  * If name is NULL or does not exist select the default printer.
173  *
174  * Returns number of printers added to list.
175  */
176 INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
177 {
178     DWORD needed, num;
179     INT i;
180     LPPRINTER_INFO_2A pi;
181     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
182     pi = HeapAlloc(GetProcessHeap(), 0, needed);
183     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
184                   &num);
185
186     SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
187     
188     for(i = 0; i < num; i++) {
189         SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
190                             (LPARAM)pi[i].pPrinterName );
191     }
192     HeapFree(GetProcessHeap(), 0, pi);
193     if(!name ||
194        (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
195                                 (LPARAM)name)) == CB_ERR) {
196
197         char buf[260];
198         DWORD dwBufLen = sizeof(buf);
199         if (name != NULL)
200             WARN("Can't find %s in printer list so trying to find default\n",
201                 debugstr_a(name));
202         if(!GetDefaultPrinterA(buf, &dwBufLen))
203             return num;
204         i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
205         if(i == CB_ERR)
206             FIXME("Can't find default printer in printer list\n");
207     }
208     SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
209     return num;
210 }
211
212 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
213 {
214     DWORD needed, num;
215     INT i;
216     LPPRINTER_INFO_2W pi;
217     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
218     pi = HeapAlloc(GetProcessHeap(), 0, needed);
219     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
220                   &num);
221
222     for(i = 0; i < num; i++) {
223         SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
224                             (LPARAM)pi[i].pPrinterName );
225     }
226     HeapFree(GetProcessHeap(), 0, pi);
227     if(!name ||
228        (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
229                                 (LPARAM)name)) == CB_ERR) {
230         WCHAR buf[260];
231         DWORD dwBufLen = sizeof(buf)/sizeof(buf[0]);
232         if (name != NULL)
233             WARN("Can't find %s in printer list so trying to find default\n",
234                 debugstr_w(name));
235         if(!GetDefaultPrinterW(buf, &dwBufLen))
236             return num;
237         i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
238         if(i == CB_ERR)
239             TRACE("Can't find default printer in printer list\n");
240     }
241     SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
242     return num;
243 }
244
245 /***********************************************************************
246  *             PRINTDLG_CreateDevNames          [internal]
247  *
248  *
249  *   creates a DevNames structure.
250  *
251  *  (NB. when we handle unicode the offsets will be in wchars).
252  */
253 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
254                                     const char* DeviceName, const char* OutputPort)
255 {
256     long size;
257     char*   pDevNamesSpace;
258     char*   pTempPtr;
259     LPDEVNAMES lpDevNames;
260     char buf[260];
261     DWORD dwBufLen = sizeof(buf);
262
263     size = strlen(DeviceDriverName) + 1
264             + strlen(DeviceName) + 1
265             + strlen(OutputPort) + 1
266             + sizeof(DEVNAMES);
267
268     if(*hmem)
269         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
270     else
271         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
272     if (*hmem == 0)
273         return FALSE;
274
275     pDevNamesSpace = GlobalLock(*hmem);
276     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
277
278     pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
279     strcpy(pTempPtr, DeviceDriverName);
280     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
281
282     pTempPtr += strlen(DeviceDriverName) + 1;
283     strcpy(pTempPtr, DeviceName);
284     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
285
286     pTempPtr += strlen(DeviceName) + 1;
287     strcpy(pTempPtr, OutputPort);
288     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
289
290     GetDefaultPrinterA(buf, &dwBufLen);
291     lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
292     GlobalUnlock(*hmem);
293     return TRUE;
294 }
295
296 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
297                                     LPCWSTR DeviceName, LPCWSTR OutputPort)
298 {
299     long size;
300     LPWSTR   pDevNamesSpace;
301     LPWSTR   pTempPtr;
302     LPDEVNAMES lpDevNames;
303     WCHAR bufW[260];
304     DWORD dwBufLen = sizeof(bufW) / sizeof(WCHAR);
305
306     size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
307             + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
308             + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
309             + sizeof(DEVNAMES);
310
311     if(*hmem)
312         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
313     else
314         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
315     if (*hmem == 0)
316         return FALSE;
317
318     pDevNamesSpace = GlobalLock(*hmem);
319     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
320
321     pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
322     lstrcpyW(pTempPtr, DeviceDriverName);
323     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
324
325     pTempPtr += lstrlenW(DeviceDriverName) + 1;
326     lstrcpyW(pTempPtr, DeviceName);
327     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
328
329     pTempPtr += lstrlenW(DeviceName) + 1;
330     lstrcpyW(pTempPtr, OutputPort);
331     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
332
333     GetDefaultPrinterW(bufW, &dwBufLen);
334     lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
335     GlobalUnlock(*hmem);
336     return TRUE;
337 }
338
339 /***********************************************************************
340  *             PRINTDLG_UpdatePrintDlg          [internal]
341  *
342  *
343  *   updates the PrintDlg structure for return values.
344  *
345  * RETURNS
346  *   FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
347  *   TRUE  if successful.
348  */
349 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
350                                     PRINT_PTRA* PrintStructures)
351 {
352     LPPRINTDLGA       lppd = PrintStructures->lpPrintDlg;
353     PDEVMODEA         lpdm = PrintStructures->lpDevMode;
354     LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
355
356
357     if(!lpdm) {
358         FIXME("No lpdm ptr?\n");
359         return FALSE;
360     }
361
362
363     if(!(lppd->Flags & PD_PRINTSETUP)) {
364         /* check whether nFromPage and nToPage are within range defined by
365          * nMinPage and nMaxPage
366          */
367         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
368             WORD nToPage;
369             WORD nFromPage;
370             BOOL translated;
371             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
372             nToPage   = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
373
374             /* if no ToPage value is entered, use the FromPage value */
375             if(!translated) nToPage = nFromPage;
376
377             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
378                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
379                 WCHAR resourcestr[256];
380                 WCHAR resultstr[256];
381                 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
382                 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
383                 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
384                 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
385                 return FALSE;
386             }
387             lppd->nFromPage = nFromPage;
388             lppd->nToPage   = nToPage;
389             lppd->Flags |= PD_PAGENUMS;
390         }
391         else
392             lppd->Flags &= ~PD_PAGENUMS;
393
394         if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
395             lppd->Flags |= PD_SELECTION;
396         else
397             lppd->Flags &= ~PD_SELECTION;
398
399         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
400             static char file[] = "FILE:";
401             lppd->Flags |= PD_PRINTTOFILE;
402             pi->pPortName = file;
403         }
404
405         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
406             FIXME("Collate lppd not yet implemented as output\n");
407         }
408
409         /* set PD_Collate and nCopies */
410         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
411           /*  The application doesn't support multiple copies or collate...
412            */
413             lppd->Flags &= ~PD_COLLATE;
414             lppd->nCopies = 1;
415           /* if the printer driver supports it... store info there
416            * otherwise no collate & multiple copies !
417            */
418             if (lpdm->dmFields & DM_COLLATE)
419                 lpdm->dmCollate =
420                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
421             if (lpdm->dmFields & DM_COPIES)
422                 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
423         } else {
424             /* Application is responsible for multiple copies */
425             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
426                 lppd->Flags |= PD_COLLATE;
427             else
428                lppd->Flags &= ~PD_COLLATE;
429             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
430             /* multiple copies already included in the document. Driver must print only one copy */
431             lpdm->u1.s1.dmCopies = 1;
432         }
433
434         /* Print quality, PrintDlg16 */
435         if(GetDlgItem(hDlg, cmb1))
436         {
437             HWND hQuality = GetDlgItem(hDlg, cmb1);
438             int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
439
440             if(Sel != CB_ERR)
441             {
442                 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
443                 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
444                 lpdm->u1.s1.dmPrintQuality = LOWORD(dpi);
445                 lpdm->dmYResolution = HIWORD(dpi);
446             }
447         }
448     }
449     return TRUE;
450 }
451
452 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
453                                     PRINT_PTRW* PrintStructures)
454 {
455     LPPRINTDLGW       lppd = PrintStructures->lpPrintDlg;
456     PDEVMODEW         lpdm = PrintStructures->lpDevMode;
457     LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
458
459
460     if(!lpdm) {
461         FIXME("No lpdm ptr?\n");
462         return FALSE;
463     }
464
465
466     if(!(lppd->Flags & PD_PRINTSETUP)) {
467         /* check whether nFromPage and nToPage are within range defined by
468          * nMinPage and nMaxPage
469          */
470         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
471             WORD nToPage;
472             WORD nFromPage;
473             BOOL translated;
474             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
475             nToPage   = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
476
477             /* if no ToPage value is entered, use the FromPage value */
478             if(!translated) nToPage = nFromPage;
479
480             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
481                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
482                 WCHAR resourcestr[256];
483                 WCHAR resultstr[256];
484                 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
485                             resourcestr, 255);
486                 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
487                 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
488                             resourcestr, 255);
489                 MessageBoxW(hDlg, resultstr, resourcestr,
490                             MB_OK | MB_ICONWARNING);
491                 return FALSE;
492             }
493             lppd->nFromPage = nFromPage;
494             lppd->nToPage   = nToPage;
495             lppd->Flags |= PD_PAGENUMS;
496         }
497         else
498             lppd->Flags &= ~PD_PAGENUMS;
499
500         if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
501             lppd->Flags |= PD_SELECTION;
502         else
503             lppd->Flags &= ~PD_SELECTION;
504
505         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
506             static WCHAR file[] = {'F','I','L','E',':',0};
507             lppd->Flags |= PD_PRINTTOFILE;
508             pi->pPortName = file;
509         }
510
511         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
512             FIXME("Collate lppd not yet implemented as output\n");
513         }
514
515         /* set PD_Collate and nCopies */
516         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
517           /*  The application doesn't support multiple copies or collate...
518            */
519             lppd->Flags &= ~PD_COLLATE;
520             lppd->nCopies = 1;
521           /* if the printer driver supports it... store info there
522            * otherwise no collate & multiple copies !
523            */
524             if (lpdm->dmFields & DM_COLLATE)
525                 lpdm->dmCollate =
526                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
527             if (lpdm->dmFields & DM_COPIES)
528                 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
529         } else {
530             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
531                 lppd->Flags |= PD_COLLATE;
532             else
533                lppd->Flags &= ~PD_COLLATE;
534             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
535         }
536     }
537     return TRUE;
538 }
539
540 /************************************************************************
541  * PRINTDLG_SetUpPaperComboBox
542  *
543  * Initialize either the papersize or inputslot combos of the Printer Setup
544  * dialog.  We store the associated word (eg DMPAPER_A4) as the item data.
545  * We also try to re-select the old selection.
546  */
547 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
548                                         int   nIDComboBox,
549                                         char* PrinterName,
550                                         char* PortName,
551                                         LPDEVMODEA dm)
552 {
553     int     i;
554     int     NrOfEntries;
555     char*   Names;
556     WORD*   Words;
557     DWORD   Sel;
558     WORD    oldWord = 0;
559     int     NamesSize;
560     int     fwCapability_Names;
561     int     fwCapability_Words;
562
563     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
564
565     /* query the dialog box for the current selected value */
566     Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
567     if(Sel != CB_ERR) {
568         /* we enter here only if a different printer is selected after
569          * the Print Setup dialog is opened. The current settings are
570          * stored into the newly selected printer.
571          */
572         oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
573                                       Sel, 0);
574         if (dm) {
575             if (nIDComboBox == cmb2)
576                 dm->u1.s1.dmPaperSize = oldWord;
577             else
578                 dm->u1.s1.dmDefaultSource = oldWord;
579         }
580     }
581     else {
582         /* we enter here only when the Print setup dialog is initially
583          * opened. In this case the settings are restored from when
584          * the dialog was last closed.
585          */
586         if (dm) {
587             if (nIDComboBox == cmb2)
588                 oldWord = dm->u1.s1.dmPaperSize;
589             else
590                 oldWord = dm->u1.s1.dmDefaultSource;
591         }
592     }
593
594     if (nIDComboBox == cmb2) {
595          NamesSize          = 64;
596          fwCapability_Names = DC_PAPERNAMES;
597          fwCapability_Words = DC_PAPERS;
598     } else {
599          nIDComboBox        = cmb3;
600          NamesSize          = 24;
601          fwCapability_Names = DC_BINNAMES;
602          fwCapability_Words = DC_BINS;
603     }
604
605     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
606                                       fwCapability_Names, NULL, dm);
607     if (NrOfEntries == 0)
608          WARN("no Name Entries found!\n");
609     else if (NrOfEntries < 0)
610          return FALSE;
611
612     if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
613        != NrOfEntries) {
614         ERR("Number of caps is different\n");
615         NrOfEntries = 0;
616     }
617
618     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
619     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
620     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
621                                       fwCapability_Names, Names, dm);
622     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
623                                       fwCapability_Words, (LPSTR)Words, dm);
624
625     /* reset any current content in the combobox */
626     SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
627
628     /* store new content */
629     for (i = 0; i < NrOfEntries; i++) {
630         DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
631                                         (LPARAM)(&Names[i*NamesSize]) );
632         SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
633                             Words[i]);
634     }
635
636     /* Look for old selection - can't do this is previous loop since
637        item order will change as more items are added */
638     Sel = 0;
639     for (i = 0; i < NrOfEntries; i++) {
640         if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
641            oldWord) {
642             Sel = i;
643             break;
644         }
645     }
646     SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
647
648     HeapFree(GetProcessHeap(),0,Words);
649     HeapFree(GetProcessHeap(),0,Names);
650     return TRUE;
651 }
652
653 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
654                                         int   nIDComboBox,
655                                         const WCHAR* PrinterName,
656                                         const WCHAR* PortName,
657                                         LPDEVMODEW dm)
658 {
659     int     i;
660     int     NrOfEntries;
661     WCHAR*  Names;
662     WORD*   Words;
663     DWORD   Sel;
664     WORD    oldWord = 0;
665     int     NamesSize;
666     int     fwCapability_Names;
667     int     fwCapability_Words;
668
669     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
670
671     /* query the dialog box for the current selected value */
672     Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
673     if(Sel != CB_ERR) {
674         /* we enter here only if a different printer is selected after
675          * the Print Setup dialog is opened. The current settings are
676          * stored into the newly selected printer.
677          */
678         oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
679                                       Sel, 0);
680         if (dm) {
681             if (nIDComboBox == cmb2)
682                 dm->u1.s1.dmPaperSize = oldWord;
683             else
684                 dm->u1.s1.dmDefaultSource = oldWord;
685         }
686     }
687     else {
688         /* we enter here only when the Print setup dialog is initially
689          * opened. In this case the settings are restored from when
690          * the dialog was last closed.
691          */
692         if (dm) {
693             if (nIDComboBox == cmb2)
694                 oldWord = dm->u1.s1.dmPaperSize;
695             else
696                 oldWord = dm->u1.s1.dmDefaultSource;
697         }
698     }
699
700     if (nIDComboBox == cmb2) {
701          NamesSize          = 64;
702          fwCapability_Names = DC_PAPERNAMES;
703          fwCapability_Words = DC_PAPERS;
704     } else {
705          nIDComboBox        = cmb3;
706          NamesSize          = 24;
707          fwCapability_Names = DC_BINNAMES;
708          fwCapability_Words = DC_BINS;
709     }
710
711     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
712                                       fwCapability_Names, NULL, dm);
713     if (NrOfEntries == 0)
714          WARN("no Name Entries found!\n");
715     else if (NrOfEntries < 0)
716          return FALSE;
717
718     if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
719        != NrOfEntries) {
720         ERR("Number of caps is different\n");
721         NrOfEntries = 0;
722     }
723
724     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
725     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
726     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
727                                       fwCapability_Names, Names, dm);
728     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
729                                       fwCapability_Words, Words, dm);
730
731     /* reset any current content in the combobox */
732     SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
733
734     /* store new content */
735     for (i = 0; i < NrOfEntries; i++) {
736         DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
737                                         (LPARAM)(&Names[i*NamesSize]) );
738         SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
739                             Words[i]);
740     }
741
742     /* Look for old selection - can't do this is previous loop since
743        item order will change as more items are added */
744     Sel = 0;
745     for (i = 0; i < NrOfEntries; i++) {
746         if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
747            oldWord) {
748             Sel = i;
749             break;
750         }
751     }
752     SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
753
754     HeapFree(GetProcessHeap(),0,Words);
755     HeapFree(GetProcessHeap(),0,Names);
756     return TRUE;
757 }
758
759
760 /***********************************************************************
761  *               PRINTDLG_UpdatePrinterInfoTexts               [internal]
762  */
763 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
764 {
765     char   StatusMsg[256];
766     char   ResourceString[256];
767     int    i;
768
769     /* Status Message */
770     StatusMsg[0]='\0';
771
772     /* add all status messages */
773     for (i = 0; i < 25; i++) {
774         if (pi->Status & (1<<i)) {
775             LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
776                         ResourceString, 255);
777             strcat(StatusMsg,ResourceString);
778         }
779     }
780     /* append "ready" */
781     /* FIXME: status==ready must only be appended if really so.
782               but how to detect? */
783     LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
784                 ResourceString, 255);
785     strcat(StatusMsg,ResourceString);
786     SetDlgItemTextA(hDlg, stc12, StatusMsg);
787
788     /* set all other printer info texts */
789     SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
790     
791     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
792         SetDlgItemTextA(hDlg, stc14, pi->pLocation);
793     else
794         SetDlgItemTextA(hDlg, stc14, pi->pPortName);
795     SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
796     return;
797 }
798
799 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
800 {
801     WCHAR   StatusMsg[256];
802     WCHAR   ResourceString[256];
803     static const WCHAR emptyW[] = {0};
804     int    i;
805
806     /* Status Message */
807     StatusMsg[0]='\0';
808
809     /* add all status messages */
810     for (i = 0; i < 25; i++) {
811         if (pi->Status & (1<<i)) {
812             LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
813                         ResourceString, 255);
814             lstrcatW(StatusMsg,ResourceString);
815         }
816     }
817     /* append "ready" */
818     /* FIXME: status==ready must only be appended if really so.
819               but how to detect? */
820     LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
821                 ResourceString, 255);
822     lstrcatW(StatusMsg,ResourceString);
823     SetDlgItemTextW(hDlg, stc12, StatusMsg);
824
825     /* set all other printer info texts */
826     SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
827     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
828         SetDlgItemTextW(hDlg, stc14, pi->pLocation);
829     else
830         SetDlgItemTextW(hDlg, stc14, pi->pPortName);
831     SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : emptyW);
832 }
833
834
835 /*******************************************************************
836  *
837  *                 PRINTDLG_ChangePrinter
838  *
839  */
840 BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name,
841                                    PRINT_PTRA *PrintStructures)
842 {
843     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
844     LPDEVMODEA lpdm = NULL;
845     LONG dmSize;
846     DWORD needed;
847     HANDLE hprn;
848
849     HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
850     HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
851     if(!OpenPrinterA(name, &hprn, NULL)) {
852         ERR("Can't open printer %s\n", name);
853         return FALSE;
854     }
855     GetPrinterA(hprn, 2, NULL, 0, &needed);
856     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
857     GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
858                 &needed);
859     GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
860     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
861     if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
862             needed, &needed)) {
863         ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
864         return FALSE;
865     }
866     ClosePrinter(hprn);
867
868     PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
869
870     HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
871     PrintStructures->lpDevMode = NULL;
872
873     dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
874     if(dmSize == -1) {
875         ERR("DocumentProperties fails on %s\n", debugstr_a(name));
876         return FALSE;
877     }
878     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
879     dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
880                                  DM_OUT_BUFFER);
881     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
882                           !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
883                                      (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
884       /* Supplied devicemode matches current printer so try to use it */
885         DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
886                             DM_OUT_BUFFER | DM_IN_BUFFER);
887     }
888     if(lpdm)
889         GlobalUnlock(lppd->hDevMode);
890
891     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
892
893     if(!(lppd->Flags & PD_PRINTSETUP)) {
894         /* Print range (All/Range/Selection) */
895         if(lppd->nFromPage != 0xffff)
896             SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
897         if(lppd->nToPage != 0xffff)
898             SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
899
900         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
901         if (lppd->Flags & PD_NOSELECTION)
902             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
903         else
904             if (lppd->Flags & PD_SELECTION)
905                 CheckRadioButton(hDlg, rad1, rad3, rad2);
906         if (lppd->Flags & PD_NOPAGENUMS) {
907             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
908             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
909             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
910             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
911             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
912         } else {
913             if (lppd->Flags & PD_PAGENUMS)
914                 CheckRadioButton(hDlg, rad1, rad3, rad3);
915         }
916
917         /* Collate pages
918          *
919          * FIXME: The ico3 is not displayed for some reason. I don't know why.
920          */
921         if (lppd->Flags & PD_COLLATE) {
922             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
923                                 (LPARAM)PrintStructures->hCollateIcon);
924             CheckDlgButton(hDlg, chx2, 1);
925         } else {
926             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
927                                 (LPARAM)PrintStructures->hNoCollateIcon);
928             CheckDlgButton(hDlg, chx2, 0);
929         }
930
931         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
932           /* if printer doesn't support it: no Collate */
933             if (!(lpdm->dmFields & DM_COLLATE)) {
934                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
935                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
936             }
937         }
938
939         /* nCopies */
940         {
941           INT copies;
942           if (lppd->hDevMode == 0)
943               copies = lppd->nCopies;
944           else
945               copies = lpdm->u1.s1.dmCopies;
946           if(copies == 0) copies = 1;
947           else if(copies < 0) copies = MAX_COPIES;
948           SetDlgItemInt(hDlg, edt3, copies, FALSE);
949         }
950
951         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
952           /* if printer doesn't support it: no nCopies */
953             if (!(lpdm->dmFields & DM_COPIES)) {
954                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
955                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
956             }
957         }
958
959         /* print to file */
960         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
961         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
962             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
963         if (lppd->Flags & PD_HIDEPRINTTOFILE)
964             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
965
966         /* Fill print quality combo, PrintDlg16 */
967         if(GetDlgItem(hDlg, cmb1))
968         {
969             DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
970                                                        PrintStructures->lpPrinterInfo->pPortName,
971                                                        DC_ENUMRESOLUTIONS, NULL, lpdm);
972
973             if(numResolutions != -1)
974             {
975                 HWND hQuality = GetDlgItem(hDlg, cmb1);
976                 LONG* Resolutions;
977                 char buf[255];
978                 DWORD i;
979                 int dpiX, dpiY;
980                 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
981                                            PrintStructures->lpPrinterInfo->pPrinterName,
982                                            0, lpdm);
983
984                 Resolutions = HeapAlloc(GetProcessHeap(), 0, numResolutions*sizeof(LONG)*2);
985                 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
986                                     PrintStructures->lpPrinterInfo->pPortName,
987                                     DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
988
989                 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
990                 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
991                 DeleteDC(hPrinterDC);
992
993                 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
994                 for(i = 0; i < (numResolutions * 2); i += 2)
995                 {
996                     BOOL IsDefault = FALSE;
997                     LRESULT Index;
998
999                     if(Resolutions[i] == Resolutions[i+1])
1000                     {
1001                         if(dpiX == Resolutions[i])
1002                             IsDefault = TRUE;
1003                         sprintf(buf, "%d dpi", Resolutions[i]);
1004                     } else
1005                     {
1006                         if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1007                             IsDefault = TRUE;
1008                         sprintf(buf, "%d dpi x %d dpi", Resolutions[i], Resolutions[i+1]);
1009                     }
1010
1011                     Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1012
1013                     if(IsDefault)
1014                         SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1015
1016                     SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1017                 }
1018                 HeapFree(GetProcessHeap(), 0, Resolutions);
1019             }
1020         }
1021     } else { /* PD_PRINTSETUP */
1022       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1023
1024       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1025                                   PrintStructures->lpPrinterInfo->pPrinterName,
1026                                   PrintStructures->lpPrinterInfo->pPortName,
1027                                   lpdm);
1028       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1029                                   PrintStructures->lpPrinterInfo->pPrinterName,
1030                                   PrintStructures->lpPrinterInfo->pPortName,
1031                                   lpdm);
1032       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1033       SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1034                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1035                                    PrintStructures->hLandscapeIcon));
1036
1037     }
1038
1039     /* help button */
1040     if ((lppd->Flags & PD_SHOWHELP)==0) {
1041         /* hide if PD_SHOWHELP not specified */
1042         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1043     }
1044     return TRUE;
1045 }
1046
1047 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1048                                    PRINT_PTRW *PrintStructures)
1049 {
1050     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1051     LPDEVMODEW lpdm = NULL;
1052     LONG dmSize;
1053     DWORD needed;
1054     HANDLE hprn;
1055
1056     HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1057     HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1058     if(!OpenPrinterW(name, &hprn, NULL)) {
1059         ERR("Can't open printer %s\n", debugstr_w(name));
1060         return FALSE;
1061     }
1062     GetPrinterW(hprn, 2, NULL, 0, &needed);
1063     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
1064     GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1065                 &needed);
1066     GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1067     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
1068     if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1069             needed, &needed)) {
1070         ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1071         return FALSE;
1072     }
1073     ClosePrinter(hprn);
1074
1075     PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1076
1077     HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1078     PrintStructures->lpDevMode = NULL;
1079
1080     dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1081     if(dmSize == -1) {
1082         ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1083         return FALSE;
1084     }
1085     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1086     dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1087                                  DM_OUT_BUFFER);
1088     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1089                           !lstrcmpW(lpdm->dmDeviceName,
1090                                   PrintStructures->lpDevMode->dmDeviceName)) {
1091       /* Supplied devicemode matches current printer so try to use it */
1092         DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1093                             DM_OUT_BUFFER | DM_IN_BUFFER);
1094     }
1095     if(lpdm)
1096         GlobalUnlock(lppd->hDevMode);
1097
1098     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
1099
1100     if(!(lppd->Flags & PD_PRINTSETUP)) {
1101         /* Print range (All/Range/Selection) */
1102         if(lppd->nFromPage != 0xffff)
1103             SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1104         if(lppd->nToPage != 0xffff)
1105             SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1106
1107         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
1108         if (lppd->Flags & PD_NOSELECTION)
1109             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1110         else
1111             if (lppd->Flags & PD_SELECTION)
1112                 CheckRadioButton(hDlg, rad1, rad3, rad2);
1113         if (lppd->Flags & PD_NOPAGENUMS) {
1114             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1115             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1116             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1117             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1118             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1119         } else {
1120             if (lppd->Flags & PD_PAGENUMS)
1121                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1122         }
1123
1124         /* Collate pages
1125          *
1126          * FIXME: The ico3 is not displayed for some reason. I don't know why.
1127          */
1128         if (lppd->Flags & PD_COLLATE) {
1129             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1130                                 (LPARAM)PrintStructures->hCollateIcon);
1131             CheckDlgButton(hDlg, chx2, 1);
1132         } else {
1133             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1134                                 (LPARAM)PrintStructures->hNoCollateIcon);
1135             CheckDlgButton(hDlg, chx2, 0);
1136         }
1137
1138         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1139           /* if printer doesn't support it: no Collate */
1140             if (!(lpdm->dmFields & DM_COLLATE)) {
1141                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1142                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1143             }
1144         }
1145
1146         /* nCopies */
1147         {
1148           INT copies;
1149           if (lppd->hDevMode == 0)
1150               copies = lppd->nCopies;
1151           else
1152               copies = lpdm->u1.s1.dmCopies;
1153           if(copies == 0) copies = 1;
1154           else if(copies < 0) copies = MAX_COPIES;
1155           SetDlgItemInt(hDlg, edt3, copies, FALSE);
1156         }
1157
1158         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1159           /* if printer doesn't support it: no nCopies */
1160             if (!(lpdm->dmFields & DM_COPIES)) {
1161                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1162                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1163             }
1164         }
1165
1166         /* print to file */
1167         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1168         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1169             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1170         if (lppd->Flags & PD_HIDEPRINTTOFILE)
1171             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1172
1173     } else { /* PD_PRINTSETUP */
1174       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1175
1176       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1177                                   PrintStructures->lpPrinterInfo->pPrinterName,
1178                                   PrintStructures->lpPrinterInfo->pPortName,
1179                                   lpdm);
1180       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1181                                   PrintStructures->lpPrinterInfo->pPrinterName,
1182                                   PrintStructures->lpPrinterInfo->pPortName,
1183                                   lpdm);
1184       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1185       SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1186                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1187                                    PrintStructures->hLandscapeIcon));
1188
1189     }
1190
1191     /* help button */
1192     if ((lppd->Flags & PD_SHOWHELP)==0) {
1193         /* hide if PD_SHOWHELP not specified */
1194         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1195     }
1196     return TRUE;
1197 }
1198
1199  /***********************************************************************
1200  *           check_printer_setup                        [internal]
1201  */
1202 static LRESULT check_printer_setup(HWND hDlg)
1203 {
1204     DWORD needed,num;
1205     WCHAR resourcestr[256],resultstr[256];
1206
1207     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1208     if(needed == 0)
1209     {
1210           EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1211     }
1212     if(needed > 0)
1213           return TRUE;
1214     else
1215     {
1216           LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1217           LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1218           MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1219           return FALSE;
1220     }
1221 }
1222
1223 /***********************************************************************
1224  *           PRINTDLG_WMInitDialog                      [internal]
1225  */
1226 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
1227                                      PRINT_PTRA* PrintStructures)
1228 {
1229     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1230     DEVNAMES *pdn;
1231     DEVMODEA *pdm;
1232     char *name = NULL;
1233     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1234
1235     /* load Collate ICONs */
1236     /* We load these with LoadImage because they are not a standard
1237        size and we don't want them rescaled */
1238     PrintStructures->hCollateIcon =
1239       LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1240     PrintStructures->hNoCollateIcon =
1241       LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1242
1243     /* These can be done with LoadIcon */
1244     PrintStructures->hPortraitIcon =
1245       LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1246     PrintStructures->hLandscapeIcon =
1247       LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1248
1249     /* display the collate/no_collate icon */
1250     SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1251                         (LPARAM)PrintStructures->hNoCollateIcon);
1252
1253     if(PrintStructures->hCollateIcon == 0 ||
1254        PrintStructures->hNoCollateIcon == 0 ||
1255        PrintStructures->hPortraitIcon == 0 ||
1256        PrintStructures->hLandscapeIcon == 0) {
1257         ERR("no icon in resourcefile\n");
1258         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1259         EndDialog(hDlg, FALSE);
1260     }
1261
1262     /*
1263      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1264      * must be registered and the Help button must be shown.
1265      */
1266     if (lppd->Flags & PD_SHOWHELP) {
1267         if((PrintStructures->HelpMessageID =
1268             RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1269             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1270             return FALSE;
1271         }
1272     } else
1273         PrintStructures->HelpMessageID = 0;
1274
1275     if(!(lppd->Flags &PD_PRINTSETUP)) {
1276         PrintStructures->hwndUpDown =
1277           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1278                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1279                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1280                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1281                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1282     }
1283
1284     /* FIXME: I allow more freedom than either Win95 or WinNT,
1285      *        which do not agree to what errors should be thrown or not
1286      *        in case nToPage or nFromPage is out-of-range.
1287      */
1288     if (lppd->nMaxPage < lppd->nMinPage)
1289         lppd->nMaxPage = lppd->nMinPage;
1290     if (lppd->nMinPage == lppd->nMaxPage)
1291         lppd->Flags |= PD_NOPAGENUMS;
1292     if (lppd->nToPage < lppd->nMinPage)
1293         lppd->nToPage = lppd->nMinPage;
1294     if (lppd->nToPage > lppd->nMaxPage)
1295         lppd->nToPage = lppd->nMaxPage;
1296     if (lppd->nFromPage < lppd->nMinPage)
1297         lppd->nFromPage = lppd->nMinPage;
1298     if (lppd->nFromPage > lppd->nMaxPage)
1299         lppd->nFromPage = lppd->nMaxPage;
1300
1301     /* if we have the combo box, fill it */
1302     if (GetDlgItem(hDlg,comboID)) {
1303         /* Fill Combobox
1304          */
1305         pdn = GlobalLock(lppd->hDevNames);
1306         pdm = GlobalLock(lppd->hDevMode);
1307         if(pdn)
1308             name = (char*)pdn + pdn->wDeviceOffset;
1309         else if(pdm)
1310             name = (char*)pdm->dmDeviceName;
1311         PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1312         if(pdm) GlobalUnlock(lppd->hDevMode);
1313         if(pdn) GlobalUnlock(lppd->hDevNames);
1314
1315         /* Now find selected printer and update rest of dlg */
1316         name = HeapAlloc(GetProcessHeap(),0,256);
1317         if (GetDlgItemTextA(hDlg, comboID, name, 255))
1318             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1319         HeapFree(GetProcessHeap(),0,name);
1320     } else {
1321         /* else use default printer */
1322         char name[200];
1323         DWORD dwBufLen = sizeof(name);
1324         BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1325
1326         if (ret)
1327             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1328         else
1329             FIXME("No default printer found, expect problems!\n");
1330     }
1331     return TRUE;
1332 }
1333
1334 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg, WPARAM wParam,
1335                                      PRINT_PTRW* PrintStructures)
1336 {
1337     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1338     DEVNAMES *pdn;
1339     DEVMODEW *pdm;
1340     WCHAR *name = NULL;
1341     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1342
1343     /* load Collate ICONs */
1344     /* We load these with LoadImage because they are not a standard
1345        size and we don't want them rescaled */
1346     PrintStructures->hCollateIcon =
1347       LoadImageW(COMDLG32_hInstance, pd32_collateW, IMAGE_ICON, 0, 0, 0);
1348     PrintStructures->hNoCollateIcon =
1349       LoadImageW(COMDLG32_hInstance, pd32_nocollateW, IMAGE_ICON, 0, 0, 0);
1350
1351     /* These can be done with LoadIcon */
1352     PrintStructures->hPortraitIcon =
1353       LoadIconW(COMDLG32_hInstance, pd32_portraitW);
1354     PrintStructures->hLandscapeIcon =
1355       LoadIconW(COMDLG32_hInstance, pd32_landscapeW);
1356
1357     /* display the collate/no_collate icon */
1358     SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1359                         (LPARAM)PrintStructures->hNoCollateIcon);
1360
1361     if(PrintStructures->hCollateIcon == 0 ||
1362        PrintStructures->hNoCollateIcon == 0 ||
1363        PrintStructures->hPortraitIcon == 0 ||
1364        PrintStructures->hLandscapeIcon == 0) {
1365         ERR("no icon in resourcefile\n");
1366         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1367         EndDialog(hDlg, FALSE);
1368     }
1369
1370     /*
1371      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1372      * must be registered and the Help button must be shown.
1373      */
1374     if (lppd->Flags & PD_SHOWHELP) {
1375         if((PrintStructures->HelpMessageID =
1376             RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1377             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1378             return FALSE;
1379         }
1380     } else
1381         PrintStructures->HelpMessageID = 0;
1382
1383     if(!(lppd->Flags &PD_PRINTSETUP)) {
1384         PrintStructures->hwndUpDown =
1385           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1386                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1387                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1388                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1389                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1390     }
1391
1392     /* FIXME: I allow more freedom than either Win95 or WinNT,
1393      *        which do not agree to what errors should be thrown or not
1394      *        in case nToPage or nFromPage is out-of-range.
1395      */
1396     if (lppd->nMaxPage < lppd->nMinPage)
1397         lppd->nMaxPage = lppd->nMinPage;
1398     if (lppd->nMinPage == lppd->nMaxPage)
1399         lppd->Flags |= PD_NOPAGENUMS;
1400     if (lppd->nToPage < lppd->nMinPage)
1401         lppd->nToPage = lppd->nMinPage;
1402     if (lppd->nToPage > lppd->nMaxPage)
1403         lppd->nToPage = lppd->nMaxPage;
1404     if (lppd->nFromPage < lppd->nMinPage)
1405         lppd->nFromPage = lppd->nMinPage;
1406     if (lppd->nFromPage > lppd->nMaxPage)
1407         lppd->nFromPage = lppd->nMaxPage;
1408
1409     /* if we have the combo box, fill it */
1410     if (GetDlgItem(hDlg,comboID)) {
1411         /* Fill Combobox
1412          */
1413         pdn = GlobalLock(lppd->hDevNames);
1414         pdm = GlobalLock(lppd->hDevMode);
1415         if(pdn)
1416             name = (WCHAR*)pdn + pdn->wDeviceOffset;
1417         else if(pdm)
1418             name = pdm->dmDeviceName;
1419         PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1420         if(pdm) GlobalUnlock(lppd->hDevMode);
1421         if(pdn) GlobalUnlock(lppd->hDevNames);
1422
1423         /* Now find selected printer and update rest of dlg */
1424         /* ansi is ok here */
1425         name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1426         if (GetDlgItemTextW(hDlg, comboID, name, 255))
1427             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1428         HeapFree(GetProcessHeap(),0,name);
1429     } else {
1430         /* else use default printer */
1431         WCHAR name[200];
1432         DWORD dwBufLen = sizeof(name) / sizeof(WCHAR);
1433         BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1434
1435         if (ret)
1436             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1437         else
1438             FIXME("No default printer found, expect problems!\n");
1439     }
1440     return TRUE;
1441 }
1442
1443 /***********************************************************************
1444  *                              PRINTDLG_WMCommand               [internal]
1445  */
1446 LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1447                         LPARAM lParam, PRINT_PTRA* PrintStructures)
1448 {
1449     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1450     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1451     LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1452
1453     switch (LOWORD(wParam))  {
1454     case IDOK:
1455         TRACE(" OK button was hit\n");
1456         if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1457             FIXME("Update printdlg was not successful!\n");
1458             return(FALSE);
1459         }
1460         EndDialog(hDlg, TRUE);
1461         return(TRUE);
1462
1463     case IDCANCEL:
1464         TRACE(" CANCEL button was hit\n");
1465         EndDialog(hDlg, FALSE);
1466         return(FALSE);
1467
1468      case pshHelp:
1469         TRACE(" HELP button was hit\n");
1470         SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1471                                 (WPARAM) hDlg, (LPARAM) lppd);
1472         break;
1473
1474      case chx2:                         /* collate pages checkbox */
1475         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1476             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1477                                     (LPARAM)PrintStructures->hCollateIcon);
1478         else
1479             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1480                                     (LPARAM)PrintStructures->hNoCollateIcon);
1481         break;
1482      case edt1:                         /* from page nr editbox */
1483      case edt2:                         /* to page nr editbox */
1484         if (HIWORD(wParam)==EN_CHANGE) {
1485             WORD nToPage;
1486             WORD nFromPage;
1487             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1488             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1489             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1490                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1491         }
1492         break;
1493
1494     case edt3:
1495         if(HIWORD(wParam) == EN_CHANGE) {
1496             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1497             if(copies <= 1)
1498                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1499             else
1500                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1501         }
1502         break;
1503
1504 #if 0
1505      case psh1:                       /* Print Setup */
1506         {
1507             PRINTDLG16  pdlg;
1508
1509             if (!PrintStructures->dlg.lpPrintDlg16) {
1510                 FIXME("The 32bit print dialog does not have this button!?\n");
1511                 break;
1512             }
1513
1514             memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1515             pdlg.Flags |= PD_PRINTSETUP;
1516             pdlg.hwndOwner = HWND_16(hDlg);
1517             if (!PrintDlg16(&pdlg))
1518                 break;
1519         }
1520         break;
1521 #endif
1522      case psh2:                       /* Properties button */
1523        {
1524          HANDLE hPrinter;
1525          char   PrinterName[256];
1526
1527          GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1528          if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1529              FIXME(" Call to OpenPrinter did not succeed!\n");
1530              break;
1531          }
1532          DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1533                              PrintStructures->lpDevMode,
1534                              PrintStructures->lpDevMode,
1535                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1536          ClosePrinter(hPrinter);
1537          break;
1538        }
1539
1540     case rad1: /* Paperorientation */
1541         if (lppd->Flags & PD_PRINTSETUP)
1542         {
1543               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1544               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1545                           (LPARAM)(PrintStructures->hPortraitIcon));
1546         }
1547         break;
1548
1549     case rad2: /* Paperorientation */
1550         if (lppd->Flags & PD_PRINTSETUP)
1551         {
1552               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1553               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1554                           (LPARAM)(PrintStructures->hLandscapeIcon));
1555         }
1556         break;
1557
1558     case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1559          if (PrinterComboID != LOWORD(wParam)) {
1560              break;
1561          }
1562          /* FALLTHROUGH */
1563     case cmb4:                         /* Printer combobox */
1564          if (HIWORD(wParam)==CBN_SELCHANGE) {
1565              char   PrinterName[256];
1566              GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1567              PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1568          }
1569          break;
1570
1571     case cmb2: /* Papersize */
1572       {
1573           DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1574           if(Sel != CB_ERR)
1575               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1576                                                             CB_GETITEMDATA,
1577                                                             Sel, 0);
1578       }
1579       break;
1580
1581     case cmb3: /* Bin */
1582       {
1583           DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1584           if(Sel != CB_ERR)
1585               lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1586                                                           CB_GETITEMDATA, Sel,
1587                                                           0);
1588       }
1589       break;
1590     }
1591     if(lppd->Flags & PD_PRINTSETUP) {
1592         switch (LOWORD(wParam)) {
1593         case rad1:                         /* orientation */
1594         case rad2:
1595             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1596                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1597                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1598                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1599                                         (WPARAM)IMAGE_ICON,
1600                                         (LPARAM)PrintStructures->hPortraitIcon);
1601                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1602                                         (WPARAM)IMAGE_ICON,
1603                                         (LPARAM)PrintStructures->hPortraitIcon);
1604                 }
1605             } else {
1606                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1607                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1608                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1609                                         (WPARAM)IMAGE_ICON,
1610                                         (LPARAM)PrintStructures->hLandscapeIcon);
1611                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1612                                         (WPARAM)IMAGE_ICON,
1613                                         (LPARAM)PrintStructures->hLandscapeIcon);
1614                 }
1615             }
1616             break;
1617         }
1618     }
1619     return FALSE;
1620 }
1621
1622 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1623                         LPARAM lParam, PRINT_PTRW* PrintStructures)
1624 {
1625     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1626     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1627     LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1628
1629     switch (LOWORD(wParam))  {
1630     case IDOK:
1631         TRACE(" OK button was hit\n");
1632         if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1633             FIXME("Update printdlg was not successful!\n");
1634             return(FALSE);
1635         }
1636         EndDialog(hDlg, TRUE);
1637         return(TRUE);
1638
1639     case IDCANCEL:
1640         TRACE(" CANCEL button was hit\n");
1641         EndDialog(hDlg, FALSE);
1642         return(FALSE);
1643
1644      case pshHelp:
1645         TRACE(" HELP button was hit\n");
1646         SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1647                                 (WPARAM) hDlg, (LPARAM) lppd);
1648         break;
1649
1650      case chx2:                         /* collate pages checkbox */
1651         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1652             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1653                                     (LPARAM)PrintStructures->hCollateIcon);
1654         else
1655             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1656                                     (LPARAM)PrintStructures->hNoCollateIcon);
1657         break;
1658      case edt1:                         /* from page nr editbox */
1659      case edt2:                         /* to page nr editbox */
1660         if (HIWORD(wParam)==EN_CHANGE) {
1661             WORD nToPage;
1662             WORD nFromPage;
1663             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1664             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1665             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1666                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1667         }
1668         break;
1669
1670     case edt3:
1671         if(HIWORD(wParam) == EN_CHANGE) {
1672             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1673             if(copies <= 1)
1674                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1675             else
1676                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1677         }
1678         break;
1679
1680      case psh1:                       /* Print Setup */
1681         {
1682                 ERR("psh1 is called from 16bit code only, we should not get here.\n");
1683         }
1684         break;
1685      case psh2:                       /* Properties button */
1686        {
1687          HANDLE hPrinter;
1688          WCHAR  PrinterName[256];
1689
1690          if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1691          if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1692              FIXME(" Call to OpenPrinter did not succeed!\n");
1693              break;
1694          }
1695          DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1696                              PrintStructures->lpDevMode,
1697                              PrintStructures->lpDevMode,
1698                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1699          ClosePrinter(hPrinter);
1700          break;
1701        }
1702
1703     case rad1: /* Paperorientation */
1704         if (lppd->Flags & PD_PRINTSETUP)
1705         {
1706               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1707               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1708                           (LPARAM)(PrintStructures->hPortraitIcon));
1709         }
1710         break;
1711
1712     case rad2: /* Paperorientation */
1713         if (lppd->Flags & PD_PRINTSETUP)
1714         {
1715               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1716               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1717                           (LPARAM)(PrintStructures->hLandscapeIcon));
1718         }
1719         break;
1720
1721     case cmb1: /* Printer Combobox in PRINT SETUP */
1722          /* FALLTHROUGH */
1723     case cmb4:                         /* Printer combobox */
1724          if (HIWORD(wParam)==CBN_SELCHANGE) {
1725              WCHAR   PrinterName[256];
1726              GetDlgItemTextW(hDlg, LOWORD(wParam), PrinterName, 255);
1727              PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1728          }
1729          break;
1730
1731     case cmb2: /* Papersize */
1732       {
1733           DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1734           if(Sel != CB_ERR)
1735               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1736                                                             CB_GETITEMDATA,
1737                                                             Sel, 0);
1738       }
1739       break;
1740
1741     case cmb3: /* Bin */
1742       {
1743           DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1744           if(Sel != CB_ERR)
1745               lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1746                                                           CB_GETITEMDATA, Sel,
1747                                                           0);
1748       }
1749       break;
1750     }
1751     if(lppd->Flags & PD_PRINTSETUP) {
1752         switch (LOWORD(wParam)) {
1753         case rad1:                         /* orientation */
1754         case rad2:
1755             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1756                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1757                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1758                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1759                                         (WPARAM)IMAGE_ICON,
1760                                         (LPARAM)PrintStructures->hPortraitIcon);
1761                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1762                                         (WPARAM)IMAGE_ICON,
1763                                         (LPARAM)PrintStructures->hPortraitIcon);
1764                 }
1765             } else {
1766                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1767                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1768                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1769                                         (WPARAM)IMAGE_ICON,
1770                                         (LPARAM)PrintStructures->hLandscapeIcon);
1771                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1772                                         (WPARAM)IMAGE_ICON,
1773                                         (LPARAM)PrintStructures->hLandscapeIcon);
1774                 }
1775             }
1776             break;
1777         }
1778     }
1779     return FALSE;
1780 }
1781
1782 /***********************************************************************
1783  *           PrintDlgProcA                      [internal]
1784  */
1785 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1786                                       LPARAM lParam)
1787 {
1788     PRINT_PTRA* PrintStructures;
1789     INT_PTR res = FALSE;
1790
1791     if (uMsg!=WM_INITDIALOG) {
1792         PrintStructures = GetPropW(hDlg, printdlg_prop);
1793         if (!PrintStructures)
1794             return FALSE;
1795     } else {
1796         PrintStructures = (PRINT_PTRA*) lParam;
1797         SetPropW(hDlg, printdlg_prop, PrintStructures);
1798         if(!check_printer_setup(hDlg))
1799         {
1800             EndDialog(hDlg,FALSE);
1801             return FALSE;
1802         }
1803         res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1804
1805         if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1806             res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1807                 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1808             );
1809         return res;
1810     }
1811
1812     if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1813         res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1814                                                          lParam);
1815         if(res) return res;
1816     }
1817
1818     switch (uMsg) {
1819     case WM_COMMAND:
1820         return PRINTDLG_WMCommandA(hDlg, wParam, lParam, PrintStructures);
1821
1822     case WM_DESTROY:
1823         DestroyIcon(PrintStructures->hCollateIcon);
1824         DestroyIcon(PrintStructures->hNoCollateIcon);
1825         DestroyIcon(PrintStructures->hPortraitIcon);
1826         DestroyIcon(PrintStructures->hLandscapeIcon);
1827         if(PrintStructures->hwndUpDown)
1828             DestroyWindow(PrintStructures->hwndUpDown);
1829         return FALSE;
1830     }
1831     return res;
1832 }
1833
1834 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
1835                                       LPARAM lParam)
1836 {
1837     PRINT_PTRW* PrintStructures;
1838     INT_PTR res = FALSE;
1839
1840     if (uMsg!=WM_INITDIALOG) {
1841         PrintStructures = GetPropW(hDlg, printdlg_prop);
1842         if (!PrintStructures)
1843             return FALSE;
1844     } else {
1845         PrintStructures = (PRINT_PTRW*) lParam;
1846         SetPropW(hDlg, printdlg_prop, PrintStructures);
1847         if(!check_printer_setup(hDlg))
1848         {
1849             EndDialog(hDlg,FALSE);
1850             return FALSE;
1851         }
1852         res = PRINTDLG_WMInitDialogW(hDlg, wParam, PrintStructures);
1853
1854         if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1855             res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
1856         return res;
1857     }
1858
1859     if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1860         res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
1861         if(res) return res;
1862     }
1863
1864     switch (uMsg) {
1865     case WM_COMMAND:
1866         return PRINTDLG_WMCommandW(hDlg, wParam, lParam, PrintStructures);
1867
1868     case WM_DESTROY:
1869         DestroyIcon(PrintStructures->hCollateIcon);
1870         DestroyIcon(PrintStructures->hNoCollateIcon);
1871         DestroyIcon(PrintStructures->hPortraitIcon);
1872         DestroyIcon(PrintStructures->hLandscapeIcon);
1873         if(PrintStructures->hwndUpDown)
1874             DestroyWindow(PrintStructures->hwndUpDown);
1875         return FALSE;
1876     }
1877     return res;
1878 }
1879
1880 /************************************************************
1881  *
1882  *      PRINTDLG_GetDlgTemplate
1883  *
1884  */
1885 static HGLOBAL PRINTDLG_GetDlgTemplateA(const PRINTDLGA *lppd)
1886 {
1887     HRSRC hResInfo;
1888     HGLOBAL hDlgTmpl;
1889
1890     if (lppd->Flags & PD_PRINTSETUP) {
1891         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1892             hDlgTmpl = lppd->hSetupTemplate;
1893         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1894             hResInfo = FindResourceA(lppd->hInstance,
1895                                      lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
1896             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1897         } else {
1898             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1899                                      (LPSTR)RT_DIALOG);
1900             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1901         }
1902     } else {
1903         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1904             hDlgTmpl = lppd->hPrintTemplate;
1905         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1906             hResInfo = FindResourceA(lppd->hInstance,
1907                                      lppd->lpPrintTemplateName,
1908                                      (LPSTR)RT_DIALOG);
1909             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1910         } else {
1911             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1912                                      (LPSTR)RT_DIALOG);
1913             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1914         }
1915     }
1916     return hDlgTmpl;
1917 }
1918
1919 static HGLOBAL PRINTDLG_GetDlgTemplateW(const PRINTDLGW *lppd)
1920 {
1921     HRSRC hResInfo;
1922     HGLOBAL hDlgTmpl;
1923     static const WCHAR xpsetup[] = { 'P','R','I','N','T','3','2','_','S','E','T','U','P',0};
1924     static const WCHAR xprint[] = { 'P','R','I','N','T','3','2',0};
1925
1926     if (lppd->Flags & PD_PRINTSETUP) {
1927         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1928             hDlgTmpl = lppd->hSetupTemplate;
1929         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1930             hResInfo = FindResourceW(lppd->hInstance,
1931                                      lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
1932             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1933         } else {
1934             hResInfo = FindResourceW(COMDLG32_hInstance, xpsetup, (LPWSTR)RT_DIALOG);
1935             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1936         }
1937     } else {
1938         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1939             hDlgTmpl = lppd->hPrintTemplate;
1940         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1941             hResInfo = FindResourceW(lppd->hInstance,
1942                                      lppd->lpPrintTemplateName,
1943                                      (LPWSTR)RT_DIALOG);
1944             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1945         } else {
1946             hResInfo = FindResourceW(COMDLG32_hInstance, xprint, (LPWSTR)RT_DIALOG);
1947             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1948         }
1949     }
1950     return hDlgTmpl;
1951 }
1952
1953 /***********************************************************************
1954  *
1955  *      PRINTDLG_CreateDC
1956  *
1957  */
1958 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
1959 {
1960     DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1961     DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1962
1963     if(lppd->Flags & PD_RETURNDC) {
1964         lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1965                               (char*)pdn + pdn->wDeviceOffset,
1966                               (char*)pdn + pdn->wOutputOffset,
1967                               pdm );
1968     } else if(lppd->Flags & PD_RETURNIC) {
1969         lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1970                               (char*)pdn + pdn->wDeviceOffset,
1971                               (char*)pdn + pdn->wOutputOffset,
1972                               pdm );
1973     }
1974     GlobalUnlock(lppd->hDevNames);
1975     GlobalUnlock(lppd->hDevMode);
1976     return lppd->hDC ? TRUE : FALSE;
1977 }
1978
1979 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
1980 {
1981     DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1982     DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
1983
1984     if(lppd->Flags & PD_RETURNDC) {
1985         lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
1986                               (WCHAR*)pdn + pdn->wDeviceOffset,
1987                               (WCHAR*)pdn + pdn->wOutputOffset,
1988                               pdm );
1989     } else if(lppd->Flags & PD_RETURNIC) {
1990         lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
1991                               (WCHAR*)pdn + pdn->wDeviceOffset,
1992                               (WCHAR*)pdn + pdn->wOutputOffset,
1993                               pdm );
1994     }
1995     GlobalUnlock(lppd->hDevNames);
1996     GlobalUnlock(lppd->hDevMode);
1997     return lppd->hDC ? TRUE : FALSE;
1998 }
1999
2000 /***********************************************************************
2001  *           PrintDlgA   (COMDLG32.@)
2002  *
2003  *  Displays the PRINT dialog box, which enables the user to specify
2004  *  specific properties of the print job.
2005  *  
2006  * PARAMS
2007  *  lppd  [IO] ptr to PRINTDLG32 struct
2008  * 
2009  * RETURNS
2010  *  nonzero if the user pressed the OK button
2011  *  zero    if the user cancelled the window or an error occurred
2012  *  
2013  * BUGS
2014  *  PrintDlg:
2015  *  * The Collate Icons do not display, even though they are in the code.
2016  *  * The Properties Button(s) should call DocumentPropertiesA().
2017  */
2018
2019 BOOL WINAPI PrintDlgA(LPPRINTDLGA lppd)
2020 {
2021     BOOL      bRet = FALSE;
2022     LPVOID    ptr;
2023     HINSTANCE hInst;
2024
2025     if (!lppd)
2026     {
2027         COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2028         return FALSE;
2029     }
2030
2031     hInst = (HINSTANCE)GetWindowLongPtrA( lppd->hwndOwner, GWLP_HINSTANCE );
2032     if(TRACE_ON(commdlg)) {
2033         char flagstr[1000] = "";
2034         const struct pd_flags *pflag = pd_flags;
2035         for( ; pflag->name; pflag++) {
2036             if(lppd->Flags & pflag->flag)
2037                 strcat(flagstr, pflag->name);
2038         }
2039         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2040               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2041               "flags %08x (%s)\n",
2042               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2043               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2044               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2045     }
2046
2047     if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2048         WARN("structure size failure !!!\n");
2049         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2050         return FALSE;
2051     }
2052
2053     if(lppd->Flags & PD_RETURNDEFAULT) {
2054         PRINTER_INFO_2A *pbuf;
2055         DRIVER_INFO_3A  *dbuf;
2056         HANDLE hprn;
2057         DWORD needed;
2058
2059         if(lppd->hDevMode || lppd->hDevNames) {
2060             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2061             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2062             return FALSE;
2063         }
2064         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2065             WARN("Can't find default printer\n");
2066             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2067             return FALSE;
2068         }
2069
2070         GetPrinterA(hprn, 2, NULL, 0, &needed);
2071         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2072         GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2073
2074         GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2075         dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2076         if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2077             ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2078                 GetLastError(),pbuf->pPrinterName);
2079             HeapFree(GetProcessHeap(), 0, dbuf);
2080             HeapFree(GetProcessHeap(), 0, pbuf);
2081             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2082             return FALSE;
2083         }
2084         ClosePrinter(hprn);
2085
2086         PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2087                                   dbuf->pDriverPath,
2088                                   pbuf->pPrinterName,
2089                                   pbuf->pPortName);
2090         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2091                                      pbuf->pDevMode->dmDriverExtra);
2092         ptr = GlobalLock(lppd->hDevMode);
2093         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2094                pbuf->pDevMode->dmDriverExtra);
2095         GlobalUnlock(lppd->hDevMode);
2096         HeapFree(GetProcessHeap(), 0, pbuf);
2097         HeapFree(GetProcessHeap(), 0, dbuf);
2098         bRet = TRUE;
2099     } else {
2100         HGLOBAL hDlgTmpl;
2101         PRINT_PTRA *PrintStructures;
2102
2103     /* load Dialog resources,
2104      * depending on Flags indicates Print32 or Print32_setup dialog
2105      */
2106         hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2107         if (!hDlgTmpl) {
2108             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2109             return FALSE;
2110         }
2111         ptr = LockResource( hDlgTmpl );
2112         if (!ptr) {
2113             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2114             return FALSE;
2115         }
2116
2117         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2118                                     sizeof(PRINT_PTRA));
2119         PrintStructures->lpPrintDlg = lppd;
2120
2121         /* and create & process the dialog .
2122          * -1 is failure, 0 is broken hwnd, everything else is ok.
2123          */
2124         bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2125                                            PrintDlgProcA,
2126                                            (LPARAM)PrintStructures));
2127
2128         if(bRet) {
2129             DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2130             PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2131             DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2132
2133             if (lppd->hDevMode == 0) {
2134                 TRACE(" No hDevMode yet... Need to create my own\n");
2135                 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2136                                         lpdm->dmSize + lpdm->dmDriverExtra);
2137             } else {
2138                 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2139                                                lpdm->dmSize + lpdm->dmDriverExtra,
2140                                                GMEM_MOVEABLE);
2141             }
2142             lpdmReturn = GlobalLock(lppd->hDevMode);
2143             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2144
2145             PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2146                     di->pDriverPath,
2147                     pi->pPrinterName,
2148                     pi->pPortName
2149             );
2150             GlobalUnlock(lppd->hDevMode);
2151         }
2152         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2153         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2154         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2155         HeapFree(GetProcessHeap(), 0, PrintStructures);
2156     }
2157     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2158         bRet = PRINTDLG_CreateDCA(lppd);
2159
2160     TRACE("exit! (%d)\n", bRet);
2161     return bRet;
2162 }
2163
2164 /***********************************************************************
2165  *           PrintDlgW   (COMDLG32.@)
2166  *
2167  * See PrintDlgA.
2168  */
2169 BOOL WINAPI PrintDlgW(LPPRINTDLGW lppd)
2170 {
2171     BOOL      bRet = FALSE;
2172     LPVOID    ptr;
2173     HINSTANCE hInst;
2174
2175     if (!lppd)
2176     {
2177         COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2178         return FALSE;
2179     }
2180
2181     hInst = (HINSTANCE)GetWindowLongPtrW( lppd->hwndOwner, GWLP_HINSTANCE );
2182     if(TRACE_ON(commdlg)) {
2183         char flagstr[1000] = "";
2184         const struct pd_flags *pflag = pd_flags;
2185         for( ; pflag->name; pflag++) {
2186             if(lppd->Flags & pflag->flag)
2187                 strcat(flagstr, pflag->name);
2188         }
2189         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2190               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2191               "flags %08x (%s)\n",
2192               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2193               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2194               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2195     }
2196
2197     if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2198         WARN("structure size failure !!!\n");
2199         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2200         return FALSE;
2201     }
2202
2203     if(lppd->Flags & PD_RETURNDEFAULT) {
2204         PRINTER_INFO_2W *pbuf;
2205         DRIVER_INFO_3W  *dbuf;
2206         HANDLE hprn;
2207         DWORD needed;
2208
2209         if(lppd->hDevMode || lppd->hDevNames) {
2210             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2211             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2212             return FALSE;
2213         }
2214         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2215             WARN("Can't find default printer\n");
2216             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2217             return FALSE;
2218         }
2219
2220         GetPrinterW(hprn, 2, NULL, 0, &needed);
2221         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2222         GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2223
2224         GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2225         dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2226         if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2227             ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2228                 GetLastError(),debugstr_w(pbuf->pPrinterName));
2229             HeapFree(GetProcessHeap(), 0, dbuf);
2230             HeapFree(GetProcessHeap(), 0, pbuf);
2231             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2232             return FALSE;
2233         }
2234         ClosePrinter(hprn);
2235
2236         PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2237                                   dbuf->pDriverPath,
2238                                   pbuf->pPrinterName,
2239                                   pbuf->pPortName);
2240         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2241                                      pbuf->pDevMode->dmDriverExtra);
2242         ptr = GlobalLock(lppd->hDevMode);
2243         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2244                pbuf->pDevMode->dmDriverExtra);
2245         GlobalUnlock(lppd->hDevMode);
2246         HeapFree(GetProcessHeap(), 0, pbuf);
2247         HeapFree(GetProcessHeap(), 0, dbuf);
2248         bRet = TRUE;
2249     } else {
2250         HGLOBAL hDlgTmpl;
2251         PRINT_PTRW *PrintStructures;
2252
2253     /* load Dialog resources,
2254      * depending on Flags indicates Print32 or Print32_setup dialog
2255      */
2256         hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2257         if (!hDlgTmpl) {
2258             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2259             return FALSE;
2260         }
2261         ptr = LockResource( hDlgTmpl );
2262         if (!ptr) {
2263             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2264             return FALSE;
2265         }
2266
2267         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2268                                     sizeof(PRINT_PTRW));
2269         PrintStructures->lpPrintDlg = lppd;
2270
2271         /* and create & process the dialog .
2272          * -1 is failure, 0 is broken hwnd, everything else is ok.
2273          */
2274         bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2275                                            PrintDlgProcW,
2276                                            (LPARAM)PrintStructures));
2277
2278         if(bRet) {
2279             DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2280             PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2281             DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2282
2283             if (lppd->hDevMode == 0) {
2284                 TRACE(" No hDevMode yet... Need to create my own\n");
2285                 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2286                                         lpdm->dmSize + lpdm->dmDriverExtra);
2287             } else {
2288                 WORD locks;
2289                 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2290                     WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2291                     while(locks--) {
2292                         GlobalUnlock(lppd->hDevMode);
2293                         TRACE("Now got %d locks\n", locks);
2294                     }
2295                 }
2296                 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2297                                                lpdm->dmSize + lpdm->dmDriverExtra,
2298                                                GMEM_MOVEABLE);
2299             }
2300             lpdmReturn = GlobalLock(lppd->hDevMode);
2301             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2302
2303             if (lppd->hDevNames != 0) {
2304                 WORD locks;
2305                 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2306                     WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2307                     while(locks--)
2308                         GlobalUnlock(lppd->hDevNames);
2309                 }
2310             }
2311             PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2312                     di->pDriverPath,
2313                     pi->pPrinterName,
2314                     pi->pPortName
2315             );
2316             GlobalUnlock(lppd->hDevMode);
2317         }
2318         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2319         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2320         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2321         HeapFree(GetProcessHeap(), 0, PrintStructures);
2322     }
2323     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2324         bRet = PRINTDLG_CreateDCW(lppd);
2325
2326     TRACE("exit! (%d)\n", bRet);
2327     return bRet;
2328 }
2329
2330 /***********************************************************************
2331  *
2332  *          PageSetupDlg
2333  * rad1 - portrait
2334  * rad2 - landscape
2335  * cmb1 - printer select (not in standard dialog template)
2336  * cmb2 - paper size
2337  * cmb3 - source (tray?)
2338  * edt4 - border left
2339  * edt5 - border top
2340  * edt6 - border right
2341  * edt7 - border bottom
2342  * psh3 - "Printer..."
2343  */
2344
2345 typedef struct
2346 {
2347     BOOL unicode;
2348     union
2349     {
2350         LPPAGESETUPDLGA dlga;
2351         LPPAGESETUPDLGW dlgw;
2352     } u;
2353     HWND hDlg;                /* Page Setup dialog handle */
2354     RECT rtDrawRect;          /* Drawing rect for page */
2355 } pagesetup_data;
2356
2357 static inline DWORD pagesetup_get_flags(const pagesetup_data *data)
2358 {
2359     return data->u.dlgw->Flags;
2360 }
2361
2362 static inline BOOL is_metric(const pagesetup_data *data)
2363 {
2364     return pagesetup_get_flags(data) & PSD_INHUNDREDTHSOFMILLIMETERS;
2365 }
2366
2367 static inline LONG tenths_mm_to_size(pagesetup_data *data, LONG size)
2368 {
2369     if (is_metric(data))
2370         return 10 * size;
2371     else
2372         return 10 * size * 100 / 254;
2373 }
2374
2375 static inline LONG thousandths_inch_to_size(pagesetup_data *data, LONG size)
2376 {
2377     if (is_metric(data))
2378         return size * 254 / 100;
2379     else
2380         return size;
2381 }
2382
2383 static WCHAR get_decimal_sep(void)
2384 {
2385     static WCHAR sep;
2386
2387     if(!sep)
2388     {
2389         WCHAR buf[2] = {'.',0};
2390         GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, sizeof(buf) / sizeof(buf[0]));
2391         sep = buf[0];
2392     }
2393     return sep;
2394 }
2395
2396 static void size2str(const pagesetup_data *data, DWORD size, LPWSTR strout)
2397 {
2398     WCHAR integer_fmt[] = {'%','d',0};
2399     WCHAR hundredths_fmt[] = {'%','d','%','c','%','0','2','d',0};
2400     WCHAR thousandths_fmt[] = {'%','d','%','c','%','0','3','d',0};
2401
2402     /* FIXME use LOCALE_SDECIMAL when the edit parsing code can cope */
2403
2404     if (is_metric(data))
2405     {
2406         if(size % 100)
2407             wsprintfW(strout, hundredths_fmt, size / 100, get_decimal_sep(), size % 100);
2408         else
2409             wsprintfW(strout, integer_fmt, size / 100);
2410     }
2411     else
2412     {
2413         if(size % 1000)
2414             wsprintfW(strout, thousandths_fmt, size / 1000, get_decimal_sep(), size % 1000);
2415         else
2416             wsprintfW(strout, integer_fmt, size / 1000);
2417
2418     }
2419 }
2420
2421 static inline BOOL is_default_metric(void)
2422 {
2423     DWORD system;
2424     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
2425                    (LPWSTR)&system, sizeof(system));
2426     return system == 0;
2427 }
2428
2429 /**********************************************
2430  *           rotate_rect
2431  * Cyclically permute the four members of rc
2432  * If sense is TRUE l -> t -> r -> b
2433  * otherwise        l <- t <- r <- b
2434  */
2435 static inline void rotate_rect(RECT *rc, BOOL sense)
2436 {
2437     INT tmp;
2438     if(sense)
2439     {
2440         tmp        = rc->bottom;
2441         rc->bottom = rc->right;
2442         rc->right  = rc->top;
2443         rc->top    = rc->left;
2444         rc->left   = tmp;
2445     }
2446     else
2447     {
2448         tmp        = rc->left;
2449         rc->left   = rc->top;
2450         rc->top    = rc->right;
2451         rc->right  = rc->bottom;
2452         rc->bottom = tmp;
2453     }
2454 }
2455
2456 static void pagesetup_set_orientation(pagesetup_data *data, WORD orient)
2457 {
2458     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2459
2460     assert(orient == DMORIENT_PORTRAIT || orient == DMORIENT_LANDSCAPE);
2461
2462     if(data->unicode)
2463         dm->u1.s1.dmOrientation = orient;
2464     else
2465     {
2466         DEVMODEA *dmA = (DEVMODEA *)dm;
2467         dmA->u1.s1.dmOrientation = orient;
2468     }
2469     GlobalUnlock(data->u.dlgw->hDevMode);
2470 }
2471
2472 static WORD pagesetup_get_orientation(const pagesetup_data *data)
2473 {
2474     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2475     WORD orient;
2476
2477     if(data->unicode)
2478         orient = dm->u1.s1.dmOrientation;
2479     else
2480     {
2481         DEVMODEA *dmA = (DEVMODEA *)dm;
2482         orient = dmA->u1.s1.dmOrientation;
2483     }
2484     GlobalUnlock(data->u.dlgw->hDevMode);
2485     return orient;
2486 }
2487
2488 static void pagesetup_set_papersize(pagesetup_data *data, WORD paper)
2489 {
2490     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2491
2492     if(data->unicode)
2493         dm->u1.s1.dmPaperSize = paper;
2494     else
2495     {
2496         DEVMODEA *dmA = (DEVMODEA *)dm;
2497         dmA->u1.s1.dmPaperSize = paper;
2498     }
2499     GlobalUnlock(data->u.dlgw->hDevMode);
2500 }
2501
2502 static WORD pagesetup_get_papersize(const pagesetup_data *data)
2503 {
2504     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2505     WORD paper;
2506
2507     if(data->unicode)
2508         paper = dm->u1.s1.dmPaperSize;
2509     else
2510     {
2511         DEVMODEA *dmA = (DEVMODEA *)dm;
2512         paper = dmA->u1.s1.dmPaperSize;
2513     }
2514     GlobalUnlock(data->u.dlgw->hDevMode);
2515     return paper;
2516 }
2517
2518 static void pagesetup_set_defaultsource(pagesetup_data *data, WORD source)
2519 {
2520     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2521
2522     if(data->unicode)
2523         dm->u1.s1.dmDefaultSource = source;
2524     else
2525     {
2526         DEVMODEA *dmA = (DEVMODEA *)dm;
2527         dmA->u1.s1.dmDefaultSource = source;
2528     }
2529     GlobalUnlock(data->u.dlgw->hDevMode);
2530 }
2531
2532 typedef enum
2533 {
2534     devnames_driver_name,
2535     devnames_device_name,
2536     devnames_output_name
2537 } devnames_name;
2538
2539
2540 static inline WORD get_devname_offset(DEVNAMES *dn, devnames_name which)
2541 {
2542     switch(which)
2543     {
2544     case devnames_driver_name: return dn->wDriverOffset;
2545     case devnames_device_name: return dn->wDeviceOffset;
2546     case devnames_output_name: return dn->wOutputOffset;
2547     }
2548     ERR("Souldn't be here\n");
2549     return 0;
2550 }
2551
2552 static WCHAR *pagesetup_get_a_devname(const pagesetup_data *data, devnames_name which)
2553 {
2554     DEVNAMES *dn;
2555     WCHAR *name;
2556
2557     dn = GlobalLock(data->u.dlgw->hDevNames);
2558     if(data->unicode)
2559         name = strdupW((WCHAR *)dn + get_devname_offset(dn, which));
2560     else
2561     {
2562         int len = MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, NULL, 0);
2563         name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2564         MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, name, len);
2565     }
2566     GlobalUnlock(data->u.dlgw->hDevNames);
2567     return name;
2568 }
2569
2570 static WCHAR *pagesetup_get_drvname(const pagesetup_data *data)
2571 {
2572     return pagesetup_get_a_devname(data, devnames_driver_name);
2573 }
2574
2575 static WCHAR *pagesetup_get_devname(const pagesetup_data *data)
2576 {
2577     return pagesetup_get_a_devname(data, devnames_device_name);
2578 }
2579
2580 static WCHAR *pagesetup_get_portname(const pagesetup_data *data)
2581 {
2582     return pagesetup_get_a_devname(data, devnames_output_name);
2583 }
2584
2585 static void pagesetup_release_a_devname(const pagesetup_data *data, WCHAR *name)
2586 {
2587     HeapFree(GetProcessHeap(), 0, name);
2588 }
2589
2590 static void pagesetup_set_devnames(pagesetup_data *data, LPCWSTR drv, LPCWSTR devname, LPCWSTR port)
2591 {
2592     DEVNAMES *dn;
2593     WCHAR def[256];
2594     DWORD len = sizeof(DEVNAMES), drv_len, dev_len, port_len;
2595
2596     if(data->unicode)
2597     {
2598         drv_len  = (strlenW(drv) + 1) * sizeof(WCHAR);
2599         dev_len  = (strlenW(devname) + 1) * sizeof(WCHAR);
2600         port_len = (strlenW(port) + 1) * sizeof(WCHAR);
2601     }
2602     else
2603     {
2604         drv_len = WideCharToMultiByte(CP_ACP, 0, drv, -1, NULL, 0, NULL, NULL);
2605         dev_len = WideCharToMultiByte(CP_ACP, 0, devname, -1, NULL, 0, NULL, NULL);
2606         port_len = WideCharToMultiByte(CP_ACP, 0, port, -1, NULL, 0, NULL, NULL);
2607     }
2608     len += drv_len + dev_len + port_len;
2609
2610     if(data->u.dlgw->hDevNames)
2611         data->u.dlgw->hDevNames = GlobalReAlloc(data->u.dlgw->hDevNames, len, GMEM_MOVEABLE);
2612     else
2613         data->u.dlgw->hDevNames = GlobalAlloc(GMEM_MOVEABLE, len);
2614
2615     dn = GlobalLock(data->u.dlgw->hDevNames);
2616
2617     if(data->unicode)
2618     {
2619         WCHAR *ptr = (WCHAR *)(dn + 1);
2620         len = sizeof(DEVNAMES) / sizeof(WCHAR);
2621         dn->wDriverOffset = len;
2622         strcpyW(ptr, drv);
2623         ptr += drv_len / sizeof(WCHAR);
2624         len += drv_len / sizeof(WCHAR);
2625         dn->wDeviceOffset = len;
2626         strcpyW(ptr, devname);
2627         ptr += dev_len / sizeof(WCHAR);
2628         len += dev_len / sizeof(WCHAR);
2629         dn->wOutputOffset = len;
2630         strcpyW(ptr, port);
2631     }
2632     else
2633     {
2634         char *ptr = (char *)(dn + 1);
2635         len = sizeof(DEVNAMES);
2636         dn->wDriverOffset = len;
2637         WideCharToMultiByte(CP_ACP, 0, drv, -1, ptr, drv_len, NULL, NULL);
2638         ptr += drv_len;
2639         len += drv_len;
2640         dn->wDeviceOffset = len;
2641         WideCharToMultiByte(CP_ACP, 0, devname, -1, ptr, dev_len, NULL, NULL);
2642         ptr += dev_len;
2643         len += dev_len;
2644         dn->wOutputOffset = len;
2645         WideCharToMultiByte(CP_ACP, 0, port, -1, ptr, port_len, NULL, NULL);
2646     }
2647
2648     dn->wDefault = 0;
2649     len = sizeof(def) / sizeof(def[0]);
2650     GetDefaultPrinterW(def, &len);
2651     if(!lstrcmpW(def, devname))
2652         dn->wDefault = 1;
2653
2654     GlobalUnlock(data->u.dlgw->hDevNames);
2655 }
2656
2657 static DEVMODEW *pagesetup_get_devmode(const pagesetup_data *data)
2658 {
2659     DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2660     DEVMODEW *ret;
2661
2662     if(data->unicode)
2663     {
2664         /* We make a copy even in the unicode case because the ptr
2665            may get passed back to us in pagesetup_set_devmode. */
2666         ret = HeapAlloc(GetProcessHeap(), 0, dm->dmSize + dm->dmDriverExtra);
2667         memcpy(ret, dm, dm->dmSize + dm->dmDriverExtra);
2668     }
2669     else
2670         ret = GdiConvertToDevmodeW((DEVMODEA *)dm);
2671
2672     GlobalUnlock(data->u.dlgw->hDevMode);
2673     return ret;
2674 }
2675
2676 static void pagesetup_release_devmode(const pagesetup_data *data, DEVMODEW *dm)
2677 {
2678     HeapFree(GetProcessHeap(), 0, dm);
2679 }
2680
2681 static void pagesetup_set_devmode(pagesetup_data *data, DEVMODEW *dm)
2682 {
2683     DEVMODEA *dmA = NULL;
2684     void *src, *dst;
2685     DWORD size;
2686
2687     if(data->unicode)
2688     {
2689         size = dm->dmSize + dm->dmDriverExtra;
2690         src = dm;
2691     }
2692     else
2693     {
2694         dmA = convert_to_devmodeA(dm);
2695         size = dmA->dmSize + dmA->dmDriverExtra;
2696         src = dmA;
2697     }
2698
2699     if(data->u.dlgw->hDevMode)
2700         data->u.dlgw->hDevMode = GlobalReAlloc(data->u.dlgw->hDevMode, size,
2701                                                GMEM_MOVEABLE);
2702     else
2703         data->u.dlgw->hDevMode = GlobalAlloc(GMEM_MOVEABLE, size);
2704
2705     dst = GlobalLock(data->u.dlgw->hDevMode);
2706     memcpy(dst, src, size);
2707     GlobalUnlock(data->u.dlgw->hDevMode);
2708     HeapFree(GetProcessHeap(), 0, dmA);
2709 }
2710
2711 static inline POINT *pagesetup_get_papersize_pt(const pagesetup_data *data)
2712 {
2713     return &data->u.dlgw->ptPaperSize;
2714 }
2715
2716 static inline RECT *pagesetup_get_margin_rect(const pagesetup_data *data)
2717 {
2718     return &data->u.dlgw->rtMargin;
2719 }
2720
2721 typedef enum
2722 {
2723     page_setup_hook,
2724     page_paint_hook
2725 } hook_type;
2726
2727 static inline LPPAGESETUPHOOK pagesetup_get_hook(const pagesetup_data *data, hook_type which)
2728 {
2729     switch(which)
2730     {
2731     case page_setup_hook: return data->u.dlgw->lpfnPageSetupHook;
2732     case page_paint_hook: return data->u.dlgw->lpfnPagePaintHook;
2733     }
2734     return NULL;
2735 }
2736
2737 /* This should only be used in calls to hook procs so we return the ptr
2738    already cast to LPARAM */
2739 static inline LPARAM pagesetup_get_dlg_struct(const pagesetup_data *data)
2740 {
2741     return (LPARAM)data->u.dlgw;
2742 }
2743
2744 static inline void swap_point(POINT *pt)
2745 {
2746     LONG tmp = pt->x;
2747     pt->x = pt->y;
2748     pt->y = tmp;
2749 }
2750
2751 static BOOL pagesetup_update_papersize(pagesetup_data *data)
2752 {
2753     DEVMODEW *dm;
2754     LPWSTR devname, portname;
2755     int i, num;
2756     WORD *words = NULL, paperword;
2757     POINT *points = NULL;
2758     BOOL retval = FALSE;
2759
2760     dm       = pagesetup_get_devmode(data);
2761     devname  = pagesetup_get_devname(data);
2762     portname = pagesetup_get_portname(data);
2763
2764     num = DeviceCapabilitiesW(devname, portname, DC_PAPERS, NULL, dm);
2765     if (num <= 0)
2766     {
2767         FIXME("No papernames found for %s/%s\n", debugstr_w(devname), debugstr_w(portname));
2768         goto end;
2769     }
2770
2771     words = HeapAlloc(GetProcessHeap(), 0, num * sizeof(WORD));
2772     points = HeapAlloc(GetProcessHeap(), 0, num * sizeof(POINT));
2773
2774     if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERS, (LPWSTR)words, dm))
2775     {
2776         FIXME("Number of returned words is not %d\n", num);
2777         goto end;
2778     }
2779
2780     if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERSIZE, (LPWSTR)points, dm))
2781     {
2782         FIXME("Number of returned sizes is not %d\n", num);
2783         goto end;
2784     }
2785
2786     paperword = pagesetup_get_papersize(data);
2787
2788     for (i = 0; i < num; i++)
2789         if (words[i] == paperword)
2790             break;
2791
2792     if (i == num)
2793     {
2794         FIXME("Papersize %d not found in list?\n", paperword);
2795         goto end;
2796     }
2797
2798     /* this is _10ths_ of a millimeter */
2799     pagesetup_get_papersize_pt(data)->x = tenths_mm_to_size(data, points[i].x);
2800     pagesetup_get_papersize_pt(data)->y = tenths_mm_to_size(data, points[i].y);
2801
2802     if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2803         swap_point(pagesetup_get_papersize_pt(data));
2804
2805     retval = TRUE;
2806
2807 end:
2808     HeapFree(GetProcessHeap(), 0, words);
2809     HeapFree(GetProcessHeap(), 0, points);
2810     pagesetup_release_a_devname(data, portname);
2811     pagesetup_release_a_devname(data, devname);
2812     pagesetup_release_devmode(data, dm);
2813
2814     return retval;
2815 }
2816
2817 /**********************************************************************************************
2818  * pagesetup_change_printer
2819  *
2820  * Redefines hDevMode and hDevNames HANDLES and initialises it.
2821  * 
2822  */
2823 static BOOL pagesetup_change_printer(LPWSTR name, pagesetup_data *data)
2824 {
2825     HANDLE hprn;
2826     DWORD needed;
2827     PRINTER_INFO_2W *prn_info = NULL;
2828     DRIVER_INFO_3W *drv_info = NULL;
2829     DEVMODEW *dm = NULL;
2830     BOOL retval = FALSE;
2831
2832     if(!OpenPrinterW(name, &hprn, NULL))
2833     {
2834         ERR("Can't open printer %s\n", debugstr_w(name));
2835         goto end;
2836     }
2837
2838     GetPrinterW(hprn, 2, NULL, 0, &needed);
2839     prn_info = HeapAlloc(GetProcessHeap(), 0, needed);
2840     GetPrinterW(hprn, 2, (LPBYTE)prn_info, needed, &needed);
2841     GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2842     drv_info = HeapAlloc(GetProcessHeap(), 0, needed);
2843     if(!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)drv_info, needed, &needed))
2844     {
2845         ERR("GetPrinterDriverA failed for %s, fix your config!\n", debugstr_w(prn_info->pPrinterName));
2846         goto end;
2847     }
2848     ClosePrinter(hprn);
2849
2850     needed = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
2851     if(needed == -1)
2852     {
2853         ERR("DocumentProperties fails on %s\n", debugstr_w(name));
2854         goto end;
2855     }
2856
2857     dm = HeapAlloc(GetProcessHeap(), 0, needed);
2858     DocumentPropertiesW(0, 0, name, dm, NULL, DM_OUT_BUFFER);
2859
2860     pagesetup_set_devmode(data, dm);
2861     pagesetup_set_devnames(data, drv_info->pDriverPath, prn_info->pPrinterName,
2862                            prn_info->pPortName);
2863
2864     retval = TRUE;
2865 end:
2866     HeapFree(GetProcessHeap(), 0, dm);
2867     HeapFree(GetProcessHeap(), 0, prn_info);
2868     HeapFree(GetProcessHeap(), 0, drv_info);
2869     return retval;
2870 }
2871
2872 /****************************************************************************************
2873  *  pagesetup_init_combos
2874  *
2875  *  Fills Printers, Paper and Source combos
2876  *
2877  */
2878 static void pagesetup_init_combos(HWND hDlg, pagesetup_data *data)
2879 {
2880     DEVMODEW *dm;
2881     LPWSTR devname, portname;
2882
2883     dm       = pagesetup_get_devmode(data);
2884     devname  = pagesetup_get_devname(data);
2885     portname = pagesetup_get_portname(data);
2886
2887     PRINTDLG_SetUpPrinterListComboW(hDlg, cmb1, devname);
2888     PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2, devname, portname, dm);
2889     PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3, devname, portname, dm);
2890
2891     pagesetup_release_a_devname(data, portname);
2892     pagesetup_release_a_devname(data, devname);
2893     pagesetup_release_devmode(data, dm);
2894 }
2895
2896
2897 /****************************************************************************************
2898  *  pagesetup_change_printer_dialog
2899  *
2900  *  Pops up another dialog that lets the user pick another printer.
2901  *
2902  *  For now we display the PrintDlg, this should display a striped down version of it.
2903  */
2904 static void pagesetup_change_printer_dialog(HWND hDlg, pagesetup_data *data)
2905 {
2906     PRINTDLGW prnt;
2907     LPWSTR drvname, devname, portname;
2908     DEVMODEW *tmp_dm, *dm;
2909
2910     memset(&prnt, 0, sizeof(prnt));
2911     prnt.lStructSize = sizeof(prnt);
2912     prnt.Flags     = 0;
2913     prnt.hwndOwner = hDlg;
2914
2915     drvname = pagesetup_get_drvname(data);
2916     devname = pagesetup_get_devname(data);
2917     portname = pagesetup_get_portname(data);
2918     prnt.hDevNames = 0;
2919     PRINTDLG_CreateDevNamesW(&prnt.hDevNames, drvname, devname, portname);
2920     pagesetup_release_a_devname(data, portname);
2921     pagesetup_release_a_devname(data, devname);
2922     pagesetup_release_a_devname(data, drvname);
2923
2924     tmp_dm = pagesetup_get_devmode(data);
2925     prnt.hDevMode = GlobalAlloc(GMEM_MOVEABLE, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2926     dm = GlobalLock(prnt.hDevMode);
2927     memcpy(dm, tmp_dm, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2928     GlobalUnlock(prnt.hDevMode);
2929     pagesetup_release_devmode(data, tmp_dm);
2930
2931     if (PrintDlgW(&prnt))
2932     {
2933         DEVMODEW *dm = GlobalLock(prnt.hDevMode);
2934         DEVNAMES *dn = GlobalLock(prnt.hDevNames);
2935
2936         pagesetup_set_devnames(data, (WCHAR*)dn + dn->wDriverOffset,
2937                                (WCHAR*)dn + dn->wDeviceOffset, (WCHAR *)dn + dn->wOutputOffset);
2938         pagesetup_set_devmode(data, dm);
2939         GlobalUnlock(prnt.hDevNames);
2940         GlobalUnlock(prnt.hDevMode);
2941         pagesetup_init_combos(hDlg, data);
2942     }
2943
2944     GlobalFree(prnt.hDevMode);
2945     GlobalFree(prnt.hDevNames);
2946
2947 }
2948
2949 /******************************************************************************************
2950  * pagesetup_change_preview
2951  *
2952  * Changes paper preview size / position
2953  *
2954  */
2955 static void pagesetup_change_preview(const pagesetup_data *data)
2956 {
2957     LONG width, height, x, y;
2958     RECT tmp;
2959     const int shadow = 4;
2960
2961     if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2962     {
2963         width  = data->rtDrawRect.right - data->rtDrawRect.left;
2964         height = pagesetup_get_papersize_pt(data)->y * width / pagesetup_get_papersize_pt(data)->x;
2965     }
2966     else
2967     {
2968         height = data->rtDrawRect.bottom - data->rtDrawRect.top;
2969         width  = pagesetup_get_papersize_pt(data)->x * height / pagesetup_get_papersize_pt(data)->y;
2970     }
2971     x = (data->rtDrawRect.right + data->rtDrawRect.left - width) / 2;
2972     y = (data->rtDrawRect.bottom + data->rtDrawRect.top - height) / 2;
2973     TRACE("draw rect %s x=%d, y=%d, w=%d, h=%d\n",
2974           wine_dbgstr_rect(&data->rtDrawRect), x, y, width, height);
2975
2976     MoveWindow(GetDlgItem(data->hDlg, rct2), x + width, y + shadow, shadow, height, FALSE);
2977     MoveWindow(GetDlgItem(data->hDlg, rct3), x + shadow, y + height, width, shadow, FALSE);
2978     MoveWindow(GetDlgItem(data->hDlg, rct1), x, y, width, height, FALSE);
2979
2980     tmp = data->rtDrawRect;
2981     tmp.right  += shadow;
2982     tmp.bottom += shadow;
2983     InvalidateRect(data->hDlg, &tmp, TRUE);
2984 }
2985
2986 static inline LONG *element_from_margin_id(RECT *rc, WORD id)
2987 {
2988     switch(id)
2989     {
2990     case edt4: return &rc->left;
2991     case edt5: return &rc->top;
2992     case edt6: return &rc->right;
2993     case edt7: return &rc->bottom;
2994     }
2995     return NULL;
2996 }
2997
2998 static void update_margin_edits(HWND hDlg, const pagesetup_data *data, WORD id)
2999 {
3000     WCHAR str[100];
3001     WORD idx;
3002
3003     for(idx = edt4; idx <= edt7; idx++)
3004     {
3005         if(id == 0 || id == idx)
3006         {
3007             size2str(data, *element_from_margin_id(pagesetup_get_margin_rect(data), idx), str);
3008             SetDlgItemTextW(hDlg, idx, str);
3009         }
3010     }
3011 }
3012
3013 static void margin_edit_notification(HWND hDlg, pagesetup_data *data, WORD msg, WORD id)
3014 {
3015     switch (msg)
3016     {
3017     case EN_CHANGE:
3018       {
3019         WCHAR buf[10];
3020         LONG val = 0;
3021         LONG *value = element_from_margin_id(pagesetup_get_margin_rect(data), id);
3022
3023         if (GetDlgItemTextW(hDlg, id, buf, sizeof(buf) / sizeof(buf[0])) != 0)
3024         {
3025             WCHAR *end;
3026             WCHAR decimal = get_decimal_sep();
3027
3028             val = strtolW(buf, &end, 10);
3029             if(end != buf || *end == decimal)
3030             {
3031                 int mult = is_metric(data) ? 100 : 1000;
3032                 val *= mult;
3033                 if(*end == decimal)
3034                 {
3035                     while(mult > 1)
3036                     {
3037                         end++;
3038                         mult /= 10;
3039                         if(isdigitW(*end))
3040                             val += (*end - '0') * mult;
3041                         else
3042                             break;
3043                     }
3044                 }
3045             }
3046         }
3047         *value = val;
3048         return;
3049       }
3050
3051     case EN_KILLFOCUS:
3052         update_margin_edits(hDlg, data, id);
3053         return;
3054     }
3055 }
3056
3057 static void set_margin_groupbox_title(HWND hDlg, const pagesetup_data *data)
3058 {
3059     WCHAR title[256];
3060
3061     if(LoadStringW(COMDLG32_hInstance, is_metric(data) ? PD32_MARGINS_IN_MILLIMETERS : PD32_MARGINS_IN_INCHES,
3062                    title, sizeof(title)/sizeof(title[0])))
3063         SetDlgItemTextW(hDlg, grp4, title);
3064 }
3065
3066 static void pagesetup_update_orientation_buttons(HWND hDlg, const pagesetup_data *data)
3067 {
3068     if (pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3069         CheckRadioButton(hDlg, rad1, rad2, rad2);
3070     else
3071         CheckRadioButton(hDlg, rad1, rad2, rad1);
3072 }
3073
3074 /****************************************************************************************
3075  *  pagesetup_printer_properties
3076  *
3077  *  Handle invocation of the 'Properties' button (not present in the default template).
3078  */
3079 static void pagesetup_printer_properties(HWND hDlg, pagesetup_data *data)
3080 {
3081     HANDLE hprn;
3082     LPWSTR devname;
3083     DEVMODEW *dm;
3084     LRESULT count;
3085     int i;
3086
3087     devname = pagesetup_get_devname(data);
3088
3089     if (!OpenPrinterW(devname, &hprn, NULL))
3090     {
3091         FIXME("Call to OpenPrinter did not succeed!\n");
3092         pagesetup_release_a_devname(data, devname);
3093         return;
3094     }
3095
3096     dm = pagesetup_get_devmode(data);
3097     DocumentPropertiesW(hDlg, hprn, devname, dm, dm, DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
3098     pagesetup_set_devmode(data, dm);
3099     pagesetup_release_devmode(data, dm);
3100     pagesetup_release_a_devname(data, devname);
3101     ClosePrinter(hprn);
3102
3103     /* Changing paper */
3104     pagesetup_update_papersize(data);
3105     pagesetup_update_orientation_buttons(hDlg, data);
3106
3107     /* Changing paper preview */
3108     pagesetup_change_preview(data);
3109
3110     /* Selecting paper in combo */
3111     count = SendDlgItemMessageW(hDlg, cmb2, CB_GETCOUNT, 0, 0);
3112     if(count != CB_ERR)
3113     {
3114         WORD paperword = pagesetup_get_papersize(data);
3115         for(i = 0; i < count; i++)
3116         {
3117             if(SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, i, 0) == paperword) {
3118                 SendDlgItemMessageW(hDlg, cmb2, CB_SETCURSEL, i, 0);
3119                 break;
3120             }
3121         }
3122     }
3123 }
3124
3125 /********************************************************************************
3126  * pagesetup_wm_command
3127  * process WM_COMMAND message for PageSetupDlg
3128  *
3129  * PARAMS
3130  *  hDlg        [in]    Main dialog HANDLE 
3131  *  wParam      [in]    WM_COMMAND wParam
3132  *  lParam      [in]    WM_COMMAND lParam
3133  *  pda         [in/out] ptr to PageSetupDataA
3134  */
3135
3136 static BOOL pagesetup_wm_command(HWND hDlg, WPARAM wParam, LPARAM lParam, pagesetup_data *data)
3137 {
3138     WORD msg = HIWORD(wParam);
3139     WORD id  = LOWORD(wParam);
3140
3141     TRACE("loword (lparam) %d, wparam 0x%lx, lparam %08lx\n",
3142             LOWORD(lParam),wParam,lParam);
3143     switch (id)  {
3144     case IDOK:
3145         EndDialog(hDlg, TRUE);
3146         return TRUE ;
3147
3148     case IDCANCEL:
3149         EndDialog(hDlg, FALSE);
3150         return FALSE ;
3151
3152     case psh3: /* Printer... */
3153         pagesetup_change_printer_dialog(hDlg, data);
3154         return TRUE;
3155
3156     case rad1: /* Portrait */
3157     case rad2: /* Landscape */
3158         if((id == rad1 && pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE) ||
3159            (id == rad2 && pagesetup_get_orientation(data) == DMORIENT_PORTRAIT))
3160         {
3161             pagesetup_set_orientation(data, (id == rad1) ? DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE);
3162             pagesetup_update_papersize(data);
3163             rotate_rect(pagesetup_get_margin_rect(data), (id == rad2));
3164             update_margin_edits(hDlg, data, 0);
3165             pagesetup_change_preview(data);
3166         }
3167         break;
3168     case cmb1: /* Printer combo */
3169         if(msg == CBN_SELCHANGE)
3170         {
3171             WCHAR name[256];
3172             GetDlgItemTextW(hDlg, id, name, sizeof(name) / sizeof(name[0]));
3173             pagesetup_change_printer(name, data);
3174             pagesetup_init_combos(hDlg, data);
3175         }
3176         break;
3177     case cmb2: /* Paper combo */
3178         if(msg == CBN_SELCHANGE)
3179         {
3180             DWORD paperword = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA,
3181                                                   SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0), 0);
3182             if (paperword != CB_ERR)
3183             {
3184                 pagesetup_set_papersize(data, paperword);
3185                 pagesetup_update_papersize(data);
3186                 pagesetup_change_preview(data);
3187             } else
3188                 FIXME("could not get dialog text for papersize cmbbox?\n");
3189         }
3190         break;
3191     case cmb3: /* Paper Source */
3192         if(msg == CBN_SELCHANGE)
3193         {
3194             WORD source = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA,
3195                                               SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0), 0);
3196             pagesetup_set_defaultsource(data, source);
3197         }
3198         break;
3199     case psh2: /* Printer Properties button */
3200         pagesetup_printer_properties(hDlg, data);
3201         break;
3202     case edt4:
3203     case edt5:
3204     case edt6:
3205     case edt7:
3206         margin_edit_notification(hDlg, data, msg, id);
3207         break;
3208     }
3209     InvalidateRect(GetDlgItem(hDlg, rct1), NULL, TRUE);
3210     return FALSE;
3211 }
3212
3213 /***********************************************************************
3214  *           default_page_paint_hook
3215  * Default hook paint procedure that receives WM_PSD_* messages from the dialog box 
3216  * whenever the sample page is redrawn.
3217  */
3218 static UINT_PTR default_page_paint_hook(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam,
3219                                         const pagesetup_data *data)
3220 {
3221     LPRECT lprc = (LPRECT) lParam;
3222     HDC hdc = (HDC) wParam;
3223     HPEN hpen, holdpen;
3224     LOGFONTW lf;
3225     HFONT hfont, holdfont;
3226     INT oldbkmode;
3227     TRACE("uMsg: WM_USER+%d\n",uMsg-WM_USER);
3228     /* Call user paint hook if enable */
3229     if (pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK)
3230         if (pagesetup_get_hook(data, page_paint_hook)(hwndDlg, uMsg, wParam, lParam))
3231             return TRUE;
3232
3233     switch (uMsg) {
3234        /* LPPAGESETUPDLG in lParam */
3235        case WM_PSD_PAGESETUPDLG:
3236        /* Inform about the sample page rectangle */
3237        case WM_PSD_FULLPAGERECT:
3238        /* Inform about the margin rectangle */
3239        case WM_PSD_MINMARGINRECT:
3240             return FALSE;
3241
3242         /* Draw dashed rectangle showing margins */
3243         case WM_PSD_MARGINRECT:
3244             hpen = CreatePen(PS_DASH, 1, GetSysColor(COLOR_3DSHADOW));
3245             holdpen = SelectObject(hdc, hpen);
3246             Rectangle(hdc, lprc->left, lprc->top, lprc->right, lprc->bottom);
3247             DeleteObject(SelectObject(hdc, holdpen));
3248             return TRUE;
3249         /* Draw the fake document */
3250         case WM_PSD_GREEKTEXTRECT:
3251             /* select a nice scalable font, because we want the text really small */
3252             SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
3253             lf.lfHeight = 6; /* value chosen based on visual effect */
3254             hfont = CreateFontIndirectW(&lf);
3255             holdfont = SelectObject(hdc, hfont);
3256
3257             /* if text not loaded, then do so now */
3258             if (wszFakeDocumentText[0] == '\0')
3259                  LoadStringW(COMDLG32_hInstance,
3260                         IDS_FAKEDOCTEXT,
3261                         wszFakeDocumentText,
3262                         sizeof(wszFakeDocumentText)/sizeof(wszFakeDocumentText[0]));
3263
3264             oldbkmode = SetBkMode(hdc, TRANSPARENT);
3265             DrawTextW(hdc, wszFakeDocumentText, -1, lprc, DT_TOP|DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
3266             SetBkMode(hdc, oldbkmode);
3267
3268             DeleteObject(SelectObject(hdc, holdfont));
3269             return TRUE;
3270
3271         /* Envelope stamp */
3272         case WM_PSD_ENVSTAMPRECT:
3273         /* Return address */
3274         case WM_PSD_YAFULLPAGERECT:
3275             FIXME("envelope/stamp is not implemented\n");
3276             return FALSE;
3277         default:
3278             FIXME("Unknown message %x\n",uMsg);
3279             return FALSE;
3280     }
3281     return TRUE;
3282 }
3283
3284 /***********************************************************************
3285  *           PagePaintProc
3286  * The main paint procedure for the PageSetupDlg function.
3287  * The Page Setup dialog box includes an image of a sample page that shows how
3288  * the user's selections affect the appearance of the printed output.
3289  * The image consists of a rectangle that represents the selected paper
3290  * or envelope type, with a dotted-line rectangle representing
3291  * the current margins, and partial (Greek text) characters
3292  * to show how text looks on the printed page. 
3293  *
3294  * The following messages in the order sends to user hook procedure:
3295  *   WM_PSD_PAGESETUPDLG    Draw the contents of the sample page
3296  *   WM_PSD_FULLPAGERECT    Inform about the bounding rectangle
3297  *   WM_PSD_MINMARGINRECT   Inform about the margin rectangle (min margin?)
3298  *   WM_PSD_MARGINRECT      Draw the margin rectangle
3299  *   WM_PSD_GREEKTEXTRECT   Draw the Greek text inside the margin rectangle
3300  * If any of first three messages returns TRUE, painting done.
3301  *
3302  * PARAMS:
3303  *   hWnd   [in] Handle to the Page Setup dialog box
3304  *   uMsg   [in] Received message
3305  *
3306  * TODO:
3307  *   WM_PSD_ENVSTAMPRECT    Draw in the envelope-stamp rectangle (for envelopes only)
3308  *   WM_PSD_YAFULLPAGERECT  Draw the return address portion (for envelopes and other paper sizes)
3309  *
3310  * RETURNS:
3311  *   FALSE if all done correctly
3312  *
3313  */
3314
3315
3316 static LRESULT CALLBACK
3317 PRINTDLG_PagePaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3318 {
3319     PAINTSTRUCT ps;
3320     RECT rcClient, rcMargin;
3321     HPEN hpen, holdpen;
3322     HDC hdc;
3323     HBRUSH hbrush, holdbrush;
3324     pagesetup_data *data;
3325     int papersize=0, orientation=0; /* FIXME: set this values for user paint hook */
3326     double scalx, scaly;
3327
3328     if (uMsg != WM_PAINT)
3329         return CallWindowProcA(lpfnStaticWndProc, hWnd, uMsg, wParam, lParam);
3330
3331     /* Processing WM_PAINT message */
3332     data = GetPropW(hWnd, pagesetupdlg_prop);
3333     if (!data) {
3334         WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3335         return FALSE;
3336     }
3337     if (default_page_paint_hook(hWnd, WM_PSD_PAGESETUPDLG, MAKELONG(papersize, orientation),
3338                                 pagesetup_get_dlg_struct(data), data))
3339         return FALSE;
3340
3341     hdc = BeginPaint(hWnd, &ps);
3342     GetClientRect(hWnd, &rcClient);
3343     
3344     scalx = rcClient.right  / (double)pagesetup_get_papersize_pt(data)->x;
3345     scaly = rcClient.bottom / (double)pagesetup_get_papersize_pt(data)->y;
3346     rcMargin = rcClient;
3347
3348     rcMargin.left   += pagesetup_get_margin_rect(data)->left   * scalx;
3349     rcMargin.top    += pagesetup_get_margin_rect(data)->top    * scaly;
3350     rcMargin.right  -= pagesetup_get_margin_rect(data)->right  * scalx;
3351     rcMargin.bottom -= pagesetup_get_margin_rect(data)->bottom * scaly;
3352
3353     /* if the space is too small then we make sure to not draw anything */
3354     rcMargin.left = min(rcMargin.left, rcMargin.right);
3355     rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3356
3357     if (!default_page_paint_hook(hWnd, WM_PSD_FULLPAGERECT, (WPARAM)hdc, (LPARAM)&rcClient, data) &&
3358         !default_page_paint_hook(hWnd, WM_PSD_MINMARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data) )
3359     {
3360         /* fill background */
3361         hbrush = GetSysColorBrush(COLOR_3DHIGHLIGHT);
3362         FillRect(hdc, &rcClient, hbrush);
3363         holdbrush = SelectObject(hdc, hbrush);
3364
3365         hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
3366         holdpen = SelectObject(hdc, hpen);
3367         
3368         /* paint left edge */
3369         MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3370         LineTo(hdc, rcClient.left, rcClient.bottom-1);
3371
3372         /* paint top edge */
3373         MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3374         LineTo(hdc, rcClient.right, rcClient.top);
3375
3376         hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
3377         DeleteObject(SelectObject(hdc, hpen));
3378
3379         /* paint right edge */
3380         MoveToEx(hdc, rcClient.right-1, rcClient.top, NULL);
3381         LineTo(hdc, rcClient.right-1, rcClient.bottom);
3382
3383         /* paint bottom edge */
3384         MoveToEx(hdc, rcClient.left, rcClient.bottom-1, NULL);
3385         LineTo(hdc, rcClient.right, rcClient.bottom-1);
3386
3387         DeleteObject(SelectObject(hdc, holdpen));
3388         DeleteObject(SelectObject(hdc, holdbrush));
3389
3390         default_page_paint_hook(hWnd, WM_PSD_MARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3391
3392         /* give text a bit of a space from the frame */
3393         rcMargin.left += 2;
3394         rcMargin.top += 2;
3395         rcMargin.right -= 2;
3396         rcMargin.bottom -= 2;
3397         
3398         /* if the space is too small then we make sure to not draw anything */
3399         rcMargin.left = min(rcMargin.left, rcMargin.right);
3400         rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3401
3402         default_page_paint_hook(hWnd, WM_PSD_GREEKTEXTRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3403     }
3404
3405     EndPaint(hWnd, &ps);
3406     return FALSE;
3407 }
3408
3409 /*******************************************************
3410  * The margin edit controls are subclassed to filter
3411  * anything other than numbers and the decimal separator.
3412  */
3413 static LRESULT CALLBACK pagesetup_margin_editproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3414 {
3415     if (msg == WM_CHAR)
3416     {
3417         WCHAR decimal = get_decimal_sep();
3418         WCHAR wc = (WCHAR)wparam;
3419         if(!isdigitW(wc) && wc != decimal && wc != VK_BACK) return 0;
3420     }
3421     return CallWindowProcW(edit_wndproc, hwnd, msg, wparam, lparam);
3422 }
3423
3424 static void subclass_margin_edits(HWND hDlg)
3425 {
3426     int id;
3427     WNDPROC old_proc;
3428
3429     for(id = edt4; id <= edt7; id++)
3430     {
3431         old_proc = (WNDPROC)SetWindowLongPtrW(GetDlgItem(hDlg, id),
3432                                               GWLP_WNDPROC,
3433                                               (ULONG_PTR)pagesetup_margin_editproc);
3434         InterlockedCompareExchangePointer((void**)&edit_wndproc, old_proc, NULL);
3435     }
3436 }
3437
3438 /***********************************************************************
3439  *           pagesetup_dlg_proc
3440  *
3441  * Message handler for PageSetupDlg
3442  */
3443 static INT_PTR CALLBACK pagesetup_dlg_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
3444 {
3445     pagesetup_data *data;
3446     INT_PTR             res = FALSE;
3447     HWND                hDrawWnd;
3448
3449     if (uMsg == WM_INITDIALOG) { /*Init dialog*/
3450         data = (pagesetup_data *)lParam;
3451         data->hDlg = hDlg;
3452
3453         hDrawWnd = GetDlgItem(hDlg, rct1); 
3454         TRACE("set property to %p\n", data);
3455         SetPropW(hDlg, pagesetupdlg_prop, data);
3456         SetPropW(hDrawWnd, pagesetupdlg_prop, data);
3457         GetWindowRect(hDrawWnd, &data->rtDrawRect); /* Calculating rect in client coordinates where paper draws */
3458         ScreenToClient(hDlg, (LPPOINT)&data->rtDrawRect);
3459         ScreenToClient(hDlg, (LPPOINT)(&data->rtDrawRect.right));
3460         lpfnStaticWndProc = (WNDPROC)SetWindowLongPtrW(
3461             hDrawWnd,
3462             GWLP_WNDPROC,
3463             (ULONG_PTR)PRINTDLG_PagePaintProc);
3464         
3465         /* FIXME: Paint hook. Must it be at begin of initialization or at end? */
3466         res = TRUE;
3467         if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3468         {
3469             if (!pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam,
3470                                                            pagesetup_get_dlg_struct(data)))
3471                 FIXME("Setup page hook failed?\n");
3472         }
3473
3474         /* if printer button disabled */
3475         if (pagesetup_get_flags(data) & PSD_DISABLEPRINTER)
3476             EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
3477         /* if margin edit boxes disabled */
3478         if (pagesetup_get_flags(data) & PSD_DISABLEMARGINS)
3479         {
3480             EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
3481             EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
3482             EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
3483             EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
3484         }
3485
3486         /* Set orientation radiobuttons properly */
3487         pagesetup_update_orientation_buttons(hDlg, data);
3488
3489         /* if orientation disabled */
3490         if (pagesetup_get_flags(data) & PSD_DISABLEORIENTATION)
3491         {
3492             EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
3493             EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
3494         }
3495
3496         /* We fill them out enabled or not */
3497         if (!(pagesetup_get_flags(data) & PSD_MARGINS))
3498         {
3499             /* default is 1 inch */
3500             LONG size = thousandths_inch_to_size(data, 1000);
3501             SetRect(pagesetup_get_margin_rect(data), size, size, size, size);
3502         }
3503         update_margin_edits(hDlg, data, 0);
3504         subclass_margin_edits(hDlg);
3505         set_margin_groupbox_title(hDlg, data);
3506
3507         /* if paper disabled */
3508         if (pagesetup_get_flags(data) & PSD_DISABLEPAPER)
3509         {
3510             EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
3511             EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
3512         }
3513
3514         /* filling combos: printer, paper, source. selecting current printer (from DEVMODEA) */
3515         pagesetup_init_combos(hDlg, data);
3516         pagesetup_update_papersize(data);
3517         pagesetup_set_defaultsource(data, DMBIN_FORMSOURCE); /* FIXME: This is the auto select bin. Is this correct? */
3518
3519         /* Drawing paper prev */
3520         pagesetup_change_preview(data);
3521         return TRUE;
3522     } else {
3523         data = GetPropW(hDlg, pagesetupdlg_prop);
3524         if (!data)
3525         {
3526             WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3527             return FALSE;
3528         }
3529         if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3530         {
3531             res = pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam, lParam);
3532             if (res) return res;
3533         }
3534     }
3535     switch (uMsg) {
3536     case WM_COMMAND:
3537         return pagesetup_wm_command(hDlg, wParam, lParam, data);
3538     }
3539     return FALSE;
3540 }
3541
3542 static WCHAR *get_default_printer(void)
3543 {
3544     WCHAR *name = NULL;
3545     DWORD len = 0;
3546
3547     GetDefaultPrinterW(NULL, &len);
3548     if(len)
3549     {
3550         name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3551         GetDefaultPrinterW(name, &len);
3552     }
3553     return name;
3554 }
3555
3556 static void pagesetup_dump_dlg_struct(pagesetup_data *data)
3557 {
3558     if(TRACE_ON(commdlg))
3559     {
3560         char flagstr[1000] = "";
3561         const struct pd_flags *pflag = psd_flags;
3562         for( ; pflag->name; pflag++)
3563         {
3564             if(pagesetup_get_flags(data) & pflag->flag)
3565             {
3566                 strcat(flagstr, pflag->name);
3567                 strcat(flagstr, "|");
3568             }
3569         }
3570         TRACE("%s: (%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
3571               "hinst %p, flags %08x (%s)\n",
3572               data->unicode ? "unicode" : "ansi",
3573               data->u.dlgw, data->u.dlgw->hwndOwner, data->u.dlgw->hDevMode,
3574               data->u.dlgw->hDevNames, data->u.dlgw->hInstance,
3575               pagesetup_get_flags(data), flagstr);
3576     }
3577 }
3578
3579 static void *pagesetup_get_template(pagesetup_data *data)
3580 {
3581     HRSRC res;
3582     HGLOBAL tmpl_handle;
3583
3584     if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATEHANDLE)
3585     {
3586         tmpl_handle = data->u.dlgw->hPageSetupTemplate;
3587     }
3588     else if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATE)
3589     {
3590         if(data->unicode)
3591             res = FindResourceW(data->u.dlgw->hInstance,
3592                                 data->u.dlgw->lpPageSetupTemplateName, MAKEINTRESOURCEW(RT_DIALOG));
3593         else
3594             res = FindResourceA(data->u.dlga->hInstance,
3595                                 data->u.dlga->lpPageSetupTemplateName, MAKEINTRESOURCEA(RT_DIALOG));
3596         tmpl_handle = LoadResource(data->u.dlgw->hInstance, res);
3597     }
3598     else
3599     {
3600         res = FindResourceW(COMDLG32_hInstance, MAKEINTRESOURCEW(PAGESETUPDLGORD),
3601                             MAKEINTRESOURCEW(RT_DIALOG));
3602         tmpl_handle = LoadResource(COMDLG32_hInstance, res);
3603     }
3604     return LockResource(tmpl_handle);
3605 }
3606
3607 static BOOL pagesetup_common(pagesetup_data *data)
3608 {
3609     BOOL ret;
3610     void *tmpl;
3611
3612     if(!pagesetup_get_dlg_struct(data))
3613     {
3614         COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
3615         return FALSE;
3616     }
3617
3618     pagesetup_dump_dlg_struct(data);
3619
3620     if(data->u.dlgw->lStructSize != sizeof(PAGESETUPDLGW))
3621     {
3622         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
3623         return FALSE;
3624     }
3625
3626     if ((pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK) &&
3627         (pagesetup_get_hook(data, page_paint_hook) == NULL))
3628     {
3629         COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
3630         return FALSE;
3631     }
3632
3633     if(!(pagesetup_get_flags(data) & (PSD_INTHOUSANDTHSOFINCHES | PSD_INHUNDREDTHSOFMILLIMETERS)))
3634         data->u.dlgw->Flags |= is_default_metric() ?
3635             PSD_INHUNDREDTHSOFMILLIMETERS : PSD_INTHOUSANDTHSOFINCHES;
3636
3637     if (!data->u.dlgw->hDevMode || !data->u.dlgw->hDevNames)
3638     {
3639         WCHAR *def = get_default_printer();
3640         if(!def)
3641         {
3642             if (!(pagesetup_get_flags(data) & PSD_NOWARNING))
3643             {
3644                 WCHAR errstr[256];
3645                 LoadStringW(COMDLG32_hInstance, PD32_NO_DEFAULT_PRINTER, errstr, 255);
3646                 MessageBoxW(data->u.dlgw->hwndOwner, errstr, 0, MB_OK | MB_ICONERROR);
3647             }
3648             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3649             return FALSE;
3650         }
3651         pagesetup_change_printer(def, data);
3652         HeapFree(GetProcessHeap(), 0, def);
3653     }
3654
3655     if (pagesetup_get_flags(data) & PSD_RETURNDEFAULT)
3656     {
3657         pagesetup_update_papersize(data);
3658         return TRUE;
3659     }
3660
3661     tmpl = pagesetup_get_template(data);
3662
3663     ret = DialogBoxIndirectParamW(data->u.dlgw->hInstance, tmpl,
3664                                   data->u.dlgw->hwndOwner,
3665                                   pagesetup_dlg_proc, (LPARAM)data) > 0;
3666     return ret;
3667 }
3668
3669 /***********************************************************************
3670  *            PageSetupDlgA  (COMDLG32.@)
3671  *
3672  *  Displays the PAGE SETUP dialog box, which enables the user to specify
3673  *  specific properties of a printed page such as
3674  *  size, source, orientation and the width of the page margins.
3675  *
3676  * PARAMS
3677  *  setupdlg [IO] PAGESETUPDLGA struct
3678  *
3679  * RETURNS
3680  *  TRUE    if the user pressed the OK button
3681  *  FALSE   if the user cancelled the window or an error occurred
3682  *
3683  * NOTES
3684  *    The values of hDevMode and hDevNames are filled on output and can be
3685  *    changed in PAGESETUPDLG when they are passed in PageSetupDlg.
3686  *
3687  */
3688 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg)
3689 {
3690     pagesetup_data data;
3691
3692     data.unicode = FALSE;
3693     data.u.dlga  = setupdlg;
3694
3695     return pagesetup_common(&data);
3696 }
3697
3698 /***********************************************************************
3699  *            PageSetupDlgW  (COMDLG32.@)
3700  *
3701  * See PageSetupDlgA.
3702  */
3703 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg)
3704 {
3705     pagesetup_data data;
3706
3707     data.unicode = TRUE;
3708     data.u.dlgw  = setupdlg;
3709
3710     return pagesetup_common(&data);
3711 }
3712
3713 /***********************************************************************
3714  * PrintDlgExA (COMDLG32.@)
3715  *
3716  * See PrintDlgExW.
3717  *
3718  * BUGS
3719  *   Only a Stub
3720  *
3721  */
3722 HRESULT WINAPI PrintDlgExA(LPPRINTDLGEXA lppd)
3723 {
3724
3725     FIXME("(%p) stub\n", lppd);
3726     if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXA))) {
3727         return E_INVALIDARG;
3728     }
3729
3730     if (!IsWindow(lppd->hwndOwner)) {
3731         return E_HANDLE;
3732     }
3733
3734     return E_NOTIMPL;
3735 }
3736
3737 /***********************************************************************
3738  * PrintDlgExW (COMDLG32.@)
3739  *
3740  * Display the property sheet style PRINT dialog box
3741  *  
3742  * PARAMS
3743  *  lppd  [IO] ptr to PRINTDLGEX struct
3744  * 
3745  * RETURNS
3746  *  Success: S_OK
3747  *  Failure: One of the following COM error codes:
3748  *    E_OUTOFMEMORY Insufficient memory.
3749  *    E_INVALIDARG  One or more arguments are invalid.
3750  *    E_POINTER     Invalid pointer.
3751  *    E_HANDLE      Invalid handle.
3752  *    E_FAIL        Unspecified error.
3753  *  
3754  * NOTES
3755  * This Dialog enables the user to specify specific properties of the print job.
3756  * The property sheet can also have additional application-specific and
3757  * driver-specific property pages.
3758  *
3759  * BUGS
3760  *   Not fully implemented
3761  *
3762  */
3763 HRESULT WINAPI PrintDlgExW(LPPRINTDLGEXW lppd)
3764 {
3765     DWORD     ret = E_FAIL;
3766     LPVOID    ptr;
3767
3768     FIXME("(%p) not fully implemented\n", lppd);
3769
3770     if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXW))) {
3771         return E_INVALIDARG;
3772     }
3773
3774     if (!IsWindow(lppd->hwndOwner)) {
3775         return E_HANDLE;
3776     }
3777
3778     if (lppd->Flags & PD_RETURNDEFAULT) {
3779         PRINTER_INFO_2W *pbuf;
3780         DRIVER_INFO_2W  *dbuf;
3781         HANDLE hprn;
3782         DWORD needed = 1024;
3783         BOOL bRet;
3784
3785         if (lppd->hDevMode || lppd->hDevNames) {
3786             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
3787             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3788             return E_INVALIDARG;
3789         }
3790         if (!PRINTDLG_OpenDefaultPrinter(&hprn)) {
3791             WARN("Can't find default printer\n");
3792             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3793             return E_FAIL;
3794         }
3795
3796         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3797         bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3798         if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3799             HeapFree(GetProcessHeap(), 0, pbuf);
3800             pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3801             bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3802         }
3803         if (!bRet) {
3804             HeapFree(GetProcessHeap(), 0, pbuf);
3805             ClosePrinter(hprn);
3806             return E_FAIL;
3807         }
3808
3809         needed = 1024;
3810         dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3811         bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3812         if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3813             HeapFree(GetProcessHeap(), 0, dbuf);
3814             dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3815             bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3816         }
3817         if (!bRet) {
3818             ERR("GetPrinterDriverW failed, last error %d, fix your config for printer %s!\n",
3819                 GetLastError(), debugstr_w(pbuf->pPrinterName));
3820             HeapFree(GetProcessHeap(), 0, dbuf);
3821             HeapFree(GetProcessHeap(), 0, pbuf);
3822             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3823             ClosePrinter(hprn);
3824             return E_FAIL;
3825         }
3826         ClosePrinter(hprn);
3827
3828         PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
3829                       dbuf->pDriverPath,
3830                       pbuf->pPrinterName,
3831                       pbuf->pPortName);
3832         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
3833                          pbuf->pDevMode->dmDriverExtra);
3834         if (lppd->hDevMode) {
3835             ptr = GlobalLock(lppd->hDevMode);
3836             if (ptr) {
3837                 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
3838                     pbuf->pDevMode->dmDriverExtra);
3839                 GlobalUnlock(lppd->hDevMode);
3840                 ret = S_OK;
3841             }
3842         }
3843         HeapFree(GetProcessHeap(), 0, pbuf);
3844         HeapFree(GetProcessHeap(), 0, dbuf);
3845
3846         return ret;
3847     }
3848
3849     return E_NOTIMPL;
3850 }