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