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