msxml3: Add ISAXAttributes_getValueFromName implementation.
[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 static const WCHAR pd32_collateW[] = { 'P', 'D', '3', '2', '_', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
79 static const WCHAR pd32_nocollateW[] = { 'P', 'D', '3', '2', '_', 'N', 'O', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
80 static const WCHAR pd32_portraitW[] = { 'P', 'D', '3', '2', '_', 'P', 'O', 'R', 'T', 'R', 'A', 'I', 'T', 0 };
81 static const WCHAR pd32_landscapeW[] = { 'P', 'D', '3', '2', '_', 'L', 'A', 'N', 'D', 'S', 'C', 'A', 'P', 'E', 0 };
82 static const WCHAR propW[] = {'_','_','W','I','N','E','_','P','R','I','N','T','D','L','G','D','A','T','A',0};
83
84 /***********************************************************************
85  *    PRINTDLG_OpenDefaultPrinter
86  *
87  * Returns a winspool printer handle to the default printer in *hprn
88  * Caller must call ClosePrinter on the handle
89  *
90  * Returns TRUE on success else FALSE
91  */
92 BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
93 {
94     WCHAR buf[260];
95     DWORD dwBufLen = sizeof(buf) / sizeof(buf[0]);
96     BOOL res;
97     if(!GetDefaultPrinterW(buf, &dwBufLen))
98         return FALSE;
99     res = OpenPrinterW(buf, hprn, NULL);
100     if (!res)
101         WARN("Could not open printer %s\n", debugstr_w(buf));
102     return res;
103 }
104
105 /***********************************************************************
106  *    PRINTDLG_SetUpPrinterListCombo
107  *
108  * Initializes printer list combox.
109  * hDlg:  HWND of dialog
110  * id:    Control id of combo
111  * name:  Name of printer to select
112  *
113  * Initializes combo with list of available printers.  Selects printer 'name'
114  * If name is NULL or does not exist select the default printer.
115  *
116  * Returns number of printers added to list.
117  */
118 INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
119 {
120     DWORD needed, num;
121     INT i;
122     LPPRINTER_INFO_2A pi;
123     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
124     pi = HeapAlloc(GetProcessHeap(), 0, needed);
125     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
126                   &num);
127
128     SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
129     
130     for(i = 0; i < num; i++) {
131         SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
132                             (LPARAM)pi[i].pPrinterName );
133     }
134     HeapFree(GetProcessHeap(), 0, pi);
135     if(!name ||
136        (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
137                                 (LPARAM)name)) == CB_ERR) {
138
139         char buf[260];
140         DWORD dwBufLen = sizeof(buf);
141         if (name != NULL)
142             WARN("Can't find %s in printer list so trying to find default\n",
143                 debugstr_a(name));
144         if(!GetDefaultPrinterA(buf, &dwBufLen))
145             return num;
146         i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
147         if(i == CB_ERR)
148             FIXME("Can't find default printer in printer list\n");
149     }
150     SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
151     return num;
152 }
153
154 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
155 {
156     DWORD needed, num;
157     INT i;
158     LPPRINTER_INFO_2W pi;
159     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
160     pi = HeapAlloc(GetProcessHeap(), 0, needed);
161     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
162                   &num);
163
164     for(i = 0; i < num; i++) {
165         SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
166                             (LPARAM)pi[i].pPrinterName );
167     }
168     HeapFree(GetProcessHeap(), 0, pi);
169     if(!name ||
170        (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
171                                 (LPARAM)name)) == CB_ERR) {
172         WCHAR buf[260];
173         DWORD dwBufLen = sizeof(buf)/sizeof(buf[0]);
174         if (name != NULL)
175             WARN("Can't find %s in printer list so trying to find default\n",
176                 debugstr_w(name));
177         if(!GetDefaultPrinterW(buf, &dwBufLen))
178             return num;
179         i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
180         if(i == CB_ERR)
181             TRACE("Can't find default printer in printer list\n");
182     }
183     SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
184     return num;
185 }
186
187 /***********************************************************************
188  *             PRINTDLG_CreateDevNames          [internal]
189  *
190  *
191  *   creates a DevNames structure.
192  *
193  *  (NB. when we handle unicode the offsets will be in wchars).
194  */
195 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
196                                     const char* DeviceName, const char* OutputPort)
197 {
198     long size;
199     char*   pDevNamesSpace;
200     char*   pTempPtr;
201     LPDEVNAMES lpDevNames;
202     char buf[260];
203     DWORD dwBufLen = sizeof(buf);
204
205     size = strlen(DeviceDriverName) + 1
206             + strlen(DeviceName) + 1
207             + strlen(OutputPort) + 1
208             + sizeof(DEVNAMES);
209
210     if(*hmem)
211         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
212     else
213         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
214     if (*hmem == 0)
215         return FALSE;
216
217     pDevNamesSpace = GlobalLock(*hmem);
218     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
219
220     pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
221     strcpy(pTempPtr, DeviceDriverName);
222     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
223
224     pTempPtr += strlen(DeviceDriverName) + 1;
225     strcpy(pTempPtr, DeviceName);
226     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
227
228     pTempPtr += strlen(DeviceName) + 1;
229     strcpy(pTempPtr, OutputPort);
230     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
231
232     GetDefaultPrinterA(buf, &dwBufLen);
233     lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
234     GlobalUnlock(*hmem);
235     return TRUE;
236 }
237
238 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
239                                     LPCWSTR DeviceName, LPCWSTR OutputPort)
240 {
241     long size;
242     LPWSTR   pDevNamesSpace;
243     LPWSTR   pTempPtr;
244     LPDEVNAMES lpDevNames;
245     WCHAR bufW[260];
246     DWORD dwBufLen = sizeof(bufW) / sizeof(WCHAR);
247
248     size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
249             + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
250             + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
251             + sizeof(DEVNAMES);
252
253     if(*hmem)
254         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
255     else
256         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
257     if (*hmem == 0)
258         return FALSE;
259
260     pDevNamesSpace = GlobalLock(*hmem);
261     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
262
263     pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
264     lstrcpyW(pTempPtr, DeviceDriverName);
265     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
266
267     pTempPtr += lstrlenW(DeviceDriverName) + 1;
268     lstrcpyW(pTempPtr, DeviceName);
269     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
270
271     pTempPtr += lstrlenW(DeviceName) + 1;
272     lstrcpyW(pTempPtr, OutputPort);
273     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
274
275     GetDefaultPrinterW(bufW, &dwBufLen);
276     lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
277     GlobalUnlock(*hmem);
278     return TRUE;
279 }
280
281 /***********************************************************************
282  *             PRINTDLG_UpdatePrintDlg          [internal]
283  *
284  *
285  *   updates the PrintDlg structure for return values.
286  *
287  * RETURNS
288  *   FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
289  *   TRUE  if successful.
290  */
291 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
292                                     PRINT_PTRA* PrintStructures)
293 {
294     LPPRINTDLGA       lppd = PrintStructures->lpPrintDlg;
295     PDEVMODEA         lpdm = PrintStructures->lpDevMode;
296     LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
297
298
299     if(!lpdm) {
300         FIXME("No lpdm ptr?\n");
301         return FALSE;
302     }
303
304
305     if(!(lppd->Flags & PD_PRINTSETUP)) {
306         /* check whether nFromPage and nToPage are within range defined by
307          * nMinPage and nMaxPage
308          */
309         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
310             WORD nToPage;
311             WORD nFromPage;
312             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
313             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
314             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
315                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
316                 WCHAR resourcestr[256];
317                 WCHAR resultstr[256];
318                 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
319                 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
320                 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
321                 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
322                 return FALSE;
323             }
324             lppd->nFromPage = nFromPage;
325             lppd->nToPage   = nToPage;
326             lppd->Flags |= PD_PAGENUMS;
327         }
328         else
329             lppd->Flags &= ~PD_PAGENUMS;
330
331         if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
332             lppd->Flags |= PD_SELECTION;
333         else
334             lppd->Flags &= ~PD_SELECTION;
335
336         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
337             static char file[] = "FILE:";
338             lppd->Flags |= PD_PRINTTOFILE;
339             pi->pPortName = file;
340         }
341
342         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
343             FIXME("Collate lppd not yet implemented as output\n");
344         }
345
346         /* set PD_Collate and nCopies */
347         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
348           /*  The application doesn't support multiple copies or collate...
349            */
350             lppd->Flags &= ~PD_COLLATE;
351             lppd->nCopies = 1;
352           /* if the printer driver supports it... store info there
353            * otherwise no collate & multiple copies !
354            */
355             if (lpdm->dmFields & DM_COLLATE)
356                 lpdm->dmCollate =
357                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
358             if (lpdm->dmFields & DM_COPIES)
359                 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
360         } else {
361             /* Application is responsible for multiple copies */
362             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
363                 lppd->Flags |= PD_COLLATE;
364             else
365                lppd->Flags &= ~PD_COLLATE;
366             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
367             /* multiple copies already included in the document. Driver must print only one copy */
368             lpdm->u1.s1.dmCopies = 1;
369         }
370
371         /* Print quality, PrintDlg16 */
372         if(GetDlgItem(hDlg, cmb1))
373         {
374             HWND hQuality = GetDlgItem(hDlg, cmb1);
375             int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
376
377             if(Sel != CB_ERR)
378             {
379                 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
380                 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
381                 lpdm->u1.s1.dmPrintQuality = LOWORD(dpi);
382                 lpdm->dmYResolution = HIWORD(dpi);
383             }
384         }
385     }
386     return TRUE;
387 }
388
389 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
390                                     PRINT_PTRW* PrintStructures)
391 {
392     LPPRINTDLGW       lppd = PrintStructures->lpPrintDlg;
393     PDEVMODEW         lpdm = PrintStructures->lpDevMode;
394     LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
395
396
397     if(!lpdm) {
398         FIXME("No lpdm ptr?\n");
399         return FALSE;
400     }
401
402
403     if(!(lppd->Flags & PD_PRINTSETUP)) {
404         /* check whether nFromPage and nToPage are within range defined by
405          * nMinPage and nMaxPage
406          */
407         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
408             WORD nToPage;
409             WORD nFromPage;
410             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
411             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
412             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
413                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
414                 WCHAR resourcestr[256];
415                 WCHAR resultstr[256];
416                 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
417                             resourcestr, 255);
418                 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
419                 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
420                             resourcestr, 255);
421                 MessageBoxW(hDlg, resultstr, resourcestr,
422                             MB_OK | MB_ICONWARNING);
423                 return FALSE;
424             }
425             lppd->nFromPage = nFromPage;
426             lppd->nToPage   = nToPage;
427             lppd->Flags |= PD_PAGENUMS;
428         }
429         else
430             lppd->Flags &= ~PD_PAGENUMS;
431
432         if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
433             lppd->Flags |= PD_SELECTION;
434         else
435             lppd->Flags &= ~PD_SELECTION;
436
437         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
438             static WCHAR file[] = {'F','I','L','E',':',0};
439             lppd->Flags |= PD_PRINTTOFILE;
440             pi->pPortName = file;
441         }
442
443         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
444             FIXME("Collate lppd not yet implemented as output\n");
445         }
446
447         /* set PD_Collate and nCopies */
448         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
449           /*  The application doesn't support multiple copies or collate...
450            */
451             lppd->Flags &= ~PD_COLLATE;
452             lppd->nCopies = 1;
453           /* if the printer driver supports it... store info there
454            * otherwise no collate & multiple copies !
455            */
456             if (lpdm->dmFields & DM_COLLATE)
457                 lpdm->dmCollate =
458                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
459             if (lpdm->dmFields & DM_COPIES)
460                 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
461         } else {
462             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
463                 lppd->Flags |= PD_COLLATE;
464             else
465                lppd->Flags &= ~PD_COLLATE;
466             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
467         }
468     }
469     return TRUE;
470 }
471
472 static BOOL PRINTDLG_PaperSizeA(
473         PRINTDLGA       *pdlga,const WORD PaperSize,LPPOINT size
474 ) {
475     DEVNAMES    *dn;
476     DEVMODEA    *dm;
477     LPSTR       devname,portname;
478     int         i;
479     INT         NrOfEntries,ret;
480     WORD        *Words = NULL;
481     POINT       *points = NULL;
482     BOOL        retval = FALSE;
483
484     dn = GlobalLock(pdlga->hDevNames);
485     dm = GlobalLock(pdlga->hDevMode);
486     devname     = ((char*)dn)+dn->wDeviceOffset;
487     portname    = ((char*)dn)+dn->wOutputOffset;
488
489
490     NrOfEntries = DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,NULL,dm);
491     if (!NrOfEntries) {
492         FIXME("No papernames found for %s/%s\n",devname,portname);
493         goto out;
494     }
495     if (NrOfEntries == -1) {
496         ERR("Hmm ? DeviceCapabilities() DC_PAPERNAMES failed, ret -1 !\n");
497         goto out;
498     }
499
500     Words = HeapAlloc(GetProcessHeap(),0,NrOfEntries*sizeof(WORD));
501     if (NrOfEntries != (ret=DeviceCapabilitiesA(devname,portname,DC_PAPERS,(LPSTR)Words,dm))) {
502         FIXME("Number of returned vals %d is not %d\n",NrOfEntries,ret);
503         goto out;
504     }
505     for (i=0;i<NrOfEntries;i++)
506         if (Words[i] == PaperSize)
507             break;
508     if (i == NrOfEntries) {
509         FIXME("Papersize %d not found in list?\n",PaperSize);
510         goto out;
511     }
512     points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
513     if (NrOfEntries!=(ret=DeviceCapabilitiesA(devname,portname,DC_PAPERSIZE,(LPSTR)points,dm))) {
514         FIXME("Number of returned sizes %d is not %d?\n",NrOfEntries,ret);
515         goto out;
516     }
517     /* this is _10ths_ of a millimeter */
518     size->x=points[i].x;
519     size->y=points[i].y;
520     retval = TRUE;
521 out:
522     GlobalUnlock(pdlga->hDevNames);
523     GlobalUnlock(pdlga->hDevMode);
524     HeapFree(GetProcessHeap(),0,Words);
525     HeapFree(GetProcessHeap(),0,points);
526     return retval;
527 }
528
529 static BOOL PRINTDLG_PaperSizeW(
530         PRINTDLGW       *pdlga,const WCHAR *PaperSize,LPPOINT size
531 ) {
532     DEVNAMES    *dn;
533     DEVMODEW    *dm;
534     LPWSTR      devname,portname;
535     int         i;
536     INT         NrOfEntries,ret;
537     WCHAR       *Names = NULL;
538     POINT       *points = NULL;
539     BOOL        retval = FALSE;
540
541     dn = GlobalLock(pdlga->hDevNames);
542     dm = GlobalLock(pdlga->hDevMode);
543     devname     = ((WCHAR*)dn)+dn->wDeviceOffset;
544     portname    = ((WCHAR*)dn)+dn->wOutputOffset;
545
546
547     NrOfEntries = DeviceCapabilitiesW(devname,portname,DC_PAPERNAMES,NULL,dm);
548     if (!NrOfEntries) {
549         FIXME("No papernames found for %s/%s\n",debugstr_w(devname),debugstr_w(portname));
550         goto out;
551     }
552     if (NrOfEntries == -1) {
553         ERR("Hmm ? DeviceCapabilities() DC_PAPERNAMES failed, ret -1 !\n");
554         goto out;
555     }
556
557     Names = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*NrOfEntries*64);
558     if (NrOfEntries != (ret=DeviceCapabilitiesW(devname,portname,DC_PAPERNAMES,Names,dm))) {
559         FIXME("Number of returned vals %d is not %d\n",NrOfEntries,ret);
560         goto out;
561     }
562     for (i=0;i<NrOfEntries;i++)
563         if (!lstrcmpW(PaperSize,Names+(64*i)))
564             break;
565     if (i==NrOfEntries) {
566         FIXME("Papersize %s not found in list?\n",debugstr_w(PaperSize));
567         goto out;
568     }
569     points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
570     if (NrOfEntries!=(ret=DeviceCapabilitiesW(devname,portname,DC_PAPERSIZE,(LPWSTR)points,dm))) {
571         FIXME("Number of returned sizes %d is not %d?\n",NrOfEntries,ret);
572         goto out;
573     }
574     /* this is _10ths_ of a millimeter */
575     size->x=points[i].x;
576     size->y=points[i].y;
577     retval = TRUE;
578 out:
579     GlobalUnlock(pdlga->hDevNames);
580     GlobalUnlock(pdlga->hDevMode);
581     HeapFree(GetProcessHeap(),0,Names);
582     HeapFree(GetProcessHeap(),0,points);
583     return retval;
584 }
585
586
587 /************************************************************************
588  * PRINTDLG_SetUpPaperComboBox
589  *
590  * Initialize either the papersize or inputslot combos of the Printer Setup
591  * dialog.  We store the associated word (eg DMPAPER_A4) as the item data.
592  * We also try to re-select the old selection.
593  */
594 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
595                                         int   nIDComboBox,
596                                         char* PrinterName,
597                                         char* PortName,
598                                         LPDEVMODEA dm)
599 {
600     int     i;
601     int     NrOfEntries;
602     char*   Names;
603     WORD*   Words;
604     DWORD   Sel;
605     WORD    oldWord = 0;
606     int     NamesSize;
607     int     fwCapability_Names;
608     int     fwCapability_Words;
609
610     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
611
612     /* query the dialog box for the current selected value */
613     Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
614     if(Sel != CB_ERR) {
615         /* we enter here only if a different printer is selected after
616          * the Print Setup dialog is opened. The current settings are
617          * stored into the newly selected printer.
618          */
619         oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
620                                       Sel, 0);
621         if (dm) {
622             if (nIDComboBox == cmb2)
623                 dm->u1.s1.dmPaperSize = oldWord;
624             else
625                 dm->u1.s1.dmDefaultSource = oldWord;
626         }
627     }
628     else {
629         /* we enter here only when the Print setup dialog is initially
630          * opened. In this case the settings are restored from when
631          * the dialog was last closed.
632          */
633         if (dm) {
634             if (nIDComboBox == cmb2)
635                 oldWord = dm->u1.s1.dmPaperSize;
636             else
637                 oldWord = dm->u1.s1.dmDefaultSource;
638         }
639     }
640
641     if (nIDComboBox == cmb2) {
642          NamesSize          = 64;
643          fwCapability_Names = DC_PAPERNAMES;
644          fwCapability_Words = DC_PAPERS;
645     } else {
646          nIDComboBox        = cmb3;
647          NamesSize          = 24;
648          fwCapability_Names = DC_BINNAMES;
649          fwCapability_Words = DC_BINS;
650     }
651
652     /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the
653      * paper settings. As Wine doesn't allow VXDs, this results in a crash.
654      */
655     WARN(" if your printer driver uses VXDs, expect a crash now!\n");
656     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
657                                       fwCapability_Names, NULL, dm);
658     if (NrOfEntries == 0)
659          WARN("no Name Entries found!\n");
660     else if (NrOfEntries < 0)
661          return FALSE;
662
663     if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
664        != NrOfEntries) {
665         ERR("Number of caps is different\n");
666         NrOfEntries = 0;
667     }
668
669     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
670     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
671     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
672                                       fwCapability_Names, Names, dm);
673     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
674                                       fwCapability_Words, (LPSTR)Words, dm);
675
676     /* reset any current content in the combobox */
677     SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
678
679     /* store new content */
680     for (i = 0; i < NrOfEntries; i++) {
681         DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
682                                         (LPARAM)(&Names[i*NamesSize]) );
683         SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
684                             Words[i]);
685     }
686
687     /* Look for old selection - can't do this is previous loop since
688        item order will change as more items are added */
689     Sel = 0;
690     for (i = 0; i < NrOfEntries; i++) {
691         if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
692            oldWord) {
693             Sel = i;
694             break;
695         }
696     }
697     SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
698
699     HeapFree(GetProcessHeap(),0,Words);
700     HeapFree(GetProcessHeap(),0,Names);
701     return TRUE;
702 }
703
704 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
705                                         int   nIDComboBox,
706                                         const WCHAR* PrinterName,
707                                         const WCHAR* PortName,
708                                         LPDEVMODEW dm)
709 {
710     int     i;
711     int     NrOfEntries;
712     WCHAR*  Names;
713     WORD*   Words;
714     DWORD   Sel;
715     WORD    oldWord = 0;
716     int     NamesSize;
717     int     fwCapability_Names;
718     int     fwCapability_Words;
719
720     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
721
722     /* query the dialog box for the current selected value */
723     Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
724     if(Sel != CB_ERR) {
725         /* we enter here only if a different printer is selected after
726          * the Print Setup dialog is opened. The current settings are
727          * stored into the newly selected printer.
728          */
729         oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
730                                       Sel, 0);
731         if (dm) {
732             if (nIDComboBox == cmb2)
733                 dm->u1.s1.dmPaperSize = oldWord;
734             else
735                 dm->u1.s1.dmDefaultSource = oldWord;
736         }
737     }
738     else {
739         /* we enter here only when the Print setup dialog is initially
740          * opened. In this case the settings are restored from when
741          * the dialog was last closed.
742          */
743         if (dm) {
744             if (nIDComboBox == cmb2)
745                 oldWord = dm->u1.s1.dmPaperSize;
746             else
747                 oldWord = dm->u1.s1.dmDefaultSource;
748         }
749     }
750
751     if (nIDComboBox == cmb2) {
752          NamesSize          = 64;
753          fwCapability_Names = DC_PAPERNAMES;
754          fwCapability_Words = DC_PAPERS;
755     } else {
756          nIDComboBox        = cmb3;
757          NamesSize          = 24;
758          fwCapability_Names = DC_BINNAMES;
759          fwCapability_Words = DC_BINS;
760     }
761
762     /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the
763      * paper settings. As Wine doesn't allow VXDs, this results in a crash.
764      */
765     WARN(" if your printer driver uses VXDs, expect a crash now!\n");
766     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
767                                       fwCapability_Names, NULL, dm);
768     if (NrOfEntries == 0)
769          WARN("no Name Entries found!\n");
770     else if (NrOfEntries < 0)
771          return FALSE;
772
773     if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
774        != NrOfEntries) {
775         ERR("Number of caps is different\n");
776         NrOfEntries = 0;
777     }
778
779     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
780     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
781     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
782                                       fwCapability_Names, Names, dm);
783     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
784                                       fwCapability_Words, (LPWSTR)Words, dm);
785
786     /* reset any current content in the combobox */
787     SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
788
789     /* store new content */
790     for (i = 0; i < NrOfEntries; i++) {
791         DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
792                                         (LPARAM)(&Names[i*NamesSize]) );
793         SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
794                             Words[i]);
795     }
796
797     /* Look for old selection - can't do this is previous loop since
798        item order will change as more items are added */
799     Sel = 0;
800     for (i = 0; i < NrOfEntries; i++) {
801         if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
802            oldWord) {
803             Sel = i;
804             break;
805         }
806     }
807     SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
808
809     HeapFree(GetProcessHeap(),0,Words);
810     HeapFree(GetProcessHeap(),0,Names);
811     return TRUE;
812 }
813
814
815 /***********************************************************************
816  *               PRINTDLG_UpdatePrinterInfoTexts               [internal]
817  */
818 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
819 {
820     char   StatusMsg[256];
821     char   ResourceString[256];
822     int    i;
823
824     /* Status Message */
825     StatusMsg[0]='\0';
826
827     /* add all status messages */
828     for (i = 0; i < 25; i++) {
829         if (pi->Status & (1<<i)) {
830             LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
831                         ResourceString, 255);
832             strcat(StatusMsg,ResourceString);
833         }
834     }
835     /* append "ready" */
836     /* FIXME: status==ready must only be appended if really so.
837               but how to detect? */
838     LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
839                 ResourceString, 255);
840     strcat(StatusMsg,ResourceString);
841     SetDlgItemTextA(hDlg, stc12, StatusMsg);
842
843     /* set all other printer info texts */
844     SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
845     
846     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
847         SetDlgItemTextA(hDlg, stc14, pi->pLocation);
848     else
849         SetDlgItemTextA(hDlg, stc14, pi->pPortName);
850     SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
851     return;
852 }
853
854 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
855 {
856     WCHAR   StatusMsg[256];
857     WCHAR   ResourceString[256];
858     static const WCHAR emptyW[] = {0};
859     int    i;
860
861     /* Status Message */
862     StatusMsg[0]='\0';
863
864     /* add all status messages */
865     for (i = 0; i < 25; i++) {
866         if (pi->Status & (1<<i)) {
867             LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
868                         ResourceString, 255);
869             lstrcatW(StatusMsg,ResourceString);
870         }
871     }
872     /* append "ready" */
873     /* FIXME: status==ready must only be appended if really so.
874               but how to detect? */
875     LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
876                 ResourceString, 255);
877     lstrcatW(StatusMsg,ResourceString);
878     SetDlgItemTextW(hDlg, stc12, StatusMsg);
879
880     /* set all other printer info texts */
881     SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
882     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
883         SetDlgItemTextW(hDlg, stc14, pi->pLocation);
884     else
885         SetDlgItemTextW(hDlg, stc14, pi->pPortName);
886     SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : emptyW);
887 }
888
889
890 /*******************************************************************
891  *
892  *                 PRINTDLG_ChangePrinter
893  *
894  */
895 BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name,
896                                    PRINT_PTRA *PrintStructures)
897 {
898     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
899     LPDEVMODEA lpdm = NULL;
900     LONG dmSize;
901     DWORD needed;
902     HANDLE hprn;
903
904     HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
905     HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
906     if(!OpenPrinterA(name, &hprn, NULL)) {
907         ERR("Can't open printer %s\n", name);
908         return FALSE;
909     }
910     GetPrinterA(hprn, 2, NULL, 0, &needed);
911     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
912     GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
913                 &needed);
914     GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
915     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
916     if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
917             needed, &needed)) {
918         ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
919         return FALSE;
920     }
921     ClosePrinter(hprn);
922
923     PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
924
925     HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
926     PrintStructures->lpDevMode = NULL;
927
928     dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
929     if(dmSize == -1) {
930         ERR("DocumentProperties fails on %s\n", debugstr_a(name));
931         return FALSE;
932     }
933     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
934     dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
935                                  DM_OUT_BUFFER);
936     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
937                           !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
938                                      (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
939       /* Supplied devicemode matches current printer so try to use it */
940         DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
941                             DM_OUT_BUFFER | DM_IN_BUFFER);
942     }
943     if(lpdm)
944         GlobalUnlock(lppd->hDevMode);
945
946     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
947
948     if(!(lppd->Flags & PD_PRINTSETUP)) {
949       /* Print range (All/Range/Selection) */
950         SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
951         SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
952         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
953         if (lppd->Flags & PD_NOSELECTION)
954             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
955         else
956             if (lppd->Flags & PD_SELECTION)
957                 CheckRadioButton(hDlg, rad1, rad3, rad2);
958         if (lppd->Flags & PD_NOPAGENUMS) {
959             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
960             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
961             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
962             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
963             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
964         } else {
965             if (lppd->Flags & PD_PAGENUMS)
966                 CheckRadioButton(hDlg, rad1, rad3, rad3);
967         }
968
969         /* Collate pages
970          *
971          * FIXME: The ico3 is not displayed for some reason. I don't know why.
972          */
973         if (lppd->Flags & PD_COLLATE) {
974             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
975                                 (LPARAM)PrintStructures->hCollateIcon);
976             CheckDlgButton(hDlg, chx2, 1);
977         } else {
978             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
979                                 (LPARAM)PrintStructures->hNoCollateIcon);
980             CheckDlgButton(hDlg, chx2, 0);
981         }
982
983         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
984           /* if printer doesn't support it: no Collate */
985             if (!(lpdm->dmFields & DM_COLLATE)) {
986                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
987                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
988             }
989         }
990
991         /* nCopies */
992         {
993           INT copies;
994           if (lppd->hDevMode == 0)
995               copies = lppd->nCopies;
996           else
997               copies = lpdm->u1.s1.dmCopies;
998           if(copies == 0) copies = 1;
999           else if(copies < 0) copies = MAX_COPIES;
1000           SetDlgItemInt(hDlg, edt3, copies, FALSE);
1001         }
1002
1003         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1004           /* if printer doesn't support it: no nCopies */
1005             if (!(lpdm->dmFields & DM_COPIES)) {
1006                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1007                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1008             }
1009         }
1010
1011         /* print to file */
1012         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1013         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1014             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1015         if (lppd->Flags & PD_HIDEPRINTTOFILE)
1016             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1017
1018         /* Fill print quality combo, PrintDlg16 */
1019         if(GetDlgItem(hDlg, cmb1))
1020         {
1021             DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1022                                                        PrintStructures->lpPrinterInfo->pPortName,
1023                                                        DC_ENUMRESOLUTIONS, NULL, lpdm);
1024
1025             if(numResolutions != -1)
1026             {
1027                 HWND hQuality = GetDlgItem(hDlg, cmb1);
1028                 LONG* Resolutions;
1029                 char buf[255];
1030                 int i;
1031                 int dpiX, dpiY;
1032                 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
1033                                            PrintStructures->lpPrinterInfo->pPrinterName,
1034                                            0, lpdm);
1035
1036                 Resolutions = HeapAlloc(GetProcessHeap(), 0, numResolutions*sizeof(LONG)*2);
1037                 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1038                                     PrintStructures->lpPrinterInfo->pPortName,
1039                                     DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
1040
1041                 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
1042                 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
1043                 DeleteDC(hPrinterDC);
1044
1045                 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
1046                 for(i = 0; i < (numResolutions * 2); i += 2)
1047                 {
1048                     BOOL IsDefault = FALSE;
1049                     LRESULT Index;
1050
1051                     if(Resolutions[i] == Resolutions[i+1])
1052                     {
1053                         if(dpiX == Resolutions[i])
1054                             IsDefault = TRUE;
1055                         sprintf(buf, "%d dpi", Resolutions[i]);
1056                     } else
1057                     {
1058                         if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1059                             IsDefault = TRUE;
1060                         sprintf(buf, "%d dpi x %d dpi", Resolutions[i], Resolutions[i+1]);
1061                     }
1062
1063                     Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1064
1065                     if(IsDefault)
1066                         SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1067
1068                     SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1069                 }
1070                 HeapFree(GetProcessHeap(), 0, Resolutions);
1071             }
1072         }
1073     } else { /* PD_PRINTSETUP */
1074       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1075
1076       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1077                                   PrintStructures->lpPrinterInfo->pPrinterName,
1078                                   PrintStructures->lpPrinterInfo->pPortName,
1079                                   lpdm);
1080       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1081                                   PrintStructures->lpPrinterInfo->pPrinterName,
1082                                   PrintStructures->lpPrinterInfo->pPortName,
1083                                   lpdm);
1084       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1085       SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1086                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1087                                    PrintStructures->hLandscapeIcon));
1088
1089     }
1090
1091     /* help button */
1092     if ((lppd->Flags & PD_SHOWHELP)==0) {
1093         /* hide if PD_SHOWHELP not specified */
1094         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1095     }
1096     return TRUE;
1097 }
1098
1099 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1100                                    PRINT_PTRW *PrintStructures)
1101 {
1102     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1103     LPDEVMODEW lpdm = NULL;
1104     LONG dmSize;
1105     DWORD needed;
1106     HANDLE hprn;
1107
1108     HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1109     HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1110     if(!OpenPrinterW(name, &hprn, NULL)) {
1111         ERR("Can't open printer %s\n", debugstr_w(name));
1112         return FALSE;
1113     }
1114     GetPrinterW(hprn, 2, NULL, 0, &needed);
1115     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*needed);
1116     GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1117                 &needed);
1118     GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1119     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*needed);
1120     if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1121             needed, &needed)) {
1122         ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1123         return FALSE;
1124     }
1125     ClosePrinter(hprn);
1126
1127     PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1128
1129     HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1130     PrintStructures->lpDevMode = NULL;
1131
1132     dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1133     if(dmSize == -1) {
1134         ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1135         return FALSE;
1136     }
1137     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1138     dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1139                                  DM_OUT_BUFFER);
1140     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1141                           !lstrcmpW(lpdm->dmDeviceName,
1142                                   PrintStructures->lpDevMode->dmDeviceName)) {
1143       /* Supplied devicemode matches current printer so try to use it */
1144         DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1145                             DM_OUT_BUFFER | DM_IN_BUFFER);
1146     }
1147     if(lpdm)
1148         GlobalUnlock(lppd->hDevMode);
1149
1150     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
1151
1152     if(!(lppd->Flags & PD_PRINTSETUP)) {
1153       /* Print range (All/Range/Selection) */
1154         SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1155         SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1156         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
1157         if (lppd->Flags & PD_NOSELECTION)
1158             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1159         else
1160             if (lppd->Flags & PD_SELECTION)
1161                 CheckRadioButton(hDlg, rad1, rad3, rad2);
1162         if (lppd->Flags & PD_NOPAGENUMS) {
1163             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1164             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1165             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1166             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1167             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1168         } else {
1169             if (lppd->Flags & PD_PAGENUMS)
1170                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1171         }
1172
1173         /* Collate pages
1174          *
1175          * FIXME: The ico3 is not displayed for some reason. I don't know why.
1176          */
1177         if (lppd->Flags & PD_COLLATE) {
1178             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1179                                 (LPARAM)PrintStructures->hCollateIcon);
1180             CheckDlgButton(hDlg, chx2, 1);
1181         } else {
1182             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1183                                 (LPARAM)PrintStructures->hNoCollateIcon);
1184             CheckDlgButton(hDlg, chx2, 0);
1185         }
1186
1187         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1188           /* if printer doesn't support it: no Collate */
1189             if (!(lpdm->dmFields & DM_COLLATE)) {
1190                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1191                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1192             }
1193         }
1194
1195         /* nCopies */
1196         {
1197           INT copies;
1198           if (lppd->hDevMode == 0)
1199               copies = lppd->nCopies;
1200           else
1201               copies = lpdm->u1.s1.dmCopies;
1202           if(copies == 0) copies = 1;
1203           else if(copies < 0) copies = MAX_COPIES;
1204           SetDlgItemInt(hDlg, edt3, copies, FALSE);
1205         }
1206
1207         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1208           /* if printer doesn't support it: no nCopies */
1209             if (!(lpdm->dmFields & DM_COPIES)) {
1210                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1211                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1212             }
1213         }
1214
1215         /* print to file */
1216         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1217         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1218             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1219         if (lppd->Flags & PD_HIDEPRINTTOFILE)
1220             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1221
1222     } else { /* PD_PRINTSETUP */
1223       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1224
1225       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1226                                   PrintStructures->lpPrinterInfo->pPrinterName,
1227                                   PrintStructures->lpPrinterInfo->pPortName,
1228                                   lpdm);
1229       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1230                                   PrintStructures->lpPrinterInfo->pPrinterName,
1231                                   PrintStructures->lpPrinterInfo->pPortName,
1232                                   lpdm);
1233       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1234       SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1235                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1236                                    PrintStructures->hLandscapeIcon));
1237
1238     }
1239
1240     /* help button */
1241     if ((lppd->Flags & PD_SHOWHELP)==0) {
1242         /* hide if PD_SHOWHELP not specified */
1243         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1244     }
1245     return TRUE;
1246 }
1247
1248  /***********************************************************************
1249  *           check_printer_setup                        [internal]
1250  */
1251 static LRESULT check_printer_setup(HWND hDlg)
1252 {
1253     DWORD needed,num;
1254     WCHAR resourcestr[256],resultstr[256];
1255
1256     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1257     if(needed == 0)
1258     {
1259           EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1260     }
1261     if(needed > 0)
1262           return TRUE;
1263     else
1264     {
1265           LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1266           LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1267           MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1268           return FALSE;
1269     }
1270 }
1271
1272 /***********************************************************************
1273  *           PRINTDLG_WMInitDialog                      [internal]
1274  */
1275 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
1276                                      PRINT_PTRA* PrintStructures)
1277 {
1278     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1279     DEVNAMES *pdn;
1280     DEVMODEA *pdm;
1281     char *name = NULL;
1282     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1283
1284     /* load Collate ICONs */
1285     /* We load these with LoadImage because they are not a standard
1286        size and we don't want them rescaled */
1287     PrintStructures->hCollateIcon =
1288       LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1289     PrintStructures->hNoCollateIcon =
1290       LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1291
1292     /* These can be done with LoadIcon */
1293     PrintStructures->hPortraitIcon =
1294       LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1295     PrintStructures->hLandscapeIcon =
1296       LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1297
1298     /* display the collate/no_collate icon */
1299     SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1300                         (LPARAM)PrintStructures->hNoCollateIcon);
1301
1302     if(PrintStructures->hCollateIcon == 0 ||
1303        PrintStructures->hNoCollateIcon == 0 ||
1304        PrintStructures->hPortraitIcon == 0 ||
1305        PrintStructures->hLandscapeIcon == 0) {
1306         ERR("no icon in resourcefile\n");
1307         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1308         EndDialog(hDlg, FALSE);
1309     }
1310
1311     /*
1312      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1313      * must be registered and the Help button must be shown.
1314      */
1315     if (lppd->Flags & PD_SHOWHELP) {
1316         if((PrintStructures->HelpMessageID =
1317             RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1318             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1319             return FALSE;
1320         }
1321     } else
1322         PrintStructures->HelpMessageID = 0;
1323
1324     if(!(lppd->Flags &PD_PRINTSETUP)) {
1325         PrintStructures->hwndUpDown =
1326           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1327                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1328                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1329                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1330                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1331     }
1332
1333     /* FIXME: I allow more freedom than either Win95 or WinNT,
1334      *        which do not agree to what errors should be thrown or not
1335      *        in case nToPage or nFromPage is out-of-range.
1336      */
1337     if (lppd->nMaxPage < lppd->nMinPage)
1338         lppd->nMaxPage = lppd->nMinPage;
1339     if (lppd->nMinPage == lppd->nMaxPage)
1340         lppd->Flags |= PD_NOPAGENUMS;
1341     if (lppd->nToPage < lppd->nMinPage)
1342         lppd->nToPage = lppd->nMinPage;
1343     if (lppd->nToPage > lppd->nMaxPage)
1344         lppd->nToPage = lppd->nMaxPage;
1345     if (lppd->nFromPage < lppd->nMinPage)
1346         lppd->nFromPage = lppd->nMinPage;
1347     if (lppd->nFromPage > lppd->nMaxPage)
1348         lppd->nFromPage = lppd->nMaxPage;
1349
1350     /* if we have the combo box, fill it */
1351     if (GetDlgItem(hDlg,comboID)) {
1352         /* Fill Combobox
1353          */
1354         pdn = GlobalLock(lppd->hDevNames);
1355         pdm = GlobalLock(lppd->hDevMode);
1356         if(pdn)
1357             name = (char*)pdn + pdn->wDeviceOffset;
1358         else if(pdm)
1359             name = (char*)pdm->dmDeviceName;
1360         PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1361         if(pdm) GlobalUnlock(lppd->hDevMode);
1362         if(pdn) GlobalUnlock(lppd->hDevNames);
1363
1364         /* Now find selected printer and update rest of dlg */
1365         name = HeapAlloc(GetProcessHeap(),0,256);
1366         if (GetDlgItemTextA(hDlg, comboID, name, 255))
1367             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1368         HeapFree(GetProcessHeap(),0,name);
1369     } else {
1370         /* else use default printer */
1371         char name[200];
1372         DWORD dwBufLen = sizeof(name);
1373         BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1374
1375         if (ret)
1376             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1377         else
1378             FIXME("No default printer found, expect problems!\n");
1379     }
1380     return TRUE;
1381 }
1382
1383 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg, WPARAM wParam,
1384                                      PRINT_PTRW* PrintStructures)
1385 {
1386     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1387     DEVNAMES *pdn;
1388     DEVMODEW *pdm;
1389     WCHAR *name = NULL;
1390     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1391
1392     /* load Collate ICONs */
1393     /* We load these with LoadImage because they are not a standard
1394        size and we don't want them rescaled */
1395     PrintStructures->hCollateIcon =
1396       LoadImageW(COMDLG32_hInstance, pd32_collateW, IMAGE_ICON, 0, 0, 0);
1397     PrintStructures->hNoCollateIcon =
1398       LoadImageW(COMDLG32_hInstance, pd32_nocollateW, IMAGE_ICON, 0, 0, 0);
1399
1400     /* These can be done with LoadIcon */
1401     PrintStructures->hPortraitIcon =
1402       LoadIconW(COMDLG32_hInstance, pd32_portraitW);
1403     PrintStructures->hLandscapeIcon =
1404       LoadIconW(COMDLG32_hInstance, pd32_landscapeW);
1405
1406     /* display the collate/no_collate icon */
1407     SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1408                         (LPARAM)PrintStructures->hNoCollateIcon);
1409
1410     if(PrintStructures->hCollateIcon == 0 ||
1411        PrintStructures->hNoCollateIcon == 0 ||
1412        PrintStructures->hPortraitIcon == 0 ||
1413        PrintStructures->hLandscapeIcon == 0) {
1414         ERR("no icon in resourcefile\n");
1415         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1416         EndDialog(hDlg, FALSE);
1417     }
1418
1419     /*
1420      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1421      * must be registered and the Help button must be shown.
1422      */
1423     if (lppd->Flags & PD_SHOWHELP) {
1424         if((PrintStructures->HelpMessageID =
1425             RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1426             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1427             return FALSE;
1428         }
1429     } else
1430         PrintStructures->HelpMessageID = 0;
1431
1432     if(!(lppd->Flags &PD_PRINTSETUP)) {
1433         PrintStructures->hwndUpDown =
1434           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1435                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1436                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1437                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1438                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1439     }
1440
1441     /* FIXME: I allow more freedom than either Win95 or WinNT,
1442      *        which do not agree to what errors should be thrown or not
1443      *        in case nToPage or nFromPage is out-of-range.
1444      */
1445     if (lppd->nMaxPage < lppd->nMinPage)
1446         lppd->nMaxPage = lppd->nMinPage;
1447     if (lppd->nMinPage == lppd->nMaxPage)
1448         lppd->Flags |= PD_NOPAGENUMS;
1449     if (lppd->nToPage < lppd->nMinPage)
1450         lppd->nToPage = lppd->nMinPage;
1451     if (lppd->nToPage > lppd->nMaxPage)
1452         lppd->nToPage = lppd->nMaxPage;
1453     if (lppd->nFromPage < lppd->nMinPage)
1454         lppd->nFromPage = lppd->nMinPage;
1455     if (lppd->nFromPage > lppd->nMaxPage)
1456         lppd->nFromPage = lppd->nMaxPage;
1457
1458     /* if we have the combo box, fill it */
1459     if (GetDlgItem(hDlg,comboID)) {
1460         /* Fill Combobox
1461          */
1462         pdn = GlobalLock(lppd->hDevNames);
1463         pdm = GlobalLock(lppd->hDevMode);
1464         if(pdn)
1465             name = (WCHAR*)pdn + pdn->wDeviceOffset;
1466         else if(pdm)
1467             name = pdm->dmDeviceName;
1468         PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1469         if(pdm) GlobalUnlock(lppd->hDevMode);
1470         if(pdn) GlobalUnlock(lppd->hDevNames);
1471
1472         /* Now find selected printer and update rest of dlg */
1473         /* ansi is ok here */
1474         name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1475         if (GetDlgItemTextW(hDlg, comboID, name, 255))
1476             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1477         HeapFree(GetProcessHeap(),0,name);
1478     } else {
1479         /* else use default printer */
1480         WCHAR name[200];
1481         DWORD dwBufLen = sizeof(name) / sizeof(WCHAR);
1482         BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1483
1484         if (ret)
1485             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1486         else
1487             FIXME("No default printer found, expect problems!\n");
1488     }
1489     return TRUE;
1490 }
1491
1492 /***********************************************************************
1493  *                              PRINTDLG_WMCommand               [internal]
1494  */
1495 LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1496                         LPARAM lParam, PRINT_PTRA* PrintStructures)
1497 {
1498     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1499     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1500     LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1501
1502     switch (LOWORD(wParam))  {
1503     case IDOK:
1504         TRACE(" OK button was hit\n");
1505         if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1506             FIXME("Update printdlg was not successful!\n");
1507             return(FALSE);
1508         }
1509         EndDialog(hDlg, TRUE);
1510         return(TRUE);
1511
1512     case IDCANCEL:
1513         TRACE(" CANCEL button was hit\n");
1514         EndDialog(hDlg, FALSE);
1515         return(FALSE);
1516
1517      case pshHelp:
1518         TRACE(" HELP button was hit\n");
1519         SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1520                                 (WPARAM) hDlg, (LPARAM) lppd);
1521         break;
1522
1523      case chx2:                         /* collate pages checkbox */
1524         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1525             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1526                                     (LPARAM)PrintStructures->hCollateIcon);
1527         else
1528             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1529                                     (LPARAM)PrintStructures->hNoCollateIcon);
1530         break;
1531      case edt1:                         /* from page nr editbox */
1532      case edt2:                         /* to page nr editbox */
1533         if (HIWORD(wParam)==EN_CHANGE) {
1534             WORD nToPage;
1535             WORD nFromPage;
1536             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1537             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1538             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1539                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1540         }
1541         break;
1542
1543     case edt3:
1544         if(HIWORD(wParam) == EN_CHANGE) {
1545             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1546             if(copies <= 1)
1547                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1548             else
1549                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1550         }
1551         break;
1552
1553 #if 0
1554      case psh1:                       /* Print Setup */
1555         {
1556             PRINTDLG16  pdlg;
1557
1558             if (!PrintStructures->dlg.lpPrintDlg16) {
1559                 FIXME("The 32bit print dialog does not have this button!?\n");
1560                 break;
1561             }
1562
1563             memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1564             pdlg.Flags |= PD_PRINTSETUP;
1565             pdlg.hwndOwner = HWND_16(hDlg);
1566             if (!PrintDlg16(&pdlg))
1567                 break;
1568         }
1569         break;
1570 #endif
1571      case psh2:                       /* Properties button */
1572        {
1573          HANDLE hPrinter;
1574          char   PrinterName[256];
1575
1576          GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1577          if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1578              FIXME(" Call to OpenPrinter did not succeed!\n");
1579              break;
1580          }
1581          DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1582                              PrintStructures->lpDevMode,
1583                              PrintStructures->lpDevMode,
1584                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1585          ClosePrinter(hPrinter);
1586          break;
1587        }
1588
1589     case rad1: /* Paperorientation */
1590         if (lppd->Flags & PD_PRINTSETUP)
1591         {
1592               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1593               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1594                           (LPARAM)(PrintStructures->hPortraitIcon));
1595         }
1596         break;
1597
1598     case rad2: /* Paperorientation */
1599         if (lppd->Flags & PD_PRINTSETUP)
1600         {
1601               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1602               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1603                           (LPARAM)(PrintStructures->hLandscapeIcon));
1604         }
1605         break;
1606
1607     case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1608          if (PrinterComboID != LOWORD(wParam)) {
1609              break;
1610          }
1611          /* FALLTHROUGH */
1612     case cmb4:                         /* Printer combobox */
1613          if (HIWORD(wParam)==CBN_SELCHANGE) {
1614              char   PrinterName[256];
1615              GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1616              PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1617          }
1618          break;
1619
1620     case cmb2: /* Papersize */
1621       {
1622           DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1623           if(Sel != CB_ERR)
1624               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1625                                                             CB_GETITEMDATA,
1626                                                             Sel, 0);
1627       }
1628       break;
1629
1630     case cmb3: /* Bin */
1631       {
1632           DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1633           if(Sel != CB_ERR)
1634               lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1635                                                           CB_GETITEMDATA, Sel,
1636                                                           0);
1637       }
1638       break;
1639     }
1640     if(lppd->Flags & PD_PRINTSETUP) {
1641         switch (LOWORD(wParam)) {
1642         case rad1:                         /* orientation */
1643         case rad2:
1644             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1645                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1646                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1647                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1648                                         (WPARAM)IMAGE_ICON,
1649                                         (LPARAM)PrintStructures->hPortraitIcon);
1650                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1651                                         (WPARAM)IMAGE_ICON,
1652                                         (LPARAM)PrintStructures->hPortraitIcon);
1653                 }
1654             } else {
1655                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1656                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1657                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1658                                         (WPARAM)IMAGE_ICON,
1659                                         (LPARAM)PrintStructures->hLandscapeIcon);
1660                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1661                                         (WPARAM)IMAGE_ICON,
1662                                         (LPARAM)PrintStructures->hLandscapeIcon);
1663                 }
1664             }
1665             break;
1666         }
1667     }
1668     return FALSE;
1669 }
1670
1671 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1672                         LPARAM lParam, PRINT_PTRW* PrintStructures)
1673 {
1674     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1675     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1676     LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1677
1678     switch (LOWORD(wParam))  {
1679     case IDOK:
1680         TRACE(" OK button was hit\n");
1681         if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1682             FIXME("Update printdlg was not successful!\n");
1683             return(FALSE);
1684         }
1685         EndDialog(hDlg, TRUE);
1686         return(TRUE);
1687
1688     case IDCANCEL:
1689         TRACE(" CANCEL button was hit\n");
1690         EndDialog(hDlg, FALSE);
1691         return(FALSE);
1692
1693      case pshHelp:
1694         TRACE(" HELP button was hit\n");
1695         SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1696                                 (WPARAM) hDlg, (LPARAM) lppd);
1697         break;
1698
1699      case chx2:                         /* collate pages checkbox */
1700         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1701             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1702                                     (LPARAM)PrintStructures->hCollateIcon);
1703         else
1704             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1705                                     (LPARAM)PrintStructures->hNoCollateIcon);
1706         break;
1707      case edt1:                         /* from page nr editbox */
1708      case edt2:                         /* to page nr editbox */
1709         if (HIWORD(wParam)==EN_CHANGE) {
1710             WORD nToPage;
1711             WORD nFromPage;
1712             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1713             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1714             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1715                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1716         }
1717         break;
1718
1719     case edt3:
1720         if(HIWORD(wParam) == EN_CHANGE) {
1721             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1722             if(copies <= 1)
1723                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1724             else
1725                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1726         }
1727         break;
1728
1729      case psh1:                       /* Print Setup */
1730         {
1731                 ERR("psh1 is called from 16bit code only, we should not get here.\n");
1732         }
1733         break;
1734      case psh2:                       /* Properties button */
1735        {
1736          HANDLE hPrinter;
1737          WCHAR  PrinterName[256];
1738
1739          if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1740          if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1741              FIXME(" Call to OpenPrinter did not succeed!\n");
1742              break;
1743          }
1744          DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1745                              PrintStructures->lpDevMode,
1746                              PrintStructures->lpDevMode,
1747                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1748          ClosePrinter(hPrinter);
1749          break;
1750        }
1751
1752     case rad1: /* Paperorientation */
1753         if (lppd->Flags & PD_PRINTSETUP)
1754         {
1755               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1756               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1757                           (LPARAM)(PrintStructures->hPortraitIcon));
1758         }
1759         break;
1760
1761     case rad2: /* Paperorientation */
1762         if (lppd->Flags & PD_PRINTSETUP)
1763         {
1764               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1765               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1766                           (LPARAM)(PrintStructures->hLandscapeIcon));
1767         }
1768         break;
1769
1770     case cmb1: /* Printer Combobox in PRINT SETUP */
1771          /* FALLTHROUGH */
1772     case cmb4:                         /* Printer combobox */
1773          if (HIWORD(wParam)==CBN_SELCHANGE) {
1774              WCHAR   PrinterName[256];
1775              GetDlgItemTextW(hDlg, LOWORD(wParam), PrinterName, 255);
1776              PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1777          }
1778          break;
1779
1780     case cmb2: /* Papersize */
1781       {
1782           DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1783           if(Sel != CB_ERR)
1784               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1785                                                             CB_GETITEMDATA,
1786                                                             Sel, 0);
1787       }
1788       break;
1789
1790     case cmb3: /* Bin */
1791       {
1792           DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1793           if(Sel != CB_ERR)
1794               lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1795                                                           CB_GETITEMDATA, Sel,
1796                                                           0);
1797       }
1798       break;
1799     }
1800     if(lppd->Flags & PD_PRINTSETUP) {
1801         switch (LOWORD(wParam)) {
1802         case rad1:                         /* orientation */
1803         case rad2:
1804             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1805                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1806                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1807                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1808                                         (WPARAM)IMAGE_ICON,
1809                                         (LPARAM)PrintStructures->hPortraitIcon);
1810                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1811                                         (WPARAM)IMAGE_ICON,
1812                                         (LPARAM)PrintStructures->hPortraitIcon);
1813                 }
1814             } else {
1815                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1816                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1817                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1818                                         (WPARAM)IMAGE_ICON,
1819                                         (LPARAM)PrintStructures->hLandscapeIcon);
1820                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1821                                         (WPARAM)IMAGE_ICON,
1822                                         (LPARAM)PrintStructures->hLandscapeIcon);
1823                 }
1824             }
1825             break;
1826         }
1827     }
1828     return FALSE;
1829 }
1830
1831 /***********************************************************************
1832  *           PrintDlgProcA                      [internal]
1833  */
1834 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1835                                       LPARAM lParam)
1836 {
1837     PRINT_PTRA* PrintStructures;
1838     INT_PTR res = FALSE;
1839
1840     if (uMsg!=WM_INITDIALOG) {
1841         PrintStructures = (PRINT_PTRA*)GetPropA(hDlg,"__WINE_PRINTDLGDATA");
1842         if (!PrintStructures)
1843             return FALSE;
1844     } else {
1845         PrintStructures = (PRINT_PTRA*) lParam;
1846         SetPropA(hDlg,"__WINE_PRINTDLGDATA",PrintStructures);
1847         if(!check_printer_setup(hDlg))
1848         {
1849             EndDialog(hDlg,FALSE);
1850             return FALSE;
1851         }
1852         res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1853
1854         if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1855             res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1856                 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1857             );
1858         return res;
1859     }
1860
1861     if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1862         res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1863                                                          lParam);
1864         if(res) return res;
1865     }
1866
1867     switch (uMsg) {
1868     case WM_COMMAND:
1869         return PRINTDLG_WMCommandA(hDlg, wParam, lParam, PrintStructures);
1870
1871     case WM_DESTROY:
1872         DestroyIcon(PrintStructures->hCollateIcon);
1873         DestroyIcon(PrintStructures->hNoCollateIcon);
1874         DestroyIcon(PrintStructures->hPortraitIcon);
1875         DestroyIcon(PrintStructures->hLandscapeIcon);
1876         if(PrintStructures->hwndUpDown)
1877             DestroyWindow(PrintStructures->hwndUpDown);
1878         return FALSE;
1879     }
1880     return res;
1881 }
1882
1883 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
1884                                       LPARAM lParam)
1885 {
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  *   Not fully implemented
3753  *
3754  */
3755 HRESULT WINAPI PrintDlgExW(LPPRINTDLGEXW lppd)
3756 {
3757     DWORD     ret = E_FAIL;
3758     LPVOID    ptr;
3759
3760     FIXME("(%p) not fully implemented\n", lppd);
3761
3762     if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXW))) {
3763         return E_INVALIDARG;
3764     }
3765
3766     if (!IsWindow(lppd->hwndOwner)) {
3767         return E_HANDLE;
3768     }
3769
3770     if (lppd->Flags & PD_RETURNDEFAULT) {
3771         PRINTER_INFO_2W *pbuf;
3772         DRIVER_INFO_2W  *dbuf;
3773         HANDLE hprn;
3774         DWORD needed = 1024;
3775         BOOL bRet;
3776
3777         if (lppd->hDevMode || lppd->hDevNames) {
3778             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
3779             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3780             return E_INVALIDARG;
3781         }
3782         if (!PRINTDLG_OpenDefaultPrinter(&hprn)) {
3783             WARN("Can't find default printer\n");
3784             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3785             return E_FAIL;
3786         }
3787
3788         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3789         bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3790         if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3791             HeapFree(GetProcessHeap(), 0, pbuf);
3792             pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3793             bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3794         }
3795         if (!bRet) {
3796             HeapFree(GetProcessHeap(), 0, pbuf);
3797             ClosePrinter(hprn);
3798             return E_FAIL;
3799         }
3800
3801         needed = 1024;
3802         dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3803         bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3804         if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3805             HeapFree(GetProcessHeap(), 0, dbuf);
3806             dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3807             bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3808         }
3809         if (!bRet) {
3810             ERR("GetPrinterDriverW failed, last error %d, fix your config for printer %s!\n",
3811                 GetLastError(), debugstr_w(pbuf->pPrinterName));
3812             HeapFree(GetProcessHeap(), 0, dbuf);
3813             HeapFree(GetProcessHeap(), 0, pbuf);
3814             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3815             ClosePrinter(hprn);
3816             return E_FAIL;
3817         }
3818         ClosePrinter(hprn);
3819
3820         PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
3821                       dbuf->pDriverPath,
3822                       pbuf->pPrinterName,
3823                       pbuf->pPortName);
3824         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
3825                          pbuf->pDevMode->dmDriverExtra);
3826         if (lppd->hDevMode) {
3827             ptr = GlobalLock(lppd->hDevMode);
3828             if (ptr) {
3829                 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
3830                     pbuf->pDevMode->dmDriverExtra);
3831                 GlobalUnlock(lppd->hDevMode);
3832                 ret = S_OK;
3833             }
3834         }
3835         HeapFree(GetProcessHeap(), 0, pbuf);
3836         HeapFree(GetProcessHeap(), 0, dbuf);
3837
3838         return ret;
3839     }
3840
3841     return E_NOTIMPL;
3842 }