Janitorial: C booleans must not be compared against TRUE.
[wine] / dlls / commdlg / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 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 /***********************************************************************
75  *    PRINTDLG_OpenDefaultPrinter
76  *
77  * Returns a winspool printer handle to the default printer in *hprn
78  * Caller must call ClosePrinter on the handle
79  *
80  * Returns TRUE on success else FALSE
81  */
82 BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
83 {
84     char buf[260];
85     DWORD dwBufLen = sizeof(buf);
86     BOOL res;
87     if(!GetDefaultPrinterA(buf, &dwBufLen))
88         return FALSE;
89     res = OpenPrinterA(buf, hprn, NULL);
90     if (!res)
91         FIXME("Could not open printer %s?!\n",buf);
92     return res;
93 }
94
95 /***********************************************************************
96  *    PRINTDLG_SetUpPrinterListCombo
97  *
98  * Initializes printer list combox.
99  * hDlg:  HWND of dialog
100  * id:    Control id of combo
101  * name:  Name of printer to select
102  *
103  * Initializes combo with list of available printers.  Selects printer 'name'
104  * If name is NULL or does not exist select the default printer.
105  *
106  * Returns number of printers added to list.
107  */
108 INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
109 {
110     DWORD needed, num;
111     INT i;
112     LPPRINTER_INFO_2A pi;
113     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
114     pi = HeapAlloc(GetProcessHeap(), 0, needed);
115     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
116                   &num);
117
118     for(i = 0; i < num; i++) {
119         SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
120                             (LPARAM)pi[i].pPrinterName );
121     }
122     HeapFree(GetProcessHeap(), 0, pi);
123     if(!name ||
124        (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
125                                 (LPARAM)name)) == CB_ERR) {
126
127         char buf[260];
128         DWORD dwBufLen = sizeof(buf);
129         FIXME("Can't find '%s' in printer list so trying to find default\n",
130               name);
131         if(!GetDefaultPrinterA(buf, &dwBufLen))
132             return num;
133         i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
134         if(i == CB_ERR)
135             FIXME("Can't find default printer in printer list\n");
136     }
137     SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
138     return num;
139 }
140
141 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
142 {
143     DWORD needed, num;
144     INT i;
145     LPPRINTER_INFO_2W pi;
146     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
147     pi = HeapAlloc(GetProcessHeap(), 0, needed);
148     EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
149                   &num);
150
151     for(i = 0; i < num; i++) {
152         SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
153                             (LPARAM)pi[i].pPrinterName );
154     }
155     HeapFree(GetProcessHeap(), 0, pi);
156     if(!name ||
157        (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
158                                 (LPARAM)name)) == CB_ERR) {
159         WCHAR buf[260];
160         DWORD dwBufLen = sizeof(buf)/sizeof(buf[0]);
161         TRACE("Can't find '%s' in printer list so trying to find default\n",
162               debugstr_w(name));
163         if(!GetDefaultPrinterW(buf, &dwBufLen))
164             return num;
165         i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
166         if(i == CB_ERR)
167             TRACE("Can't find default printer in printer list\n");
168     }
169     SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
170     return num;
171 }
172
173 /***********************************************************************
174  *             PRINTDLG_CreateDevNames          [internal]
175  *
176  *
177  *   creates a DevNames structure.
178  *
179  *  (NB. when we handle unicode the offsets will be in wchars).
180  */
181 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, char* DeviceDriverName,
182                                     char* DeviceName, char* OutputPort)
183 {
184     long size;
185     char*   pDevNamesSpace;
186     char*   pTempPtr;
187     LPDEVNAMES lpDevNames;
188     char buf[260];
189     DWORD dwBufLen = sizeof(buf);
190
191     size = strlen(DeviceDriverName) + 1
192             + strlen(DeviceName) + 1
193             + strlen(OutputPort) + 1
194             + sizeof(DEVNAMES);
195
196     if(*hmem)
197         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
198     else
199         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
200     if (*hmem == 0)
201         return FALSE;
202
203     pDevNamesSpace = GlobalLock(*hmem);
204     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
205
206     pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
207     strcpy(pTempPtr, DeviceDriverName);
208     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
209
210     pTempPtr += strlen(DeviceDriverName) + 1;
211     strcpy(pTempPtr, DeviceName);
212     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
213
214     pTempPtr += strlen(DeviceName) + 1;
215     strcpy(pTempPtr, OutputPort);
216     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
217
218     GetDefaultPrinterA(buf, &dwBufLen);
219     lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
220     GlobalUnlock(*hmem);
221     return TRUE;
222 }
223
224 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
225                                     LPCWSTR DeviceName, LPCWSTR OutputPort)
226 {
227     long size;
228     LPWSTR   pDevNamesSpace;
229     LPWSTR   pTempPtr;
230     LPDEVNAMES lpDevNames;
231     WCHAR bufW[260];
232     DWORD dwBufLen = sizeof(bufW) / sizeof(WCHAR);
233
234     size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
235             + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
236             + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
237             + sizeof(DEVNAMES);
238
239     if(*hmem)
240         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
241     else
242         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
243     if (*hmem == 0)
244         return FALSE;
245
246     pDevNamesSpace = GlobalLock(*hmem);
247     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
248
249     pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
250     lstrcpyW(pTempPtr, DeviceDriverName);
251     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
252
253     pTempPtr += lstrlenW(DeviceDriverName) + 1;
254     lstrcpyW(pTempPtr, DeviceName);
255     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
256
257     pTempPtr += lstrlenW(DeviceName) + 1;
258     lstrcpyW(pTempPtr, OutputPort);
259     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
260
261     GetDefaultPrinterW(bufW, &dwBufLen);
262     lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
263     GlobalUnlock(*hmem);
264     return TRUE;
265 }
266
267 /***********************************************************************
268  *             PRINTDLG_UpdatePrintDlg          [internal]
269  *
270  *
271  *   updates the PrintDlg structure for return values.
272  *
273  * RETURNS
274  *   FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
275  *   TRUE  if successful.
276  */
277 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
278                                     PRINT_PTRA* PrintStructures)
279 {
280     LPPRINTDLGA       lppd = PrintStructures->lpPrintDlg;
281     PDEVMODEA         lpdm = PrintStructures->lpDevMode;
282     LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
283
284
285     if(!lpdm) {
286         FIXME("No lpdm ptr?\n");
287         return FALSE;
288     }
289
290
291     if(!(lppd->Flags & PD_PRINTSETUP)) {
292         /* check whether nFromPage and nToPage are within range defined by
293          * nMinPage and nMaxPage
294          */
295         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
296             WORD nToPage;
297             WORD nFromPage;
298             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
299             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
300             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
301                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
302                 char resourcestr[256];
303                 char resultstr[256];
304                 LoadStringA(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
305                             resourcestr, 255);
306                 sprintf(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
307                 LoadStringA(COMDLG32_hInstance, PD32_PRINT_TITLE,
308                             resourcestr, 255);
309                 MessageBoxA(hDlg, resultstr, resourcestr,
310                             MB_OK | MB_ICONWARNING);
311                 return FALSE;
312             }
313             lppd->nFromPage = nFromPage;
314             lppd->nToPage   = nToPage;
315             lppd->Flags |= PD_PAGENUMS;
316         }
317         else
318             lppd->Flags &= ~PD_PAGENUMS;
319
320         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
321             lppd->Flags |= PD_PRINTTOFILE;
322             pi->pPortName = "FILE:";
323         }
324
325         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
326             FIXME("Collate lppd not yet implemented as output\n");
327         }
328
329         /* set PD_Collate and nCopies */
330         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
331           /*  The application doesn't support multiple copies or collate...
332            */
333             lppd->Flags &= ~PD_COLLATE;
334             lppd->nCopies = 1;
335           /* if the printer driver supports it... store info there
336            * otherwise no collate & multiple copies !
337            */
338             if (lpdm->dmFields & DM_COLLATE)
339                 lpdm->dmCollate =
340                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
341             if (lpdm->dmFields & DM_COPIES)
342                 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
343         } else {
344             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
345                 lppd->Flags |= PD_COLLATE;
346             else
347                lppd->Flags &= ~PD_COLLATE;
348             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
349         }
350     }
351     return TRUE;
352 }
353
354 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
355                                     PRINT_PTRW* PrintStructures)
356 {
357     LPPRINTDLGW       lppd = PrintStructures->lpPrintDlg;
358     PDEVMODEW         lpdm = PrintStructures->lpDevMode;
359     LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
360
361
362     if(!lpdm) {
363         FIXME("No lpdm ptr?\n");
364         return FALSE;
365     }
366
367
368     if(!(lppd->Flags & PD_PRINTSETUP)) {
369         /* check whether nFromPage and nToPage are within range defined by
370          * nMinPage and nMaxPage
371          */
372         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
373             WORD nToPage;
374             WORD nFromPage;
375             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
376             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
377             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
378                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
379                 WCHAR resourcestr[256];
380                 WCHAR resultstr[256];
381                 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
382                             resourcestr, 255);
383                 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
384                 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
385                             resourcestr, 255);
386                 MessageBoxW(hDlg, resultstr, resourcestr,
387                             MB_OK | MB_ICONWARNING);
388                 return FALSE;
389             }
390             lppd->nFromPage = nFromPage;
391             lppd->nToPage   = nToPage;
392         }
393
394         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
395             static WCHAR file[] = {'F','I','L','E',':',0};
396             lppd->Flags |= PD_PRINTTOFILE;
397             pi->pPortName = file;
398         }
399
400         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
401             FIXME("Collate lppd not yet implemented as output\n");
402         }
403
404         /* set PD_Collate and nCopies */
405         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
406           /*  The application doesn't support multiple copies or collate...
407            */
408             lppd->Flags &= ~PD_COLLATE;
409             lppd->nCopies = 1;
410           /* if the printer driver supports it... store info there
411            * otherwise no collate & multiple copies !
412            */
413             if (lpdm->dmFields & DM_COLLATE)
414                 lpdm->dmCollate =
415                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
416             if (lpdm->dmFields & DM_COPIES)
417                 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
418         } else {
419             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
420                 lppd->Flags |= PD_COLLATE;
421             else
422                lppd->Flags &= ~PD_COLLATE;
423             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
424         }
425     }
426     return TRUE;
427 }
428
429 static BOOL PRINTDLG_PaperSizeA(
430         PRINTDLGA       *pdlga,const char *PaperSize,LPPOINT size
431 ) {
432     DEVNAMES    *dn;
433     DEVMODEA    *dm;
434     LPSTR       devname,portname;
435     int         i;
436     INT         NrOfEntries,ret;
437     char        *Names = NULL;
438     POINT       *points = NULL;
439     BOOL        retval = FALSE;
440
441     dn = GlobalLock(pdlga->hDevNames);
442     dm = GlobalLock(pdlga->hDevMode);
443     devname     = ((char*)dn)+dn->wDeviceOffset;
444     portname    = ((char*)dn)+dn->wOutputOffset;
445
446
447     NrOfEntries = DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,NULL,dm);
448     if (!NrOfEntries) {
449         FIXME("No papernames found for %s/%s\n",devname,portname);
450         goto out;
451     }
452     if (NrOfEntries == -1) {
453         ERR("Hmm ? DeviceCapabilities() DC_PAPERNAMES failed, ret -1 !\n");
454         goto out;
455     }
456
457     Names = (char*)HeapAlloc(GetProcessHeap(),0,NrOfEntries*64);
458     if (NrOfEntries != (ret=DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,Names,dm))) {
459         FIXME("Number of returned vals %d is not %d\n",NrOfEntries,ret);
460         goto out;
461     }
462     for (i=0;i<NrOfEntries;i++)
463         if (!strcmp(PaperSize,Names+(64*i)))
464             break;
465     HeapFree(GetProcessHeap(),0,Names);
466     if (i==NrOfEntries) {
467         FIXME("Papersize %s not found in list?\n",PaperSize);
468         goto out;
469     }
470     points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
471     if (NrOfEntries!=(ret=DeviceCapabilitiesA(devname,portname,DC_PAPERSIZE,(LPBYTE)points,dm))) {
472         FIXME("Number of returned sizes %d is not %d?\n",NrOfEntries,ret);
473         goto out;
474     }
475     /* this is _10ths_ of a millimeter */
476     size->x=points[i].x;
477     size->y=points[i].y;
478     retval = TRUE;
479 out:
480     GlobalUnlock(pdlga->hDevNames);
481     GlobalUnlock(pdlga->hDevMode);
482     if (Names) HeapFree(GetProcessHeap(),0,Names);
483     if (points) HeapFree(GetProcessHeap(),0,points);
484     return retval;
485 }
486
487 static BOOL PRINTDLG_PaperSizeW(
488         PRINTDLGW       *pdlga,const WCHAR *PaperSize,LPPOINT size
489 ) {
490     DEVNAMES    *dn;
491     DEVMODEW    *dm;
492     LPWSTR      devname,portname;
493     int         i;
494     INT         NrOfEntries,ret;
495     WCHAR       *Names = NULL;
496     POINT       *points = NULL;
497     BOOL        retval = FALSE;
498
499     dn = GlobalLock(pdlga->hDevNames);
500     dm = GlobalLock(pdlga->hDevMode);
501     devname     = ((WCHAR*)dn)+dn->wDeviceOffset;
502     portname    = ((WCHAR*)dn)+dn->wOutputOffset;
503
504
505     NrOfEntries = DeviceCapabilitiesW(devname,portname,DC_PAPERNAMES,NULL,dm);
506     if (!NrOfEntries) {
507         FIXME("No papernames found for %s/%s\n",debugstr_w(devname),debugstr_w(portname));
508         goto out;
509     }
510     if (NrOfEntries == -1) {
511         ERR("Hmm ? DeviceCapabilities() DC_PAPERNAMES failed, ret -1 !\n");
512         goto out;
513     }
514
515     Names = (WCHAR*)HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*NrOfEntries*64);
516     if (NrOfEntries != (ret=DeviceCapabilitiesW(devname,portname,DC_PAPERNAMES,Names,dm))) {
517         FIXME("Number of returned vals %d is not %d\n",NrOfEntries,ret);
518         goto out;
519     }
520     for (i=0;i<NrOfEntries;i++)
521         if (!lstrcmpW(PaperSize,Names+(64*i)))
522             break;
523     HeapFree(GetProcessHeap(),0,Names);
524     if (i==NrOfEntries) {
525         FIXME("Papersize %s not found in list?\n",debugstr_w(PaperSize));
526         goto out;
527     }
528     points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
529     if (NrOfEntries!=(ret=DeviceCapabilitiesW(devname,portname,DC_PAPERSIZE,(LPWSTR)points,dm))) {
530         FIXME("Number of returned sizes %d is not %d?\n",NrOfEntries,ret);
531         goto out;
532     }
533     /* this is _10ths_ of a millimeter */
534     size->x=points[i].x;
535     size->y=points[i].y;
536     retval = TRUE;
537 out:
538     GlobalUnlock(pdlga->hDevNames);
539     GlobalUnlock(pdlga->hDevMode);
540     if (Names) HeapFree(GetProcessHeap(),0,Names);
541     if (points) HeapFree(GetProcessHeap(),0,points);
542     return retval;
543 }
544
545
546 /************************************************************************
547  * PRINTDLG_SetUpPaperComboBox
548  *
549  * Initialize either the papersize or inputslot combos of the Printer Setup
550  * dialog.  We store the associated word (eg DMPAPER_A4) as the item data.
551  * We also try to re-select the old selection.
552  */
553 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
554                                         int   nIDComboBox,
555                                         char* PrinterName,
556                                         char* PortName,
557                                         LPDEVMODEA dm)
558 {
559     int     i;
560     int     NrOfEntries;
561     char*   Names;
562     WORD*   Words;
563     DWORD   Sel;
564     WORD    oldWord = 0;
565     int     NamesSize;
566     int     fwCapability_Names;
567     int     fwCapability_Words;
568
569     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
570
571     /* query the dialog box for the current selected value */
572     Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
573     if(Sel != CB_ERR) {
574         /* we enter here only if a different printer is selected after
575          * the Print Setup dialog is opened. The current settings are
576          * stored into the newly selected printer.
577          */
578         oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
579                                       Sel, 0);
580         if (dm) {
581             if (nIDComboBox == cmb2)
582                 dm->u1.s1.dmPaperSize = oldWord;
583             else
584                 dm->dmDefaultSource = oldWord;
585         }
586     }
587     else {
588         /* we enter here only when the Print setup dialog is initially
589          * opened. In this case the settings are restored from when
590          * the dialog was last closed.
591          */
592         if (dm) {
593             if (nIDComboBox == cmb2)
594                 oldWord = dm->u1.s1.dmPaperSize;
595             else
596                 oldWord = dm->dmDefaultSource;
597         }
598     }
599
600     if (nIDComboBox == cmb2) {
601          NamesSize          = 64;
602          fwCapability_Names = DC_PAPERNAMES;
603          fwCapability_Words = DC_PAPERS;
604     } else {
605          nIDComboBox        = cmb3;
606          NamesSize          = 24;
607          fwCapability_Names = DC_BINNAMES;
608          fwCapability_Words = DC_BINS;
609     }
610
611     /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the
612      * paper settings. As Wine doesn't allow VXDs, this results in a crash.
613      */
614     WARN(" if your printer driver uses VXDs, expect a crash now!\n");
615     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
616                                       fwCapability_Names, NULL, dm);
617     if (NrOfEntries == 0)
618          WARN("no Name Entries found!\n");
619     else if (NrOfEntries < 0)
620          return FALSE;
621
622     if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
623        != NrOfEntries) {
624         ERR("Number of caps is different\n");
625         NrOfEntries = 0;
626     }
627
628     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
629     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
630     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
631                                       fwCapability_Names, Names, dm);
632     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
633                                       fwCapability_Words, (LPSTR)Words, dm);
634
635     /* reset any current content in the combobox */
636     SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
637
638     /* store new content */
639     for (i = 0; i < NrOfEntries; i++) {
640         DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
641                                         (LPARAM)(&Names[i*NamesSize]) );
642         SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
643                             Words[i]);
644     }
645
646     /* Look for old selection - can't do this is previous loop since
647        item order will change as more items are added */
648     Sel = 0;
649     for (i = 0; i < NrOfEntries; i++) {
650         if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
651            oldWord) {
652             Sel = i;
653             break;
654         }
655     }
656     SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
657
658     HeapFree(GetProcessHeap(),0,Words);
659     HeapFree(GetProcessHeap(),0,Names);
660     return TRUE;
661 }
662
663 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
664                                         int   nIDComboBox,
665                                         WCHAR* PrinterName,
666                                         WCHAR* PortName,
667                                         LPDEVMODEW dm)
668 {
669     int     i;
670     int     NrOfEntries;
671     WCHAR*  Names;
672     WORD*   Words;
673     DWORD   Sel;
674     WORD    oldWord = 0;
675     int     NamesSize;
676     int     fwCapability_Names;
677     int     fwCapability_Words;
678
679     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
680
681     /* query the dialog box for the current selected value */
682     Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
683     if(Sel != CB_ERR) {
684         /* we enter here only if a different printer is selected after
685          * the Print Setup dialog is opened. The current settings are
686          * stored into the newly selected printer.
687          */
688         oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
689                                       Sel, 0);
690         if (dm) {
691             if (nIDComboBox == cmb2)
692                 dm->u1.s1.dmPaperSize = oldWord;
693             else
694                 dm->dmDefaultSource = oldWord;
695         }
696     }
697     else {
698         /* we enter here only when the Print setup dialog is initially
699          * opened. In this case the settings are restored from when
700          * the dialog was last closed.
701          */
702         if (dm) {
703             if (nIDComboBox == cmb2)
704                 oldWord = dm->u1.s1.dmPaperSize;
705             else
706                 oldWord = dm->dmDefaultSource;
707         }
708     }
709
710     if (nIDComboBox == cmb2) {
711          NamesSize          = 64;
712          fwCapability_Names = DC_PAPERNAMES;
713          fwCapability_Words = DC_PAPERS;
714     } else {
715          nIDComboBox        = cmb3;
716          NamesSize          = 24;
717          fwCapability_Names = DC_BINNAMES;
718          fwCapability_Words = DC_BINS;
719     }
720
721     /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the
722      * paper settings. As Wine doesn't allow VXDs, this results in a crash.
723      */
724     WARN(" if your printer driver uses VXDs, expect a crash now!\n");
725     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
726                                       fwCapability_Names, NULL, dm);
727     if (NrOfEntries == 0)
728          WARN("no Name Entries found!\n");
729     else if (NrOfEntries < 0)
730          return FALSE;
731
732     if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
733        != NrOfEntries) {
734         ERR("Number of caps is different\n");
735         NrOfEntries = 0;
736     }
737
738     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
739     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
740     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
741                                       fwCapability_Names, Names, dm);
742     NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
743                                       fwCapability_Words, (LPWSTR)Words, dm);
744
745     /* reset any current content in the combobox */
746     SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
747
748     /* store new content */
749     for (i = 0; i < NrOfEntries; i++) {
750         DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
751                                         (LPARAM)(&Names[i*NamesSize]) );
752         SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
753                             Words[i]);
754     }
755
756     /* Look for old selection - can't do this is previous loop since
757        item order will change as more items are added */
758     Sel = 0;
759     for (i = 0; i < NrOfEntries; i++) {
760         if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
761            oldWord) {
762             Sel = i;
763             break;
764         }
765     }
766     SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
767
768     HeapFree(GetProcessHeap(),0,Words);
769     HeapFree(GetProcessHeap(),0,Names);
770     return TRUE;
771 }
772
773
774 /***********************************************************************
775  *               PRINTDLG_UpdatePrinterInfoTexts               [internal]
776  */
777 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, LPPRINTER_INFO_2A pi)
778 {
779     char   StatusMsg[256];
780     char   ResourceString[256];
781     int    i;
782
783     /* Status Message */
784     StatusMsg[0]='\0';
785
786     /* add all status messages */
787     for (i = 0; i < 25; i++) {
788         if (pi->Status & (1<<i)) {
789             LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
790                         ResourceString, 255);
791             strcat(StatusMsg,ResourceString);
792         }
793     }
794     /* append "ready" */
795     /* FIXME: status==ready must only be appended if really so.
796               but how to detect? */
797     LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
798                 ResourceString, 255);
799     strcat(StatusMsg,ResourceString);
800     SetDlgItemTextA(hDlg, stc12, StatusMsg);
801
802     /* set all other printer info texts */
803     SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
804     
805     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
806         SetDlgItemTextA(hDlg, stc14, pi->pLocation);
807     else
808         SetDlgItemTextA(hDlg, stc14, pi->pPortName);
809     SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
810     return;
811 }
812
813 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, LPPRINTER_INFO_2W pi)
814 {
815     WCHAR   StatusMsg[256];
816     WCHAR   ResourceString[256];
817     static const WCHAR emptyW[] = {0};
818     int    i;
819
820     /* Status Message */
821     StatusMsg[0]='\0';
822
823     /* add all status messages */
824     for (i = 0; i < 25; i++) {
825         if (pi->Status & (1<<i)) {
826             LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
827                         ResourceString, 255);
828             lstrcatW(StatusMsg,ResourceString);
829         }
830     }
831     /* append "ready" */
832     /* FIXME: status==ready must only be appended if really so.
833               but how to detect? */
834     LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
835                 ResourceString, 255);
836     lstrcatW(StatusMsg,ResourceString);
837     SetDlgItemTextW(hDlg, stc12, StatusMsg);
838
839     /* set all other printer info texts */
840     SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
841     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
842         SetDlgItemTextW(hDlg, stc14, pi->pLocation);
843     else
844         SetDlgItemTextW(hDlg, stc14, pi->pPortName);
845     SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : emptyW);
846 }
847
848
849 /*******************************************************************
850  *
851  *                 PRINTDLG_ChangePrinter
852  *
853  */
854 BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name,
855                                    PRINT_PTRA *PrintStructures)
856 {
857     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
858     LPDEVMODEA lpdm = NULL;
859     LONG dmSize;
860     DWORD needed;
861     HANDLE hprn;
862
863     if(PrintStructures->lpPrinterInfo)
864         HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
865     if(PrintStructures->lpDriverInfo)
866         HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
867     if(!OpenPrinterA(name, &hprn, NULL)) {
868         ERR("Can't open printer %s\n", name);
869         return FALSE;
870     }
871     GetPrinterA(hprn, 2, NULL, 0, &needed);
872     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
873     GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
874                 &needed);
875     GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
876     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
877     if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
878             needed, &needed)) {
879         ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
880         return FALSE;
881     }
882     ClosePrinter(hprn);
883
884     PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
885
886     if(PrintStructures->lpDevMode) {
887         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
888         PrintStructures->lpDevMode = NULL;
889     }
890
891     dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
892     if(dmSize == -1) {
893         ERR("DocumentProperties fails on %s\n", debugstr_a(name));
894         return FALSE;
895     }
896     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
897     dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
898                                  DM_OUT_BUFFER);
899     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
900                           !strcmp(lpdm->dmDeviceName,
901                                   PrintStructures->lpDevMode->dmDeviceName)) {
902       /* Supplied devicemode matches current printer so try to use it */
903         DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
904                             DM_OUT_BUFFER | DM_IN_BUFFER);
905     }
906     if(lpdm)
907         GlobalUnlock(lppd->hDevMode);
908
909     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
910
911     if(!(lppd->Flags & PD_PRINTSETUP)) {
912       /* Print range (All/Range/Selection) */
913         SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
914         SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
915         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
916         if (lppd->Flags & PD_NOSELECTION)
917             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
918         else
919             if (lppd->Flags & PD_SELECTION)
920                 CheckRadioButton(hDlg, rad1, rad3, rad2);
921         if (lppd->Flags & PD_NOPAGENUMS) {
922             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
923             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
924             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
925             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
926             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
927         } else {
928             if (lppd->Flags & PD_PAGENUMS)
929                 CheckRadioButton(hDlg, rad1, rad3, rad3);
930         }
931         /* "All xxx pages"... */
932         {
933             char        resourcestr[64];
934             char        result[64];
935             LoadStringA(COMDLG32_hInstance, PD32_PRINT_ALL_X_PAGES,
936                         resourcestr, 49);
937             sprintf(result,resourcestr,lppd->nMaxPage - lppd->nMinPage + 1);
938             SendDlgItemMessageA(hDlg, rad1, WM_SETTEXT, 0, (LPARAM) result);
939         }
940
941         /* Collate pages
942          *
943          * FIXME: The ico3 is not displayed for some reason. I don't know why.
944          */
945         if (lppd->Flags & PD_COLLATE) {
946             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
947                                 (LPARAM)PrintStructures->hCollateIcon);
948             CheckDlgButton(hDlg, chx2, 1);
949         } else {
950             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
951                                 (LPARAM)PrintStructures->hNoCollateIcon);
952             CheckDlgButton(hDlg, chx2, 0);
953         }
954
955         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
956           /* if printer doesn't support it: no Collate */
957             if (!(lpdm->dmFields & DM_COLLATE)) {
958                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
959                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
960             }
961         }
962
963         /* nCopies */
964         {
965           INT copies;
966           if (lppd->hDevMode == 0)
967               copies = lppd->nCopies;
968           else
969               copies = lpdm->dmCopies;
970           if(copies == 0) copies = 1;
971           else if(copies < 0) copies = MAX_COPIES;
972           SetDlgItemInt(hDlg, edt3, copies, FALSE);
973         }
974
975         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
976           /* if printer doesn't support it: no nCopies */
977             if (!(lpdm->dmFields & DM_COPIES)) {
978                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
979                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
980             }
981         }
982
983         /* print to file */
984         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
985         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
986             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
987         if (lppd->Flags & PD_HIDEPRINTTOFILE)
988             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
989
990     } else { /* PD_PRINTSETUP */
991       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
992
993       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
994                                   PrintStructures->lpPrinterInfo->pPrinterName,
995                                   PrintStructures->lpPrinterInfo->pPortName,
996                                   lpdm);
997       PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
998                                   PrintStructures->lpPrinterInfo->pPrinterName,
999                                   PrintStructures->lpPrinterInfo->pPortName,
1000                                   lpdm);
1001       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1002       SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1003                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1004                                    PrintStructures->hLandscapeIcon));
1005
1006     }
1007
1008     /* help button */
1009     if ((lppd->Flags & PD_SHOWHELP)==0) {
1010         /* hide if PD_SHOWHELP not specified */
1011         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1012     }
1013     return TRUE;
1014 }
1015
1016 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1017                                    PRINT_PTRW *PrintStructures)
1018 {
1019     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1020     LPDEVMODEW lpdm = NULL;
1021     LONG dmSize;
1022     DWORD needed;
1023     HANDLE hprn;
1024
1025     if(PrintStructures->lpPrinterInfo)
1026         HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1027     if(PrintStructures->lpDriverInfo)
1028         HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1029     if(!OpenPrinterW(name, &hprn, NULL)) {
1030         ERR("Can't open printer %s\n", debugstr_w(name));
1031         return FALSE;
1032     }
1033     GetPrinterW(hprn, 2, NULL, 0, &needed);
1034     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*needed);
1035     GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1036                 &needed);
1037     GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1038     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*needed);
1039     if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1040             needed, &needed)) {
1041         ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1042         return FALSE;
1043     }
1044     ClosePrinter(hprn);
1045
1046     PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1047
1048     if(PrintStructures->lpDevMode) {
1049         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1050         PrintStructures->lpDevMode = NULL;
1051     }
1052
1053     dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1054     if(dmSize == -1) {
1055         ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1056         return FALSE;
1057     }
1058     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1059     dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1060                                  DM_OUT_BUFFER);
1061     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1062                           !lstrcmpW(lpdm->dmDeviceName,
1063                                   PrintStructures->lpDevMode->dmDeviceName)) {
1064       /* Supplied devicemode matches current printer so try to use it */
1065         DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1066                             DM_OUT_BUFFER | DM_IN_BUFFER);
1067     }
1068     if(lpdm)
1069         GlobalUnlock(lppd->hDevMode);
1070
1071     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
1072
1073     if(!(lppd->Flags & PD_PRINTSETUP)) {
1074       /* Print range (All/Range/Selection) */
1075         SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1076         SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1077         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
1078         if (lppd->Flags & PD_NOSELECTION)
1079             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1080         else
1081             if (lppd->Flags & PD_SELECTION)
1082                 CheckRadioButton(hDlg, rad1, rad3, rad2);
1083         if (lppd->Flags & PD_NOPAGENUMS) {
1084             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1085             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1086             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1087             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1088             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1089         } else {
1090             if (lppd->Flags & PD_PAGENUMS)
1091                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1092         }
1093         /* "All xxx pages"... */
1094         {
1095             WCHAR        resourcestr[64];
1096             WCHAR        result[64];
1097             LoadStringW(COMDLG32_hInstance, PD32_PRINT_ALL_X_PAGES,
1098                         resourcestr, 49);
1099             wsprintfW(result,resourcestr,lppd->nMaxPage - lppd->nMinPage + 1);
1100             SendDlgItemMessageW(hDlg, rad1, WM_SETTEXT, 0, (LPARAM) result);
1101         }
1102
1103         /* Collate pages
1104          *
1105          * FIXME: The ico3 is not displayed for some reason. I don't know why.
1106          */
1107         if (lppd->Flags & PD_COLLATE) {
1108             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1109                                 (LPARAM)PrintStructures->hCollateIcon);
1110             CheckDlgButton(hDlg, chx2, 1);
1111         } else {
1112             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1113                                 (LPARAM)PrintStructures->hNoCollateIcon);
1114             CheckDlgButton(hDlg, chx2, 0);
1115         }
1116
1117         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1118           /* if printer doesn't support it: no Collate */
1119             if (!(lpdm->dmFields & DM_COLLATE)) {
1120                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1121                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1122             }
1123         }
1124
1125         /* nCopies */
1126         {
1127           INT copies;
1128           if (lppd->hDevMode == 0)
1129               copies = lppd->nCopies;
1130           else
1131               copies = lpdm->dmCopies;
1132           if(copies == 0) copies = 1;
1133           else if(copies < 0) copies = MAX_COPIES;
1134           SetDlgItemInt(hDlg, edt3, copies, FALSE);
1135         }
1136
1137         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1138           /* if printer doesn't support it: no nCopies */
1139             if (!(lpdm->dmFields & DM_COPIES)) {
1140                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1141                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1142             }
1143         }
1144
1145         /* print to file */
1146         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1147         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1148             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1149         if (lppd->Flags & PD_HIDEPRINTTOFILE)
1150             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1151
1152     } else { /* PD_PRINTSETUP */
1153       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1154
1155       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1156                                   PrintStructures->lpPrinterInfo->pPrinterName,
1157                                   PrintStructures->lpPrinterInfo->pPortName,
1158                                   lpdm);
1159       PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1160                                   PrintStructures->lpPrinterInfo->pPrinterName,
1161                                   PrintStructures->lpPrinterInfo->pPortName,
1162                                   lpdm);
1163       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1164       SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1165                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1166                                    PrintStructures->hLandscapeIcon));
1167
1168     }
1169
1170     /* help button */
1171     if ((lppd->Flags & PD_SHOWHELP)==0) {
1172         /* hide if PD_SHOWHELP not specified */
1173         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1174     }
1175     return TRUE;
1176 }
1177
1178 /***********************************************************************
1179  *           PRINTDLG_WMInitDialog                      [internal]
1180  */
1181 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
1182                                      PRINT_PTRA* PrintStructures)
1183 {
1184     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1185     DEVNAMES *pdn;
1186     DEVMODEA *pdm;
1187     char *name = NULL;
1188     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1189
1190     /* load Collate ICONs */
1191     /* We load these with LoadImage because they are not a standard
1192        size and we don't want them rescaled */
1193     PrintStructures->hCollateIcon =
1194       LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1195     PrintStructures->hNoCollateIcon =
1196       LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1197
1198     /* These can be done with LoadIcon */
1199     PrintStructures->hPortraitIcon =
1200       LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1201     PrintStructures->hLandscapeIcon =
1202       LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1203
1204     /* display the collate/no_collate icon */
1205     SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1206                         (LPARAM)PrintStructures->hNoCollateIcon);
1207
1208     if(PrintStructures->hCollateIcon == 0 ||
1209        PrintStructures->hNoCollateIcon == 0 ||
1210        PrintStructures->hPortraitIcon == 0 ||
1211        PrintStructures->hLandscapeIcon == 0) {
1212         ERR("no icon in resourcefile\n");
1213         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1214         EndDialog(hDlg, FALSE);
1215     }
1216
1217     /*
1218      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1219      * must be registered and the Help button must be shown.
1220      */
1221     if (lppd->Flags & PD_SHOWHELP) {
1222         if((PrintStructures->HelpMessageID =
1223             RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1224             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1225             return FALSE;
1226         }
1227     } else
1228         PrintStructures->HelpMessageID = 0;
1229
1230     if(!(lppd->Flags &PD_PRINTSETUP)) {
1231         PrintStructures->hwndUpDown =
1232           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1233                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1234                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1235                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1236                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1237     }
1238
1239     /* FIXME: I allow more freedom than either Win95 or WinNT,
1240      *        which do not agree to what errors should be thrown or not
1241      *        in case nToPage or nFromPage is out-of-range.
1242      */
1243     if (lppd->nMaxPage < lppd->nMinPage)
1244         lppd->nMaxPage = lppd->nMinPage;
1245     if (lppd->nMinPage == lppd->nMaxPage)
1246         lppd->Flags |= PD_NOPAGENUMS;
1247     if (lppd->nToPage < lppd->nMinPage)
1248         lppd->nToPage = lppd->nMinPage;
1249     if (lppd->nToPage > lppd->nMaxPage)
1250         lppd->nToPage = lppd->nMaxPage;
1251     if (lppd->nFromPage < lppd->nMinPage)
1252         lppd->nFromPage = lppd->nMinPage;
1253     if (lppd->nFromPage > lppd->nMaxPage)
1254         lppd->nFromPage = lppd->nMaxPage;
1255
1256     /* if we have the combo box, fill it */
1257     if (GetDlgItem(hDlg,comboID)) {
1258         /* Fill Combobox
1259          */
1260         pdn = GlobalLock(lppd->hDevNames);
1261         pdm = GlobalLock(lppd->hDevMode);
1262         if(pdn)
1263             name = (char*)pdn + pdn->wDeviceOffset;
1264         else if(pdm)
1265             name = pdm->dmDeviceName;
1266         PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1267         if(pdm) GlobalUnlock(lppd->hDevMode);
1268         if(pdn) GlobalUnlock(lppd->hDevNames);
1269
1270         /* Now find selected printer and update rest of dlg */
1271         name = HeapAlloc(GetProcessHeap(),0,256);
1272         if (GetDlgItemTextA(hDlg, comboID, name, 255))
1273             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1274         HeapFree(GetProcessHeap(),0,name);
1275     } else {
1276         /* else use default printer */
1277         char name[200];
1278         DWORD dwBufLen = sizeof(name);
1279         BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1280
1281         if (ret)
1282             PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1283         else
1284             FIXME("No default printer found, expect problems!\n");
1285     }
1286     return TRUE;
1287 }
1288
1289 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg, WPARAM wParam,
1290                                      PRINT_PTRW* PrintStructures)
1291 {
1292     const static WCHAR PD32_COLLATE[] = { 'P', 'D', '3', '2', '_', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
1293     const static WCHAR PD32_NOCOLLATE[] = { 'P', 'D', '3', '2', '_', 'N', 'O', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
1294     const static WCHAR PD32_PORTRAIT[] = { 'P', 'D', '3', '2', '_', 'P', 'O', 'R', 'T', 'R', 'A', 'I', 'T', 0 };
1295     const static WCHAR PD32_LANDSCAPE[] = { 'P', 'D', '3', '2', '_', 'L', 'A', 'N', 'D', 'S', 'C', 'A', 'P', 'E', 0 };
1296     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1297     DEVNAMES *pdn;
1298     DEVMODEW *pdm;
1299     WCHAR *name = NULL;
1300     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1301
1302     /* load Collate ICONs */
1303     /* We load these with LoadImage because they are not a standard
1304        size and we don't want them rescaled */
1305     PrintStructures->hCollateIcon =
1306       LoadImageW(COMDLG32_hInstance, PD32_COLLATE, IMAGE_ICON, 0, 0, 0);
1307     PrintStructures->hNoCollateIcon =
1308       LoadImageW(COMDLG32_hInstance, PD32_NOCOLLATE, IMAGE_ICON, 0, 0, 0);
1309
1310     /* These can be done with LoadIcon */
1311     PrintStructures->hPortraitIcon =
1312       LoadIconW(COMDLG32_hInstance, PD32_PORTRAIT);
1313     PrintStructures->hLandscapeIcon =
1314       LoadIconW(COMDLG32_hInstance, PD32_LANDSCAPE);
1315
1316     /* display the collate/no_collate icon */
1317     SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1318                         (LPARAM)PrintStructures->hNoCollateIcon);
1319
1320     if(PrintStructures->hCollateIcon == 0 ||
1321        PrintStructures->hNoCollateIcon == 0 ||
1322        PrintStructures->hPortraitIcon == 0 ||
1323        PrintStructures->hLandscapeIcon == 0) {
1324         ERR("no icon in resourcefile\n");
1325         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1326         EndDialog(hDlg, FALSE);
1327     }
1328
1329     /*
1330      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1331      * must be registered and the Help button must be shown.
1332      */
1333     if (lppd->Flags & PD_SHOWHELP) {
1334         if((PrintStructures->HelpMessageID =
1335             RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1336             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1337             return FALSE;
1338         }
1339     } else
1340         PrintStructures->HelpMessageID = 0;
1341
1342     if(!(lppd->Flags &PD_PRINTSETUP)) {
1343         PrintStructures->hwndUpDown =
1344           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1345                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1346                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1347                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
1348                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1349     }
1350
1351     /* FIXME: I allow more freedom than either Win95 or WinNT,
1352      *        which do not agree to what errors should be thrown or not
1353      *        in case nToPage or nFromPage is out-of-range.
1354      */
1355     if (lppd->nMaxPage < lppd->nMinPage)
1356         lppd->nMaxPage = lppd->nMinPage;
1357     if (lppd->nMinPage == lppd->nMaxPage)
1358         lppd->Flags |= PD_NOPAGENUMS;
1359     if (lppd->nToPage < lppd->nMinPage)
1360         lppd->nToPage = lppd->nMinPage;
1361     if (lppd->nToPage > lppd->nMaxPage)
1362         lppd->nToPage = lppd->nMaxPage;
1363     if (lppd->nFromPage < lppd->nMinPage)
1364         lppd->nFromPage = lppd->nMinPage;
1365     if (lppd->nFromPage > lppd->nMaxPage)
1366         lppd->nFromPage = lppd->nMaxPage;
1367
1368     /* if we have the combo box, fill it */
1369     if (GetDlgItem(hDlg,comboID)) {
1370         /* Fill Combobox
1371          */
1372         pdn = GlobalLock(lppd->hDevNames);
1373         pdm = GlobalLock(lppd->hDevMode);
1374         if(pdn)
1375             name = (WCHAR*)pdn + pdn->wDeviceOffset;
1376         else if(pdm)
1377             name = pdm->dmDeviceName;
1378         PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1379         if(pdm) GlobalUnlock(lppd->hDevMode);
1380         if(pdn) GlobalUnlock(lppd->hDevNames);
1381
1382         /* Now find selected printer and update rest of dlg */
1383         /* ansi is ok here */
1384         name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1385         if (GetDlgItemTextW(hDlg, comboID, name, 255))
1386             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1387         HeapFree(GetProcessHeap(),0,name);
1388     } else {
1389         /* else use default printer */
1390         WCHAR name[200];
1391         DWORD dwBufLen = sizeof(name) / sizeof(WCHAR);
1392         BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1393
1394         if (ret)
1395             PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1396         else
1397             FIXME("No default printer found, expect problems!\n");
1398     }
1399     return TRUE;
1400 }
1401
1402 /***********************************************************************
1403  *                              PRINTDLG_WMCommand               [internal]
1404  */
1405 LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1406                         LPARAM lParam, PRINT_PTRA* PrintStructures)
1407 {
1408     LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1409     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1410     LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1411
1412     switch (LOWORD(wParam))  {
1413     case IDOK:
1414         TRACE(" OK button was hit\n");
1415         if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1416             FIXME("Update printdlg was not successful!\n");
1417             return(FALSE);
1418         }
1419         EndDialog(hDlg, TRUE);
1420         return(TRUE);
1421
1422     case IDCANCEL:
1423         TRACE(" CANCEL button was hit\n");
1424         EndDialog(hDlg, FALSE);
1425         return(FALSE);
1426
1427      case pshHelp:
1428         TRACE(" HELP button was hit\n");
1429         SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1430                                 (WPARAM) hDlg, (LPARAM) lppd);
1431         break;
1432
1433      case chx2:                         /* collate pages checkbox */
1434         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1435             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1436                                     (LPARAM)PrintStructures->hCollateIcon);
1437         else
1438             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1439                                     (LPARAM)PrintStructures->hNoCollateIcon);
1440         break;
1441      case edt1:                         /* from page nr editbox */
1442      case edt2:                         /* to page nr editbox */
1443         if (HIWORD(wParam)==EN_CHANGE) {
1444             WORD nToPage;
1445             WORD nFromPage;
1446             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1447             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1448             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1449                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1450         }
1451         break;
1452
1453     case edt3:
1454         if(HIWORD(wParam) == EN_CHANGE) {
1455             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1456             if(copies <= 1)
1457                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1458             else
1459                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1460         }
1461         break;
1462
1463 #if 0
1464      case psh1:                       /* Print Setup */
1465         {
1466             PRINTDLG16  pdlg;
1467
1468             if (!PrintStructures->dlg.lpPrintDlg16) {
1469                 FIXME("The 32bit print dialog does not have this button!?\n");
1470                 break;
1471             }
1472
1473             memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1474             pdlg.Flags |= PD_PRINTSETUP;
1475             pdlg.hwndOwner = HWND_16(hDlg);
1476             if (!PrintDlg16(&pdlg))
1477                 break;
1478         }
1479         break;
1480 #endif
1481      case psh2:                       /* Properties button */
1482        {
1483          HANDLE hPrinter;
1484          char   PrinterName[256];
1485
1486          GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1487          if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1488              FIXME(" Call to OpenPrinter did not succeed!\n");
1489              break;
1490          }
1491          DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1492                              PrintStructures->lpDevMode,
1493                              PrintStructures->lpDevMode,
1494                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1495          ClosePrinter(hPrinter);
1496          break;
1497        }
1498
1499     case rad1: /* Paperorientation */
1500         if (lppd->Flags & PD_PRINTSETUP)
1501         {
1502               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1503               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1504                           (LPARAM)(PrintStructures->hPortraitIcon));
1505         }
1506         break;
1507
1508     case rad2: /* Paperorientation */
1509         if (lppd->Flags & PD_PRINTSETUP)
1510         {
1511               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1512               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1513                           (LPARAM)(PrintStructures->hLandscapeIcon));
1514         }
1515         break;
1516
1517     case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT */
1518          if (PrinterComboID != LOWORD(wParam)) {
1519              FIXME("No handling for print quality combo box yet.\n");
1520              break;
1521          }
1522          /* FALLTHROUGH */
1523     case cmb4:                         /* Printer combobox */
1524          if (HIWORD(wParam)==CBN_SELCHANGE) {
1525              char   PrinterName[256];
1526              GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1527              PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1528          }
1529          break;
1530
1531     case cmb2: /* Papersize */
1532       {
1533           DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1534           if(Sel != CB_ERR)
1535               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1536                                                             CB_GETITEMDATA,
1537                                                             Sel, 0);
1538       }
1539       break;
1540
1541     case cmb3: /* Bin */
1542       {
1543           DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1544           if(Sel != CB_ERR)
1545               lpdm->dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1546                                                           CB_GETITEMDATA, Sel,
1547                                                           0);
1548       }
1549       break;
1550     }
1551     if(lppd->Flags & PD_PRINTSETUP) {
1552         switch (LOWORD(wParam)) {
1553         case rad1:                         /* orientation */
1554         case rad2:
1555             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1556                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1557                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1558                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1559                                         (WPARAM)IMAGE_ICON,
1560                                         (LPARAM)PrintStructures->hPortraitIcon);
1561                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1562                                         (WPARAM)IMAGE_ICON,
1563                                         (LPARAM)PrintStructures->hPortraitIcon);
1564                 }
1565             } else {
1566                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1567                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1568                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1569                                         (WPARAM)IMAGE_ICON,
1570                                         (LPARAM)PrintStructures->hLandscapeIcon);
1571                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1572                                         (WPARAM)IMAGE_ICON,
1573                                         (LPARAM)PrintStructures->hLandscapeIcon);
1574                 }
1575             }
1576             break;
1577         }
1578     }
1579     return FALSE;
1580 }
1581
1582 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1583                         LPARAM lParam, PRINT_PTRW* PrintStructures)
1584 {
1585     LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1586     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1587     LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1588
1589     switch (LOWORD(wParam))  {
1590     case IDOK:
1591         TRACE(" OK button was hit\n");
1592         if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1593             FIXME("Update printdlg was not successful!\n");
1594             return(FALSE);
1595         }
1596         EndDialog(hDlg, TRUE);
1597         return(TRUE);
1598
1599     case IDCANCEL:
1600         TRACE(" CANCEL button was hit\n");
1601         EndDialog(hDlg, FALSE);
1602         return(FALSE);
1603
1604      case pshHelp:
1605         TRACE(" HELP button was hit\n");
1606         SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1607                                 (WPARAM) hDlg, (LPARAM) lppd);
1608         break;
1609
1610      case chx2:                         /* collate pages checkbox */
1611         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1612             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1613                                     (LPARAM)PrintStructures->hCollateIcon);
1614         else
1615             SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1616                                     (LPARAM)PrintStructures->hNoCollateIcon);
1617         break;
1618      case edt1:                         /* from page nr editbox */
1619      case edt2:                         /* to page nr editbox */
1620         if (HIWORD(wParam)==EN_CHANGE) {
1621             WORD nToPage;
1622             WORD nFromPage;
1623             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1624             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1625             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1626                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1627         }
1628         break;
1629
1630     case edt3:
1631         if(HIWORD(wParam) == EN_CHANGE) {
1632             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1633             if(copies <= 1)
1634                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1635             else
1636                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1637         }
1638         break;
1639
1640      case psh1:                       /* Print Setup */
1641         {
1642                 ERR("psh1 is called from 16bit code only, we should not get here.\n");
1643         }
1644         break;
1645      case psh2:                       /* Properties button */
1646        {
1647          HANDLE hPrinter;
1648          WCHAR  PrinterName[256];
1649
1650          if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1651          if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1652              FIXME(" Call to OpenPrinter did not succeed!\n");
1653              break;
1654          }
1655          DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1656                              PrintStructures->lpDevMode,
1657                              PrintStructures->lpDevMode,
1658                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1659          ClosePrinter(hPrinter);
1660          break;
1661        }
1662
1663     case rad1: /* Paperorientation */
1664         if (lppd->Flags & PD_PRINTSETUP)
1665         {
1666               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1667               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1668                           (LPARAM)(PrintStructures->hPortraitIcon));
1669         }
1670         break;
1671
1672     case rad2: /* Paperorientation */
1673         if (lppd->Flags & PD_PRINTSETUP)
1674         {
1675               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1676               SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1677                           (LPARAM)(PrintStructures->hLandscapeIcon));
1678         }
1679         break;
1680
1681     case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT */
1682          if (PrinterComboID != LOWORD(wParam)) {
1683              FIXME("No handling for print quality combo box yet.\n");
1684              break;
1685          }
1686          /* FALLTHROUGH */
1687     case cmb4:                         /* Printer combobox */
1688          if (HIWORD(wParam)==CBN_SELCHANGE) {
1689              WCHAR   PrinterName[256];
1690              GetDlgItemTextW(hDlg, LOWORD(wParam), PrinterName, 255);
1691              PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1692          }
1693          break;
1694
1695     case cmb2: /* Papersize */
1696       {
1697           DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1698           if(Sel != CB_ERR)
1699               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1700                                                             CB_GETITEMDATA,
1701                                                             Sel, 0);
1702       }
1703       break;
1704
1705     case cmb3: /* Bin */
1706       {
1707           DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1708           if(Sel != CB_ERR)
1709               lpdm->dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1710                                                           CB_GETITEMDATA, Sel,
1711                                                           0);
1712       }
1713       break;
1714     }
1715     if(lppd->Flags & PD_PRINTSETUP) {
1716         switch (LOWORD(wParam)) {
1717         case rad1:                         /* orientation */
1718         case rad2:
1719             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1720                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1721                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1722                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1723                                         (WPARAM)IMAGE_ICON,
1724                                         (LPARAM)PrintStructures->hPortraitIcon);
1725                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1726                                         (WPARAM)IMAGE_ICON,
1727                                         (LPARAM)PrintStructures->hPortraitIcon);
1728                 }
1729             } else {
1730                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1731                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1732                     SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1733                                         (WPARAM)IMAGE_ICON,
1734                                         (LPARAM)PrintStructures->hLandscapeIcon);
1735                     SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1736                                         (WPARAM)IMAGE_ICON,
1737                                         (LPARAM)PrintStructures->hLandscapeIcon);
1738                 }
1739             }
1740             break;
1741         }
1742     }
1743     return FALSE;
1744 }
1745
1746 /***********************************************************************
1747  *           PrintDlgProcA                      [internal]
1748  */
1749 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1750                                       LPARAM lParam)
1751 {
1752     PRINT_PTRA* PrintStructures;
1753     INT_PTR res = FALSE;
1754
1755     if (uMsg!=WM_INITDIALOG) {
1756         PrintStructures = (PRINT_PTRA*)GetPropA(hDlg,"__WINE_PRINTDLGDATA");
1757         if (!PrintStructures)
1758             return FALSE;
1759     } else {
1760         PrintStructures = (PRINT_PTRA*) lParam;
1761         SetPropA(hDlg,"__WINE_PRINTDLGDATA",PrintStructures);
1762         res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1763
1764         if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1765             res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1766                 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1767             );
1768         return res;
1769     }
1770
1771     if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1772         res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1773                                                          lParam);
1774         if(res) return res;
1775     }
1776
1777     switch (uMsg) {
1778     case WM_COMMAND:
1779         return PRINTDLG_WMCommandA(hDlg, wParam, lParam, PrintStructures);
1780
1781     case WM_DESTROY:
1782         DestroyIcon(PrintStructures->hCollateIcon);
1783         DestroyIcon(PrintStructures->hNoCollateIcon);
1784         DestroyIcon(PrintStructures->hPortraitIcon);
1785         DestroyIcon(PrintStructures->hLandscapeIcon);
1786         if(PrintStructures->hwndUpDown)
1787             DestroyWindow(PrintStructures->hwndUpDown);
1788         return FALSE;
1789     }
1790     return res;
1791 }
1792
1793 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
1794                                       LPARAM lParam)
1795 {
1796     static const WCHAR propW[] = {'_','_','W','I','N','E','_','P','R','I','N','T','D','L','G','D','A','T','A',0};
1797     PRINT_PTRW* PrintStructures;
1798     INT_PTR res = FALSE;
1799
1800     if (uMsg!=WM_INITDIALOG) {
1801         PrintStructures = (PRINT_PTRW*) GetPropW(hDlg, propW);
1802         if (!PrintStructures)
1803             return FALSE;
1804     } else {
1805         PrintStructures = (PRINT_PTRW*) lParam;
1806         SetPropW(hDlg, propW, PrintStructures);
1807         res = PRINTDLG_WMInitDialogW(hDlg, wParam, PrintStructures);
1808
1809         if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1810             res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
1811         return res;
1812     }
1813
1814     if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1815         res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
1816         if(res) return res;
1817     }
1818
1819     switch (uMsg) {
1820     case WM_COMMAND:
1821         return PRINTDLG_WMCommandW(hDlg, wParam, lParam, PrintStructures);
1822
1823     case WM_DESTROY:
1824         DestroyIcon(PrintStructures->hCollateIcon);
1825         DestroyIcon(PrintStructures->hNoCollateIcon);
1826         DestroyIcon(PrintStructures->hPortraitIcon);
1827         DestroyIcon(PrintStructures->hLandscapeIcon);
1828         if(PrintStructures->hwndUpDown)
1829             DestroyWindow(PrintStructures->hwndUpDown);
1830         return FALSE;
1831     }
1832     return res;
1833 }
1834
1835 /************************************************************
1836  *
1837  *      PRINTDLG_GetDlgTemplate
1838  *
1839  */
1840 static HGLOBAL PRINTDLG_GetDlgTemplateA(PRINTDLGA *lppd)
1841 {
1842     HRSRC hResInfo;
1843     HGLOBAL hDlgTmpl;
1844
1845     if (lppd->Flags & PD_PRINTSETUP) {
1846         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1847             hDlgTmpl = lppd->hSetupTemplate;
1848         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1849             hResInfo = FindResourceA(lppd->hInstance,
1850                                      lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
1851             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1852         } else {
1853             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1854                                      (LPSTR)RT_DIALOG);
1855             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1856         }
1857     } else {
1858         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1859             hDlgTmpl = lppd->hPrintTemplate;
1860         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1861             hResInfo = FindResourceA(lppd->hInstance,
1862                                      lppd->lpPrintTemplateName,
1863                                      (LPSTR)RT_DIALOG);
1864             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1865         } else {
1866             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1867                                      (LPSTR)RT_DIALOG);
1868             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1869         }
1870     }
1871     return hDlgTmpl;
1872 }
1873
1874 static HGLOBAL PRINTDLG_GetDlgTemplateW(PRINTDLGW *lppd)
1875 {
1876     HRSRC hResInfo;
1877     HGLOBAL hDlgTmpl;
1878     static const WCHAR xpsetup[] = { 'P','R','I','N','T','3','2','_','S','E','T','U','P',0};
1879     static const WCHAR xprint[] = { 'P','R','I','N','T','3','2',0};
1880
1881     if (lppd->Flags & PD_PRINTSETUP) {
1882         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1883             hDlgTmpl = lppd->hSetupTemplate;
1884         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1885             hResInfo = FindResourceW(lppd->hInstance,
1886                                      lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
1887             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1888         } else {
1889             hResInfo = FindResourceW(COMDLG32_hInstance, xpsetup, (LPWSTR)RT_DIALOG);
1890             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1891         }
1892     } else {
1893         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1894             hDlgTmpl = lppd->hPrintTemplate;
1895         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1896             hResInfo = FindResourceW(lppd->hInstance,
1897                                      lppd->lpPrintTemplateName,
1898                                      (LPWSTR)RT_DIALOG);
1899             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1900         } else {
1901             hResInfo = FindResourceW(COMDLG32_hInstance, xprint, (LPWSTR)RT_DIALOG);
1902             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1903         }
1904     }
1905     return hDlgTmpl;
1906 }
1907
1908 /***********************************************************************
1909  *
1910  *      PRINTDLG_CreateDC
1911  *
1912  */
1913 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
1914 {
1915     DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1916     DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1917
1918     if(lppd->Flags & PD_RETURNDC) {
1919         lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1920                               (char*)pdn + pdn->wDeviceOffset,
1921                               (char*)pdn + pdn->wOutputOffset,
1922                               pdm );
1923     } else if(lppd->Flags & PD_RETURNIC) {
1924         lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1925                               (char*)pdn + pdn->wDeviceOffset,
1926                               (char*)pdn + pdn->wOutputOffset,
1927                               pdm );
1928     }
1929     GlobalUnlock(lppd->hDevNames);
1930     GlobalUnlock(lppd->hDevMode);
1931     return lppd->hDC ? TRUE : FALSE;
1932 }
1933
1934 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
1935 {
1936     DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1937     DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
1938
1939     if(lppd->Flags & PD_RETURNDC) {
1940         lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
1941                               (WCHAR*)pdn + pdn->wDeviceOffset,
1942                               (WCHAR*)pdn + pdn->wOutputOffset,
1943                               pdm );
1944     } else if(lppd->Flags & PD_RETURNIC) {
1945         lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
1946                               (WCHAR*)pdn + pdn->wDeviceOffset,
1947                               (WCHAR*)pdn + pdn->wOutputOffset,
1948                               pdm );
1949     }
1950     GlobalUnlock(lppd->hDevNames);
1951     GlobalUnlock(lppd->hDevMode);
1952     return lppd->hDC ? TRUE : FALSE;
1953 }
1954
1955 /***********************************************************************
1956  *           PrintDlgA   (COMDLG32.@)
1957  *
1958  *  Displays the the PRINT dialog box, which enables the user to specify
1959  *  specific properties of the print job.
1960  *
1961  * RETURNS
1962  *  nonzero if the user pressed the OK button
1963  *  zero    if the user cancelled the window or an error occurred
1964  *
1965  * BUGS
1966  *  PrintDlg:
1967  *  * The Collate Icons do not display, even though they are in the code.
1968  *  * The Properties Button(s) should call DocumentPropertiesA().
1969  *  PrintSetupDlg:
1970  *  * The Paper Orientation Icons are not implemented yet.
1971  *  * The Properties Button(s) should call DocumentPropertiesA().
1972  *  * Settings are not yet taken from a provided DevMode or
1973  *    default printer settings.
1974  */
1975
1976 BOOL WINAPI PrintDlgA(
1977                       LPPRINTDLGA lppd /* [in/out] ptr to PRINTDLG32 struct */
1978                       )
1979 {
1980     BOOL      bRet = FALSE;
1981     LPVOID   ptr;
1982     HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrA( lppd->hwndOwner, GWLP_HINSTANCE );
1983
1984     if(TRACE_ON(commdlg)) {
1985         char flagstr[1000] = "";
1986         struct pd_flags *pflag = pd_flags;
1987         for( ; pflag->name; pflag++) {
1988             if(lppd->Flags & pflag->flag)
1989                 strcat(flagstr, pflag->name);
1990         }
1991         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
1992               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
1993               "flags %08lx (%s)\n",
1994               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1995               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1996               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1997     }
1998
1999     if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2000         WARN("structure size failure !!!\n");
2001         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2002         return FALSE;
2003     }
2004
2005     if(lppd->Flags & PD_RETURNDEFAULT) {
2006         PRINTER_INFO_2A *pbuf;
2007         DRIVER_INFO_3A  *dbuf;
2008         HANDLE hprn;
2009         DWORD needed;
2010
2011         if(lppd->hDevMode || lppd->hDevNames) {
2012             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2013             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2014             return FALSE;
2015         }
2016         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2017             WARN("Can't find default printer\n");
2018             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2019             return FALSE;
2020         }
2021
2022         GetPrinterA(hprn, 2, NULL, 0, &needed);
2023         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2024         GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2025
2026         GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2027         dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2028         if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2029             ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",GetLastError(),pbuf->pPrinterName);
2030             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2031             return FALSE;
2032         }
2033         ClosePrinter(hprn);
2034
2035         PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2036                                   dbuf->pDriverPath,
2037                                   pbuf->pPrinterName,
2038                                   pbuf->pPortName);
2039         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2040                                      pbuf->pDevMode->dmDriverExtra);
2041         ptr = GlobalLock(lppd->hDevMode);
2042         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2043                pbuf->pDevMode->dmDriverExtra);
2044         GlobalUnlock(lppd->hDevMode);
2045         HeapFree(GetProcessHeap(), 0, pbuf);
2046         HeapFree(GetProcessHeap(), 0, dbuf);
2047         bRet = TRUE;
2048     } else {
2049         HGLOBAL hDlgTmpl;
2050         PRINT_PTRA *PrintStructures;
2051
2052     /* load Dialog resources,
2053      * depending on Flags indicates Print32 or Print32_setup dialog
2054      */
2055         hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2056         if (!hDlgTmpl) {
2057             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2058             return FALSE;
2059         }
2060         ptr = LockResource( hDlgTmpl );
2061         if (!ptr) {
2062             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2063             return FALSE;
2064         }
2065
2066         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2067                                     sizeof(PRINT_PTRA));
2068         PrintStructures->lpPrintDlg = lppd;
2069
2070         /* and create & process the dialog .
2071          * -1 is failure, 0 is broken hwnd, everything else is ok.
2072          */
2073         bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2074                                            PrintDlgProcA,
2075                                            (LPARAM)PrintStructures));
2076
2077         if(bRet) {
2078             DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2079             PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2080             DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2081
2082             if (lppd->hDevMode == 0) {
2083                 TRACE(" No hDevMode yet... Need to create my own\n");
2084                 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2085                                         lpdm->dmSize + lpdm->dmDriverExtra);
2086             } else {
2087                 WORD locks;
2088                 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2089                     WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2090                     while(locks--) {
2091                         GlobalUnlock(lppd->hDevMode);
2092                         TRACE("Now got %d locks\n", locks);
2093                     }
2094                 }
2095                 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2096                                                lpdm->dmSize + lpdm->dmDriverExtra,
2097                                                GMEM_MOVEABLE);
2098             }
2099             lpdmReturn = GlobalLock(lppd->hDevMode);
2100             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2101
2102             if (lppd->hDevNames != 0) {
2103                 WORD locks;
2104                 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2105                     WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2106                     while(locks--)
2107                         GlobalUnlock(lppd->hDevNames);
2108                 }
2109             }
2110             PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2111                     di->pDriverPath,
2112                     pi->pPrinterName,
2113                     pi->pPortName
2114             );
2115             GlobalUnlock(lppd->hDevMode);
2116         }
2117         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2118         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2119         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2120         HeapFree(GetProcessHeap(), 0, PrintStructures);
2121     }
2122     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2123         bRet = PRINTDLG_CreateDCA(lppd);
2124
2125     TRACE("exit! (%d)\n", bRet);
2126     return bRet;
2127 }
2128
2129 /***********************************************************************
2130  *           PrintDlgW   (COMDLG32.@)
2131  */
2132 BOOL WINAPI PrintDlgW(
2133                       LPPRINTDLGW lppd /* [in/out] ptr to PRINTDLG32 struct */
2134                       )
2135 {
2136     BOOL      bRet = FALSE;
2137     LPVOID   ptr;
2138     HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW( lppd->hwndOwner, GWLP_HINSTANCE );
2139
2140     if(TRACE_ON(commdlg)) {
2141         char flagstr[1000] = "";
2142         struct pd_flags *pflag = pd_flags;
2143         for( ; pflag->name; pflag++) {
2144             if(lppd->Flags & pflag->flag)
2145                 strcat(flagstr, pflag->name);
2146         }
2147         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2148               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2149               "flags %08lx (%s)\n",
2150               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2151               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2152               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2153     }
2154
2155     if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2156         WARN("structure size failure !!!\n");
2157         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2158         return FALSE;
2159     }
2160
2161     if(lppd->Flags & PD_RETURNDEFAULT) {
2162         PRINTER_INFO_2W *pbuf;
2163         DRIVER_INFO_3W  *dbuf;
2164         HANDLE hprn;
2165         DWORD needed;
2166
2167         if(lppd->hDevMode || lppd->hDevNames) {
2168             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2169             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2170             return FALSE;
2171         }
2172         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2173             WARN("Can't find default printer\n");
2174             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2175             return FALSE;
2176         }
2177
2178         GetPrinterW(hprn, 2, NULL, 0, &needed);
2179         pbuf = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*needed);
2180         GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2181
2182         GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2183         dbuf = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*needed);
2184         if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2185             ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",GetLastError(),debugstr_w(pbuf->pPrinterName));
2186             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2187             return FALSE;
2188         }
2189         ClosePrinter(hprn);
2190
2191         PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2192                                   dbuf->pDriverPath,
2193                                   pbuf->pPrinterName,
2194                                   pbuf->pPortName);
2195         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2196                                      pbuf->pDevMode->dmDriverExtra);
2197         ptr = GlobalLock(lppd->hDevMode);
2198         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2199                pbuf->pDevMode->dmDriverExtra);
2200         GlobalUnlock(lppd->hDevMode);
2201         HeapFree(GetProcessHeap(), 0, pbuf);
2202         HeapFree(GetProcessHeap(), 0, dbuf);
2203         bRet = TRUE;
2204     } else {
2205         HGLOBAL hDlgTmpl;
2206         PRINT_PTRW *PrintStructures;
2207
2208     /* load Dialog resources,
2209      * depending on Flags indicates Print32 or Print32_setup dialog
2210      */
2211         hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2212         if (!hDlgTmpl) {
2213             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2214             return FALSE;
2215         }
2216         ptr = LockResource( hDlgTmpl );
2217         if (!ptr) {
2218             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2219             return FALSE;
2220         }
2221
2222         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2223                                     sizeof(PRINT_PTRW));
2224         PrintStructures->lpPrintDlg = lppd;
2225
2226         /* and create & process the dialog .
2227          * -1 is failure, 0 is broken hwnd, everything else is ok.
2228          */
2229         bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2230                                            PrintDlgProcW,
2231                                            (LPARAM)PrintStructures));
2232
2233         if(bRet) {
2234             DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2235             PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2236             DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2237
2238             if (lppd->hDevMode == 0) {
2239                 TRACE(" No hDevMode yet... Need to create my own\n");
2240                 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2241                                         lpdm->dmSize + lpdm->dmDriverExtra);
2242             } else {
2243                 WORD locks;
2244                 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2245                     WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2246                     while(locks--) {
2247                         GlobalUnlock(lppd->hDevMode);
2248                         TRACE("Now got %d locks\n", locks);
2249                     }
2250                 }
2251                 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2252                                                lpdm->dmSize + lpdm->dmDriverExtra,
2253                                                GMEM_MOVEABLE);
2254             }
2255             lpdmReturn = GlobalLock(lppd->hDevMode);
2256             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2257
2258             if (lppd->hDevNames != 0) {
2259                 WORD locks;
2260                 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2261                     WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2262                     while(locks--)
2263                         GlobalUnlock(lppd->hDevNames);
2264                 }
2265             }
2266             PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2267                     di->pDriverPath,
2268                     pi->pPrinterName,
2269                     pi->pPortName
2270             );
2271             GlobalUnlock(lppd->hDevMode);
2272         }
2273         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2274         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2275         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2276         HeapFree(GetProcessHeap(), 0, PrintStructures);
2277     }
2278     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2279         bRet = PRINTDLG_CreateDCW(lppd);
2280
2281     TRACE("exit! (%d)\n", bRet);
2282     return bRet;
2283 }
2284
2285 /***********************************************************************
2286  *
2287  *          PageSetupDlg
2288  * rad1 - portrait
2289  * rad2 - landscape
2290  * cmb2 - paper size
2291  * cmb3 - source (tray?)
2292  * edt4 - border left
2293  * edt5 - border top
2294  * edt6 - border right
2295  * edt7 - border bottom
2296  * psh3 - "Printer..."
2297  */
2298
2299 typedef struct {
2300     LPPAGESETUPDLGA     dlga;
2301     PRINTDLGA           pdlg;
2302 } PageSetupDataA;
2303
2304 typedef struct {
2305     LPPAGESETUPDLGW     dlga;
2306     PRINTDLGW           pdlg;
2307 } PageSetupDataW;
2308
2309 static HGLOBAL PRINTDLG_GetPGSTemplateA(PAGESETUPDLGA *lppd)
2310 {
2311     HRSRC hResInfo;
2312     HGLOBAL hDlgTmpl;
2313
2314     if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATEHANDLE) {
2315         hDlgTmpl = lppd->hPageSetupTemplate;
2316     } else if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATE) {
2317         hResInfo = FindResourceA(lppd->hInstance,
2318                                  lppd->lpPageSetupTemplateName, (LPSTR)RT_DIALOG);
2319         hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2320     } else {
2321         hResInfo = FindResourceA(COMDLG32_hInstance,(LPCSTR)PAGESETUPDLGORD,(LPSTR)RT_DIALOG);
2322         hDlgTmpl = LoadResource(COMDLG32_hInstance,hResInfo);
2323     }
2324     return hDlgTmpl;
2325 }
2326
2327 static HGLOBAL PRINTDLG_GetPGSTemplateW(PAGESETUPDLGW *lppd)
2328 {
2329     HRSRC hResInfo;
2330     HGLOBAL hDlgTmpl;
2331
2332     if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATEHANDLE) {
2333         hDlgTmpl = lppd->hPageSetupTemplate;
2334     } else if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATE) {
2335         hResInfo = FindResourceW(lppd->hInstance,
2336                                  lppd->lpPageSetupTemplateName, (LPWSTR)RT_DIALOG);
2337         hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2338     } else {
2339         hResInfo = FindResourceW(COMDLG32_hInstance,(LPCWSTR)PAGESETUPDLGORD,(LPWSTR)RT_DIALOG);
2340         hDlgTmpl = LoadResource(COMDLG32_hInstance,hResInfo);
2341     }
2342     return hDlgTmpl;
2343 }
2344
2345 static DWORD
2346 _c_10mm2size(PAGESETUPDLGA *dlga,DWORD size) {
2347     if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
2348         return 10*size*10/25.4;
2349     /* If we don't have a flag, we can choose one. Use millimeters
2350      * to avoid confusing me
2351      */
2352     dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
2353     return 10*size;
2354 }
2355
2356
2357 static DWORD
2358 _c_inch2size(PAGESETUPDLGA *dlga,DWORD size) {
2359     if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
2360         return size;
2361     if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
2362         return (size*254)/10;
2363     /* if we don't have a flag, we can choose one. Use millimeters
2364      * to avoid confusing me
2365      */
2366     dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
2367     return (size*254)/10;
2368 }
2369
2370 static void
2371 _c_size2strA(PageSetupDataA *pda,DWORD size,LPSTR strout) {
2372     strcpy(strout,"<undef>");
2373     if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
2374         sprintf(strout,"%.2fmm",(size*1.0)/100.0);
2375         return;
2376     }
2377     if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
2378         sprintf(strout,"%.2fin",(size*1.0)/1000.0);
2379         return;
2380     }
2381     pda->dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
2382     sprintf(strout,"%.2fmm",(size*1.0)/100.0);
2383     return;
2384 }
2385 static void
2386 _c_size2strW(PageSetupDataW *pda,DWORD size,LPWSTR strout) {
2387     const static WCHAR UNDEF[] = { '<', 'u', 'n', 'd', 'e', 'f', '>', 0 };
2388     const static WCHAR mm_fmt[] = { '%', '.', '2', 'f', 'm', 'm', 0 };
2389     const static WCHAR in_fmt[] = { '%', '.', '2', 'f', 'i', 'n', 0 };
2390     lstrcpyW(strout, UNDEF);
2391     if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
2392         wsprintfW(strout,mm_fmt,(size*1.0)/100.0);
2393         return;
2394     }
2395     if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
2396         wsprintfW(strout,in_fmt,(size*1.0)/1000.0);
2397         return;
2398     }
2399     pda->dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
2400     wsprintfW(strout,mm_fmt,(size*1.0)/100.0);
2401     return;
2402 }
2403
2404 static DWORD
2405 _c_str2sizeA(PAGESETUPDLGA *dlga,LPCSTR strin) {
2406     float       val;
2407     char        rest[200];
2408
2409     rest[0]='\0';
2410     if (!sscanf(strin,"%f%s",&val,rest))
2411         return 0;
2412
2413     if (!strcmp(rest,"in") || !strcmp(rest,"inch")) {
2414         if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
2415             return 1000*val;
2416         else
2417             return val*25.4*100;
2418     }
2419     if (!strcmp(rest,"cm")) { rest[0]='m'; val = val*10.0; }
2420     if (!strcmp(rest,"m")) { strcpy(rest,"mm"); val = val*1000.0; }
2421
2422     if (!strcmp(rest,"mm")) {
2423         if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
2424             return 100*val;
2425         else
2426             return 1000.0*val/25.4;
2427     }
2428     if (rest[0]=='\0') {
2429         /* use application supplied default */
2430         if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
2431             /* 100*mm */
2432             return 100.0*val;
2433         }
2434         if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
2435             /* 1000*inch */
2436             return 1000.0*val;
2437         }
2438     }
2439     ERR("Did not find a conversion for type '%s'!\n",rest);
2440     return 0;
2441 }
2442
2443
2444 static DWORD
2445 _c_str2sizeW(PAGESETUPDLGW *dlga, LPCWSTR strin) {
2446     char        buf[200];
2447
2448     /* this W -> A transition is OK */
2449     /* we need a unicode version of sscanf to avoid it */
2450     WideCharToMultiByte(CP_ACP, 0, strin, -1, buf, sizeof(buf), NULL, NULL);
2451     return _c_str2sizeA((PAGESETUPDLGA *)dlga, buf);
2452 }
2453
2454
2455 /*
2456  * This is called on finish and will update the output fields of the
2457  * struct.
2458  */
2459 static BOOL
2460 PRINTDLG_PS_UpdateDlgStructA(HWND hDlg, PageSetupDataA *pda) {
2461     DEVNAMES    *dn;
2462     DEVMODEA    *dm;
2463     LPSTR       devname,portname;
2464     char        papername[64];
2465     char        buf[200];
2466
2467     dn = GlobalLock(pda->pdlg.hDevNames);
2468     dm = GlobalLock(pda->pdlg.hDevMode);
2469     devname     = ((char*)dn)+dn->wDeviceOffset;
2470     portname    = ((char*)dn)+dn->wOutputOffset;
2471     PRINTDLG_SetUpPaperComboBoxA(hDlg,cmb2,devname,portname,dm);
2472     PRINTDLG_SetUpPaperComboBoxA(hDlg,cmb3,devname,portname,dm);
2473
2474     if (GetDlgItemTextA(hDlg,cmb2,papername,sizeof(papername))>0) {
2475         PRINTDLG_PaperSizeA(&(pda->pdlg),papername,&(pda->dlga->ptPaperSize));
2476         pda->dlga->ptPaperSize.x = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.x);
2477         pda->dlga->ptPaperSize.y = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.y);
2478     } else
2479         FIXME("could not get dialog text for papersize cmbbox?\n");
2480 #define GETVAL(id,val) if (GetDlgItemTextA(hDlg,id,buf,sizeof(buf))>0) { val = _c_str2sizeA(pda->dlga,buf); } else { FIXME("could not get dlgitemtexta for %x\n",id); }
2481     GETVAL(edt4,pda->dlga->rtMargin.left);
2482     GETVAL(edt5,pda->dlga->rtMargin.top);
2483     GETVAL(edt6,pda->dlga->rtMargin.right);
2484     GETVAL(edt7,pda->dlga->rtMargin.bottom);
2485 #undef GETVAL
2486
2487     /* If we are in landscape, swap x and y of page size */
2488     if (IsDlgButtonChecked(hDlg, rad2)) {
2489         DWORD tmp;
2490         tmp = pda->dlga->ptPaperSize.x;
2491         pda->dlga->ptPaperSize.x = pda->dlga->ptPaperSize.y;
2492         pda->dlga->ptPaperSize.y = tmp;
2493     }
2494     GlobalUnlock(pda->pdlg.hDevNames);
2495     GlobalUnlock(pda->pdlg.hDevMode);
2496     return TRUE;
2497 }
2498
2499 static BOOL
2500 PRINTDLG_PS_UpdateDlgStructW(HWND hDlg, PageSetupDataW *pda) {
2501     DEVNAMES    *dn;
2502     DEVMODEW    *dm;
2503     LPWSTR      devname,portname;
2504     WCHAR       papername[64];
2505     WCHAR       buf[200];
2506
2507     dn = GlobalLock(pda->pdlg.hDevNames);
2508     dm = GlobalLock(pda->pdlg.hDevMode);
2509     devname     = ((WCHAR*)dn)+dn->wDeviceOffset;
2510     portname    = ((WCHAR*)dn)+dn->wOutputOffset;
2511     PRINTDLG_SetUpPaperComboBoxW(hDlg,cmb2,devname,portname,dm);
2512     PRINTDLG_SetUpPaperComboBoxW(hDlg,cmb3,devname,portname,dm);
2513
2514     if (GetDlgItemTextW(hDlg,cmb2,papername,sizeof(papername))>0) {
2515         PRINTDLG_PaperSizeW(&(pda->pdlg),papername,&(pda->dlga->ptPaperSize));
2516         pda->dlga->ptPaperSize.x = _c_10mm2size((LPPAGESETUPDLGA)pda->dlga,pda->dlga->ptPaperSize.x);
2517         pda->dlga->ptPaperSize.y = _c_10mm2size((LPPAGESETUPDLGA)pda->dlga,pda->dlga->ptPaperSize.y);
2518     } else
2519         FIXME("could not get dialog text for papersize cmbbox?\n");
2520 #define GETVAL(id,val) if (GetDlgItemTextW(hDlg,id,buf,sizeof(buf)/sizeof(buf[0]))>0) { val = _c_str2sizeW(pda->dlga,buf); } else { FIXME("could not get dlgitemtextw for %x\n",id); }
2521     GETVAL(edt4,pda->dlga->rtMargin.left);
2522     GETVAL(edt5,pda->dlga->rtMargin.top);
2523     GETVAL(edt6,pda->dlga->rtMargin.right);
2524     GETVAL(edt7,pda->dlga->rtMargin.bottom);
2525 #undef GETVAL
2526
2527     /* If we are in landscape, swap x and y of page size */
2528     if (IsDlgButtonChecked(hDlg, rad2)) {
2529         DWORD tmp;
2530         tmp = pda->dlga->ptPaperSize.x;
2531         pda->dlga->ptPaperSize.x = pda->dlga->ptPaperSize.y;
2532         pda->dlga->ptPaperSize.y = tmp;
2533     }
2534     GlobalUnlock(pda->pdlg.hDevNames);
2535     GlobalUnlock(pda->pdlg.hDevMode);
2536     return TRUE;
2537 }
2538
2539 /*
2540  * This is called after returning from PrintDlg().
2541  */
2542 static BOOL
2543 PRINTDLG_PS_ChangePrinterA(HWND hDlg, PageSetupDataA *pda) {
2544     DEVNAMES    *dn;
2545     DEVMODEA    *dm;
2546     LPSTR       devname,portname;
2547
2548     dn = GlobalLock(pda->pdlg.hDevNames);
2549     dm = GlobalLock(pda->pdlg.hDevMode);
2550     devname     = ((char*)dn)+dn->wDeviceOffset;
2551     portname    = ((char*)dn)+dn->wOutputOffset;
2552     PRINTDLG_SetUpPaperComboBoxA(hDlg,cmb2,devname,portname,dm);
2553     PRINTDLG_SetUpPaperComboBoxA(hDlg,cmb3,devname,portname,dm);
2554     GlobalUnlock(pda->pdlg.hDevNames);
2555     GlobalUnlock(pda->pdlg.hDevMode);
2556     return TRUE;
2557 }
2558
2559 static BOOL
2560 PRINTDLG_PS_ChangePrinterW(HWND hDlg, PageSetupDataW *pda) {
2561     DEVNAMES    *dn;
2562     DEVMODEW    *dm;
2563     LPWSTR      devname,portname;
2564
2565     dn = GlobalLock(pda->pdlg.hDevNames);
2566     dm = GlobalLock(pda->pdlg.hDevMode);
2567     devname     = ((WCHAR*)dn)+dn->wDeviceOffset;
2568     portname    = ((WCHAR*)dn)+dn->wOutputOffset;
2569     PRINTDLG_SetUpPaperComboBoxW(hDlg,cmb2,devname,portname,dm);
2570     PRINTDLG_SetUpPaperComboBoxW(hDlg,cmb3,devname,portname,dm);
2571     GlobalUnlock(pda->pdlg.hDevNames);
2572     GlobalUnlock(pda->pdlg.hDevMode);
2573     return TRUE;
2574 }
2575
2576 static BOOL
2577 PRINTDLG_PS_WMCommandA(
2578     HWND hDlg, WPARAM wParam, LPARAM lParam, PageSetupDataA *pda
2579 ) {
2580     switch (LOWORD(wParam))  {
2581     case IDOK:
2582         if (!PRINTDLG_PS_UpdateDlgStructA(hDlg, pda))
2583             return(FALSE);
2584         EndDialog(hDlg, TRUE);
2585         return TRUE ;
2586
2587     case IDCANCEL:
2588         EndDialog(hDlg, FALSE);
2589         return FALSE ;
2590
2591     case psh3: {
2592         pda->pdlg.Flags         = 0;
2593         pda->pdlg.hwndOwner     = hDlg;
2594         if (PrintDlgA(&(pda->pdlg)))
2595             PRINTDLG_PS_ChangePrinterA(hDlg,pda);
2596         return TRUE;
2597     }
2598     }
2599     FIXME("loword (lparam) %d, wparam 0x%x, lparam %08lx, STUB mostly.\n",
2600             LOWORD(lParam),wParam,lParam
2601     );
2602     return FALSE;
2603 }
2604
2605 static BOOL
2606 PRINTDLG_PS_WMCommandW(
2607     HWND hDlg, WPARAM wParam, LPARAM lParam, PageSetupDataW *pda
2608 ) {
2609     switch (LOWORD(wParam))  {
2610     case IDOK:
2611         if (!PRINTDLG_PS_UpdateDlgStructW(hDlg, pda))
2612             return(FALSE);
2613         EndDialog(hDlg, TRUE);
2614         return TRUE ;
2615
2616     case IDCANCEL:
2617         EndDialog(hDlg, FALSE);
2618         return FALSE ;
2619
2620     case psh3: {
2621         pda->pdlg.Flags         = 0;
2622         pda->pdlg.hwndOwner     = hDlg;
2623         if (PrintDlgW(&(pda->pdlg)))
2624             PRINTDLG_PS_ChangePrinterW(hDlg,pda);
2625         return TRUE;
2626     }
2627     }
2628     FIXME("loword (lparam) %d, wparam 0x%x, lparam %08lx, STUB mostly.\n",
2629             LOWORD(lParam),wParam,lParam
2630     );
2631     return FALSE;
2632 }
2633
2634
2635 static INT_PTR CALLBACK
2636 PageDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
2637 {
2638     PageSetupDataA      *pda;
2639     INT_PTR             res = FALSE;
2640
2641     if (uMsg==WM_INITDIALOG) {
2642         res = TRUE;
2643         pda = (PageSetupDataA*)lParam;
2644         SetPropA(hDlg,"__WINE_PAGESETUPDLGDATA",pda);
2645         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2646             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,(LPARAM)pda->dlga);
2647             if (!res) {
2648                 FIXME("Setup page hook failed?\n");
2649                 res = TRUE;
2650             }
2651         }
2652         if (pda->dlga->Flags & PSD_ENABLEPAGEPAINTHOOK) {
2653             FIXME("PagePaintHook not yet implemented!\n");
2654         }
2655         if (pda->dlga->Flags & PSD_DISABLEPRINTER)
2656             EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
2657         if (pda->dlga->Flags & PSD_DISABLEMARGINS) {
2658             EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
2659             EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
2660             EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
2661             EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
2662         }
2663         /* width larger as height -> landscape */
2664         if (pda->dlga->ptPaperSize.x > pda->dlga->ptPaperSize.y)
2665             CheckRadioButton(hDlg, rad1, rad2, rad2);
2666         else /* this is default if papersize is not set */
2667             CheckRadioButton(hDlg, rad1, rad2, rad1);
2668         if (pda->dlga->Flags & PSD_DISABLEORIENTATION) {
2669             EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
2670             EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
2671         }
2672         /* We fill them out enabled or not */
2673         if (pda->dlga->Flags & PSD_MARGINS) {
2674             char str[100];
2675             _c_size2strA(pda,pda->dlga->rtMargin.left,str);
2676             SetDlgItemTextA(hDlg,edt4,str);
2677             _c_size2strA(pda,pda->dlga->rtMargin.top,str);
2678             SetDlgItemTextA(hDlg,edt5,str);
2679             _c_size2strA(pda,pda->dlga->rtMargin.right,str);
2680             SetDlgItemTextA(hDlg,edt6,str);
2681             _c_size2strA(pda,pda->dlga->rtMargin.bottom,str);
2682             SetDlgItemTextA(hDlg,edt7,str);
2683         } else {
2684             /* default is 1 inch */
2685             DWORD size = _c_inch2size(pda->dlga,1000);
2686             char        str[20];
2687             _c_size2strA(pda,size,str);
2688             SetDlgItemTextA(hDlg,edt4,str);
2689             SetDlgItemTextA(hDlg,edt5,str);
2690             SetDlgItemTextA(hDlg,edt6,str);
2691             SetDlgItemTextA(hDlg,edt7,str);
2692         }
2693         PRINTDLG_PS_ChangePrinterA(hDlg,pda);
2694         if (pda->dlga->Flags & PSD_DISABLEPAPER) {
2695             EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
2696             EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
2697         }
2698         return TRUE;
2699     } else {
2700         pda = (PageSetupDataA*)GetPropA(hDlg,"__WINE_PAGESETUPDLGDATA");
2701         if (!pda) {
2702             WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
2703             return FALSE;
2704         }
2705         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2706             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
2707             if (res) return res;
2708         }
2709     }
2710     switch (uMsg) {
2711     case WM_COMMAND:
2712         return PRINTDLG_PS_WMCommandA(hDlg, wParam, lParam, pda);
2713     }
2714     return FALSE;
2715 }
2716
2717 static INT_PTR CALLBACK
2718 PageDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
2719 {
2720     const static WCHAR __WINE_PAGESETUPDLGDATA[] = 
2721         { '_', '_', 'W', 'I', 'N', 'E', '_', 'P', 'A', 'G', 'E', 
2722           'S', 'E', 'T', 'U', 'P', 'D', 'L', 'G', 'D', 'A', 'T', 'A', 0 };
2723     PageSetupDataW      *pda;
2724     BOOL                res = FALSE;
2725
2726     if (uMsg==WM_INITDIALOG) {
2727         res = TRUE;
2728         pda = (PageSetupDataW*)lParam;
2729         SetPropW(hDlg, __WINE_PAGESETUPDLGDATA, pda);
2730         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2731             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,(LPARAM)pda->dlga);
2732             if (!res) {
2733                 FIXME("Setup page hook failed?\n");
2734                 res = TRUE;
2735             }
2736         }
2737         if (pda->dlga->Flags & PSD_ENABLEPAGEPAINTHOOK) {
2738             FIXME("PagePaintHook not yet implemented!\n");
2739         }
2740         if (pda->dlga->Flags & PSD_DISABLEPRINTER)
2741             EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
2742         if (pda->dlga->Flags & PSD_DISABLEMARGINS) {
2743             EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
2744             EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
2745             EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
2746             EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
2747         }
2748         /* width larger as height -> landscape */
2749         if (pda->dlga->ptPaperSize.x > pda->dlga->ptPaperSize.y)
2750             CheckRadioButton(hDlg, rad1, rad2, rad2);
2751         else /* this is default if papersize is not set */
2752             CheckRadioButton(hDlg, rad1, rad2, rad1);
2753         if (pda->dlga->Flags & PSD_DISABLEORIENTATION) {
2754             EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
2755             EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
2756         }
2757         /* We fill them out enabled or not */
2758         if (pda->dlga->Flags & PSD_MARGINS) {
2759             WCHAR str[100];
2760             _c_size2strW(pda,pda->dlga->rtMargin.left,str);
2761             SetDlgItemTextW(hDlg,edt4,str);
2762             _c_size2strW(pda,pda->dlga->rtMargin.top,str);
2763             SetDlgItemTextW(hDlg,edt5,str);
2764             _c_size2strW(pda,pda->dlga->rtMargin.right,str);
2765             SetDlgItemTextW(hDlg,edt6,str);
2766             _c_size2strW(pda,pda->dlga->rtMargin.bottom,str);
2767             SetDlgItemTextW(hDlg,edt7,str);
2768         } else {
2769             /* default is 1 inch */
2770             DWORD size = _c_inch2size((LPPAGESETUPDLGA)pda->dlga,1000);
2771             WCHAR       str[20];
2772             _c_size2strW(pda,size,str);
2773             SetDlgItemTextW(hDlg,edt4,str);
2774             SetDlgItemTextW(hDlg,edt5,str);
2775             SetDlgItemTextW(hDlg,edt6,str);
2776             SetDlgItemTextW(hDlg,edt7,str);
2777         }
2778         PRINTDLG_PS_ChangePrinterW(hDlg,pda);
2779         if (pda->dlga->Flags & PSD_DISABLEPAPER) {
2780             EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
2781             EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
2782         }
2783         return TRUE;
2784     } else {
2785         pda = (PageSetupDataW*)GetPropW(hDlg, __WINE_PAGESETUPDLGDATA);
2786         if (!pda) {
2787             WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
2788             return FALSE;
2789         }
2790         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2791             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
2792             if (res) return res;
2793         }
2794     }
2795     switch (uMsg) {
2796     case WM_COMMAND:
2797         return PRINTDLG_PS_WMCommandW(hDlg, wParam, lParam, pda);
2798     }
2799     return FALSE;
2800 }
2801
2802 /***********************************************************************
2803  *            PageSetupDlgA  (COMDLG32.@)
2804  */
2805 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg) {
2806     HGLOBAL             hDlgTmpl;
2807     LPVOID              ptr;
2808     BOOL                bRet;
2809     PageSetupDataA      *pda;
2810     PRINTDLGA           pdlg;
2811
2812     if(TRACE_ON(commdlg)) {
2813         char flagstr[1000] = "";
2814         struct pd_flags *pflag = psd_flags;
2815         for( ; pflag->name; pflag++) {
2816             if(setupdlg->Flags & pflag->flag) {
2817                 strcat(flagstr, pflag->name);
2818                 strcat(flagstr, "|");
2819             }
2820         }
2821         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2822               "hinst %p, flags %08lx (%s)\n",
2823               setupdlg, setupdlg->hwndOwner, setupdlg->hDevMode,
2824               setupdlg->hDevNames,
2825               setupdlg->hInstance, setupdlg->Flags, flagstr);
2826     }
2827
2828     /* First get default printer data, we need it right after that. */
2829     memset(&pdlg,0,sizeof(pdlg));
2830     pdlg.lStructSize    = sizeof(pdlg);
2831     pdlg.Flags          = PD_RETURNDEFAULT;
2832     bRet = PrintDlgA(&pdlg);
2833     if (!bRet) return FALSE;
2834
2835     /* short cut exit, just return default values */
2836     if (setupdlg->Flags & PSD_RETURNDEFAULT) {
2837         setupdlg->hDevMode      = pdlg.hDevMode;
2838         setupdlg->hDevNames     = pdlg.hDevNames;
2839         /* FIXME: Just return "A4" for now. */
2840         PRINTDLG_PaperSizeA(&pdlg,"A4",&setupdlg->ptPaperSize);
2841         setupdlg->ptPaperSize.x=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.x);
2842         setupdlg->ptPaperSize.y=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.y);
2843         return TRUE;
2844     }
2845     hDlgTmpl = PRINTDLG_GetPGSTemplateA(setupdlg);
2846     if (!hDlgTmpl) {
2847         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2848         return FALSE;
2849     }
2850     ptr = LockResource( hDlgTmpl );
2851     if (!ptr) {
2852         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2853         return FALSE;
2854     }
2855     pda = HeapAlloc(GetProcessHeap(),0,sizeof(*pda));
2856     pda->dlga = setupdlg;
2857     memcpy(&pda->pdlg,&pdlg,sizeof(pdlg));
2858
2859     bRet = (0<DialogBoxIndirectParamA(
2860                 setupdlg->hInstance,
2861                 ptr,
2862                 setupdlg->hwndOwner,
2863                 PageDlgProcA,
2864                 (LPARAM)pda)
2865     );
2866     return bRet;
2867 }
2868 /***********************************************************************
2869  *            PageSetupDlgW  (COMDLG32.@)
2870  */
2871 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg) {
2872     HGLOBAL             hDlgTmpl;
2873     LPVOID              ptr;
2874     BOOL                bRet;
2875     PageSetupDataW      *pdw;
2876     PRINTDLGW           pdlg;
2877
2878     if(TRACE_ON(commdlg)) {
2879         char flagstr[1000] = "";
2880         struct pd_flags *pflag = psd_flags;
2881         for( ; pflag->name; pflag++) {
2882             if(setupdlg->Flags & pflag->flag) {
2883                 strcat(flagstr, pflag->name);
2884                 strcat(flagstr, "|");
2885             }
2886         }
2887         TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2888               "hinst %p, flags %08lx (%s)\n",
2889               setupdlg, setupdlg->hwndOwner, setupdlg->hDevMode,
2890               setupdlg->hDevNames,
2891               setupdlg->hInstance, setupdlg->Flags, flagstr);
2892     }
2893
2894     /* First get default printer data, we need it right after that. */
2895     memset(&pdlg,0,sizeof(pdlg));
2896     pdlg.lStructSize    = sizeof(pdlg);
2897     pdlg.Flags          = PD_RETURNDEFAULT;
2898     bRet = PrintDlgW(&pdlg);
2899     if (!bRet) return FALSE;
2900
2901     /* short cut exit, just return default values */
2902     if (setupdlg->Flags & PSD_RETURNDEFAULT) {
2903         static const WCHAR a4[] = {'A','4',0};
2904         setupdlg->hDevMode      = pdlg.hDevMode;
2905         setupdlg->hDevNames     = pdlg.hDevNames;
2906         /* FIXME: Just return "A4" for now. */
2907         PRINTDLG_PaperSizeW(&pdlg,a4,&setupdlg->ptPaperSize);
2908         setupdlg->ptPaperSize.x=_c_10mm2size((LPPAGESETUPDLGA)setupdlg,setupdlg->ptPaperSize.x);
2909         setupdlg->ptPaperSize.y=_c_10mm2size((LPPAGESETUPDLGA)setupdlg,setupdlg->ptPaperSize.y);
2910         return TRUE;
2911     }
2912     hDlgTmpl = PRINTDLG_GetPGSTemplateW(setupdlg);
2913     if (!hDlgTmpl) {
2914         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2915         return FALSE;
2916     }
2917     ptr = LockResource( hDlgTmpl );
2918     if (!ptr) {
2919         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2920         return FALSE;
2921     }
2922     pdw = HeapAlloc(GetProcessHeap(),0,sizeof(*pdw));
2923     pdw->dlga = setupdlg;
2924     memcpy(&pdw->pdlg,&pdlg,sizeof(pdlg));
2925
2926     bRet = (0<DialogBoxIndirectParamW(
2927                 setupdlg->hInstance,
2928                 ptr,
2929                 setupdlg->hwndOwner,
2930                 PageDlgProcW,
2931                 (LPARAM)pdw)
2932     );
2933     return bRet;
2934 }
2935
2936 /***********************************************************************
2937  *      PrintDlgExA (COMDLG32.@)
2938  */
2939 HRESULT WINAPI PrintDlgExA(LPVOID lpPrintDlgExA) /* [???] FIXME: LPPRINTDLGEXA */
2940 {
2941         FIXME("stub\n");
2942         return E_NOTIMPL;
2943 }
2944 /***********************************************************************
2945  *      PrintDlgExW (COMDLG32.@)
2946  */
2947 HRESULT WINAPI PrintDlgExW(LPVOID lpPrintDlgExW) /* [???] FIXME: LPPRINTDLGEXW */
2948 {
2949         FIXME("stub\n");
2950         return E_NOTIMPL;
2951 }