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