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