When including 'wine/port.h', include it first.
[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 #include <ctype.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wingdi.h"
16 #include "wine/wingdi16.h"
17 #include "winuser.h"
18 #include "wine/winuser16.h"
19 #include "commdlg.h"
20 #include "dlgs.h"
21 #include "debugtools.h"
22 #include "cderr.h"
23 #include "winspool.h"
24 #include "winerror.h"
25
26 DEFAULT_DEBUG_CHANNEL(commdlg);
27
28 #include "cdlg.h"
29
30 /* This PRINTDLGA internal structure stores
31  * pointers to several throughout useful structures.
32  * 
33  */
34 typedef struct  
35 {
36   LPDEVMODEA        lpDevMode;
37   struct {
38       LPPRINTDLGA       lpPrintDlg;
39       LPPRINTDLG16      lpPrintDlg16;
40   } dlg;
41   LPPRINTER_INFO_2A lpPrinterInfo;
42   LPDRIVER_INFO_3A  lpDriverInfo;
43   UINT              HelpMessageID;
44   HICON             hCollateIcon;    /* PrintDlg only */
45   HICON             hNoCollateIcon;  /* PrintDlg only */
46   HICON             hPortraitIcon;   /* PrintSetupDlg only */
47   HICON             hLandscapeIcon;  /* PrintSetupDlg only */
48   HWND              hwndUpDown;
49 } PRINT_PTRA;
50
51 /* Debugging info */
52 static struct pd_flags {
53   DWORD flag;
54   LPSTR name;
55 } pd_flags[] = {
56   {PD_SELECTION, "PD_SELECTION "},
57   {PD_PAGENUMS, "PD_PAGENUMS "},
58   {PD_NOSELECTION, "PD_NOSELECTION "},
59   {PD_NOPAGENUMS, "PD_NOPAGENUMS "},
60   {PD_COLLATE, "PD_COLLATE "},
61   {PD_PRINTTOFILE, "PD_PRINTTOFILE "},
62   {PD_PRINTSETUP, "PD_PRINTSETUP "},
63   {PD_NOWARNING, "PD_NOWARNING "},
64   {PD_RETURNDC, "PD_RETURNDC "},
65   {PD_RETURNIC, "PD_RETURNIC "},
66   {PD_RETURNDEFAULT, "PD_RETURNDEFAULT "},
67   {PD_SHOWHELP, "PD_SHOWHELP "},
68   {PD_ENABLEPRINTHOOK, "PD_ENABLEPRINTHOOK "},
69   {PD_ENABLESETUPHOOK, "PD_ENABLESETUPHOOK "},
70   {PD_ENABLEPRINTTEMPLATE, "PD_ENABLEPRINTTEMPLATE "},
71   {PD_ENABLESETUPTEMPLATE, "PD_ENABLESETUPTEMPLATE "},
72   {PD_ENABLEPRINTTEMPLATEHANDLE, "PD_ENABLEPRINTTEMPLATEHANDLE "},
73   {PD_ENABLESETUPTEMPLATEHANDLE, "PD_ENABLESETUPTEMPLATEHANDLE "},
74   {PD_USEDEVMODECOPIES, "PD_USEDEVMODECOPIES[ANDCOLLATE] "},
75   {PD_DISABLEPRINTTOFILE, "PD_DISABLEPRINTTOFILE "},
76   {PD_HIDEPRINTTOFILE, "PD_HIDEPRINTTOFILE "},
77   {PD_NONETWORKBUTTON, "PD_NONETWORKBUTTON "},
78   {-1, NULL}
79 };
80
81 /* Debugging info */
82 static struct pd_flags psd_flags[] = {
83   {PSD_MINMARGINS,"PSD_MINMARGINS"},
84   {PSD_MARGINS,"PSD_MARGINS"},
85   {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
86   {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
87   {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
88   {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
89   {PSD_NOWARNING,"PSD_NOWARNING"},
90   {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
91   {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
92   {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
93   {PSD_SHOWHELP,"PSD_SHOWHELP"},
94   {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
95   {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
96   {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
97   {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
98   {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
99   {-1, NULL}
100 };
101
102 /* Yes these constants are the same, but we're just copying win98 */
103 #define UPDOWN_ID 0x270f
104 #define MAX_COPIES 9999
105
106 /***********************************************************************
107  *    PRINTDLG_GetDefaultPrinterName
108  *
109  * Returns the default printer name in buf.
110  * Even under WinNT/2000 default printer is retrieved via GetProfileString - 
111  * these entries are mapped somewhere in the registry rather than win.ini.
112  *
113  * Returns TRUE on success else FALSE
114  */
115 static BOOL PRINTDLG_GetDefaultPrinterName(LPSTR buf, DWORD len)
116 {
117     char *ptr;
118
119     if(!GetProfileStringA("windows", "device", "", buf, len)) {
120         TRACE("No profile entry for default printer found.\n");
121         return FALSE;
122     }
123     if((ptr = strchr(buf, ',')) == NULL) {
124         FIXME("bad format for default printer (%s)!\n",buf);
125         return FALSE;
126     }
127     *ptr = '\0';
128     return TRUE;
129 }
130
131 /***********************************************************************
132  *    PRINTDLG_OpenDefaultPrinter
133  *
134  * Returns a winspool printer handle to the default printer in *hprn
135  * Caller must call ClosePrinter on the handle
136  *
137  * Returns TRUE on success else FALSE
138  */
139 static BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
140 {
141     char buf[260];
142     BOOL res;
143     if(!PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf)))
144         return FALSE;
145     res = OpenPrinterA(buf, hprn, NULL);
146     if (!res)
147         FIXME("Could not open printer %s?!\n",buf);
148     return res;
149 }
150
151 /***********************************************************************
152  *    PRINTDLG_SetUpPrinterListCombo
153  *
154  * Initializes printer list combox.
155  * hDlg:  HWND of dialog
156  * id:    Control id of combo
157  * name:  Name of printer to select
158  *
159  * Initializes combo with list of available printers.  Selects printer 'name'
160  * If name is NULL or does not exist select the default printer.
161  *
162  * Returns number of printers added to list.
163  */
164 static INT PRINTDLG_SetUpPrinterListCombo(HWND hDlg, UINT id, LPCSTR name)
165 {
166     DWORD needed, num;
167     INT i;
168     LPPRINTER_INFO_2A pi;
169     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
170     pi = HeapAlloc(GetProcessHeap(), 0, needed);
171     EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed, 
172                   &num);
173
174     for(i = 0; i < num; i++) {
175         SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
176                             (LPARAM)pi[i].pPrinterName );
177     }
178     HeapFree(GetProcessHeap(), 0, pi);
179     if(!name ||
180        (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
181                                 (LPARAM)name)) == CB_ERR) {
182
183         char buf[260];
184         FIXME("Can't find '%s' in printer list so trying to find default\n",
185               name);
186         if(!PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf)))
187             return num;
188         i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
189         if(i == CB_ERR)
190             FIXME("Can't find default printer in printer list\n");
191     }
192     SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
193     return num;
194 }
195
196 /***********************************************************************
197  *             PRINTDLG_CreateDevNames          [internal]
198  *
199  *
200  *   creates a DevNames structure.
201  *
202  *  (NB. when we handle unicode the offsets will be in wchars).
203  */
204 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, char* DeviceDriverName, 
205                                     char* DeviceName, char* OutputPort)
206 {
207     long size;
208     char*   pDevNamesSpace;
209     char*   pTempPtr;
210     LPDEVNAMES lpDevNames;
211     char buf[260];
212
213     size = strlen(DeviceDriverName) + 1
214             + strlen(DeviceName) + 1
215             + strlen(OutputPort) + 1
216             + sizeof(DEVNAMES);
217             
218     if(*hmem)
219         *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
220     else
221         *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
222     if (*hmem == 0)
223         return FALSE;
224
225     pDevNamesSpace = GlobalLock(*hmem);
226     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
227
228     pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
229     strcpy(pTempPtr, DeviceDriverName);
230     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
231
232     pTempPtr += strlen(DeviceDriverName) + 1;
233     strcpy(pTempPtr, DeviceName);
234     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
235         
236     pTempPtr += strlen(DeviceName) + 1;
237     strcpy(pTempPtr, OutputPort);
238     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
239
240     PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf));
241     lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
242     GlobalUnlock(*hmem);
243     return TRUE;
244 }
245
246 static BOOL PRINTDLG_CreateDevNames16(HGLOBAL16 *hmem, char* DeviceDriverName, 
247                                       char* DeviceName, char* OutputPort)
248 {
249     long size;
250     char*   pDevNamesSpace;
251     char*   pTempPtr;
252     LPDEVNAMES lpDevNames;
253     char buf[260];
254
255     size = strlen(DeviceDriverName) + 1
256             + strlen(DeviceName) + 1
257             + strlen(OutputPort) + 1
258             + sizeof(DEVNAMES);
259             
260     if(*hmem)
261         *hmem = GlobalReAlloc16(*hmem, size, GMEM_MOVEABLE);
262     else
263         *hmem = GlobalAlloc16(GMEM_MOVEABLE, size);
264     if (*hmem == 0)
265         return FALSE;
266
267     pDevNamesSpace = GlobalLock16(*hmem);
268     lpDevNames = (LPDEVNAMES) pDevNamesSpace;
269
270     pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
271     strcpy(pTempPtr, DeviceDriverName);
272     lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
273
274     pTempPtr += strlen(DeviceDriverName) + 1;
275     strcpy(pTempPtr, DeviceName);
276     lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
277         
278     pTempPtr += strlen(DeviceName) + 1;
279     strcpy(pTempPtr, OutputPort);
280     lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
281
282     PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf));
283     lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
284     GlobalUnlock16(*hmem);
285     return TRUE;
286 }
287
288
289 /***********************************************************************
290  *             PRINTDLG_UpdatePrintDlg          [internal]
291  *
292  *
293  *   updates the PrintDlg structure for returnvalues.
294  *      
295  * RETURNS
296  *   FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
297  *   TRUE  if succesful.
298  */
299 static BOOL PRINTDLG_UpdatePrintDlg(HWND hDlg, 
300                                     PRINT_PTRA* PrintStructures)
301 {
302     LPPRINTDLGA       lppd = PrintStructures->dlg.lpPrintDlg;
303     PDEVMODEA         lpdm = PrintStructures->lpDevMode;
304     LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
305
306
307     if(!lpdm) {
308         FIXME("No lpdm ptr?\n");
309         return FALSE;
310     }
311
312
313     if(!(lppd->Flags & PD_PRINTSETUP)) {
314         /* check whether nFromPage and nToPage are within range defined by
315          * nMinPage and nMaxPage
316          */
317         if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
318             WORD nToPage;
319             WORD nFromPage;
320             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
321             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
322             if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
323                 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
324                 char resourcestr[256];
325                 char resultstr[256];
326                 LoadStringA(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, 
327                             resourcestr, 255);
328                 sprintf(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
329                 LoadStringA(COMDLG32_hInstance, PD32_PRINT_TITLE, 
330                             resourcestr, 255);
331                 MessageBoxA(hDlg, resultstr, resourcestr,
332                             MB_OK | MB_ICONWARNING);
333                 return FALSE;
334             }
335             lppd->nFromPage = nFromPage;
336             lppd->nToPage   = nToPage;
337         }
338
339         if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
340             lppd->Flags |= PD_PRINTTOFILE;
341             pi->pPortName = "FILE:";
342         }
343
344         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
345             FIXME("Collate lppd not yet implemented as output\n");
346         }
347
348         /* set PD_Collate and nCopies */
349         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
350           /*  The application doesn't support multiple copies or collate...
351            */
352             lppd->Flags &= ~PD_COLLATE;
353             lppd->nCopies = 1;
354           /* if the printer driver supports it... store info there
355            * otherwise no collate & multiple copies !
356            */
357             if (lpdm->dmFields & DM_COLLATE)
358                 lpdm->dmCollate = 
359                   (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
360             if (lpdm->dmFields & DM_COPIES)
361                 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
362         } else {
363             if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
364                 lppd->Flags |= PD_COLLATE;
365             else
366                lppd->Flags &= ~PD_COLLATE;
367             lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
368         }
369     }
370     return TRUE;
371 }
372
373 static BOOL PRINTDLG_PaperSize(
374         PRINTDLGA       *pdlga,const char *PaperSize,LPPOINT size
375 ) {
376     DEVNAMES    *dn;
377     DEVMODEA    *dm;
378     LPSTR       devname,portname;
379     int         i;
380     INT         NrOfEntries,ret;
381     char        *Names = NULL;
382     POINT       *points = NULL;
383     BOOL        retval = FALSE;
384     
385     dn = GlobalLock(pdlga->hDevNames);
386     dm = GlobalLock(pdlga->hDevMode);
387     devname     = ((char*)dn)+dn->wDeviceOffset;
388     portname    = ((char*)dn)+dn->wOutputOffset;
389
390
391     NrOfEntries = DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,NULL,dm);
392     if (!NrOfEntries) {
393         FIXME("No papernames found for %s/%s\n",devname,portname);
394         goto out;
395     }
396     if (NrOfEntries == -1) {
397         ERR("Hmm ? DeviceCapabilities() DC_PAPERNAMES failed, ret -1 !\n");
398         goto out;
399     }
400             
401     Names = (char*)HeapAlloc(GetProcessHeap(),0,NrOfEntries*64);
402     if (NrOfEntries != (ret=DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,Names,dm))) {
403         FIXME("Number of returned vals %d is not %d\n",NrOfEntries,ret);
404         goto out;
405     }
406     for (i=0;i<NrOfEntries;i++)
407         if (!strcmp(PaperSize,Names+(64*i)))
408             break;
409     HeapFree(GetProcessHeap(),0,Names);
410     if (i==NrOfEntries) {
411         FIXME("Papersize %s not found in list?\n",PaperSize);
412         goto out;
413     }
414     points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
415     if (NrOfEntries!=(ret=DeviceCapabilitiesA(devname,portname,DC_PAPERSIZE,(LPBYTE)points,dm))) {
416         FIXME("Number of returned sizes %d is not %d?\n",NrOfEntries,ret);
417         goto out;
418     }
419     /* this is _10ths_ of a millimeter */
420     size->x=points[i].x;
421     size->y=points[i].y;
422     FIXME("papersize is %ld x %ld\n",size->x,size->y);
423     retval = TRUE;
424 out:
425     GlobalUnlock(pdlga->hDevNames);
426     GlobalUnlock(pdlga->hDevMode);
427     if (Names) HeapFree(GetProcessHeap(),0,Names);
428     if (points) HeapFree(GetProcessHeap(),0,points);
429     return retval;
430 }
431
432
433 /************************************************************************
434  * PRINTDLG_SetUpPaperComboBox
435  *
436  * Initialize either the papersize or inputslot combos of the Printer Setup
437  * dialog.  We store the associated word (eg DMPAPER_A4) as the item data.
438  * We also try to re-select the old selection.
439  */
440 static BOOL PRINTDLG_SetUpPaperComboBox(HWND hDlg,
441                                         int   nIDComboBox,
442                                         char* PrinterName, 
443                                         char* PortName,
444                                         LPDEVMODEA dm)
445 {
446     int     i;
447     int     NrOfEntries;
448     char*   Names;
449     WORD*   Words;
450     DWORD   Sel;
451     WORD    oldWord = 0;
452     int     NamesSize;
453     int     fwCapability_Names;
454     int     fwCapability_Words;
455     
456     TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
457     
458     /* query the dialog box for the current selected value */
459     Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
460     if(Sel != CB_ERR) {
461         /* we enter here only if a different printer is selected after
462          * the Print Setup dialog is opened. The current settings are
463          * stored into the newly selected printer.
464          */
465         oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
466                                       Sel, 0);
467         if (dm) {
468             if (nIDComboBox == cmb2)
469                 dm->u1.s1.dmPaperSize = oldWord;
470             else
471                 dm->dmDefaultSource = oldWord;
472         }
473     }
474     else {
475         /* we enter here only when the Print setup dialog is initially
476          * opened. In this case the settings are restored from when
477          * the dialog was last closed.
478          */
479         if (dm) {
480             if (nIDComboBox == cmb2)
481                 oldWord = dm->u1.s1.dmPaperSize;
482             else
483                 oldWord = dm->dmDefaultSource;
484         }
485     }
486
487     if (nIDComboBox == cmb2) {
488          NamesSize          = 64;
489          fwCapability_Names = DC_PAPERNAMES;
490          fwCapability_Words = DC_PAPERS;
491     } else {
492          nIDComboBox        = cmb3;
493          NamesSize          = 24;
494          fwCapability_Names = DC_BINNAMES;
495          fwCapability_Words = DC_BINS;
496     }
497     
498     /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the 
499      * paper settings. As Wine doesn't allow VXDs, this results in a crash.
500      */
501     WARN(" if your printer driver uses VXDs, expect a crash now!\n");
502     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
503                                       fwCapability_Names, NULL, dm);
504     if (NrOfEntries == 0)
505          WARN("no Name Entries found!\n");
506     else if (NrOfEntries < 0)
507          return FALSE;
508
509     if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
510        != NrOfEntries) {
511         ERR("Number of caps is different\n");
512         NrOfEntries = 0;
513     }
514
515     Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*NamesSize);
516     Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
517     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
518                                       fwCapability_Names, Names, dm);
519     NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
520                                       fwCapability_Words, (LPSTR)Words, dm);
521
522     /* reset any current content in the combobox */
523     SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
524     
525     /* store new content */
526     for (i = 0; i < NrOfEntries; i++) {
527         DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
528                                         (LPARAM)(&Names[i*NamesSize]) );
529         SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos, 
530                             Words[i]);
531     }
532
533     /* Look for old selection - can't do this is previous loop since
534        item order will change as more items are added */
535     Sel = 0;
536     for (i = 0; i < NrOfEntries; i++) {
537         if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
538            oldWord) {
539             Sel = i;
540             break;
541         }
542     }
543     SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
544
545     HeapFree(GetProcessHeap(),0,Words);
546     HeapFree(GetProcessHeap(),0,Names);
547     return TRUE;
548 }
549
550 /***********************************************************************
551  *               PRINTDLG_UpdatePrinterInfoTexts               [internal]
552  */
553 static void PRINTDLG_UpdatePrinterInfoTexts(HWND hDlg, LPPRINTER_INFO_2A pi)
554 {
555     char   StatusMsg[256];
556     char   ResourceString[256];
557     int    i;
558
559     /* Status Message */
560     StatusMsg[0]='\0';
561
562     /* add all status messages */
563     for (i = 0; i < 25; i++) {
564         if (pi->Status & (1<<i)) {
565             LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i, 
566                         ResourceString, 255);
567             strcat(StatusMsg,ResourceString);
568         }
569     }
570     /* append "ready" */
571     /* FIXME: status==ready must only be appended if really so. 
572               but how to detect? */
573     LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY, 
574                 ResourceString, 255);
575     strcat(StatusMsg,ResourceString);
576   
577     SendDlgItemMessageA(hDlg, stc12, WM_SETTEXT, 0, (LPARAM)StatusMsg);
578
579     /* set all other printer info texts */
580     SendDlgItemMessageA(hDlg, stc11, WM_SETTEXT, 0, (LPARAM)pi->pDriverName);
581     if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
582         SendDlgItemMessageA(hDlg, stc14, WM_SETTEXT, 0,(LPARAM)pi->pLocation);
583     else
584         SendDlgItemMessageA(hDlg, stc14, WM_SETTEXT, 0,(LPARAM)pi->pPortName);
585     SendDlgItemMessageA(hDlg, stc13, WM_SETTEXT, 0, (LPARAM)(pi->pComment ?
586                         pi->pComment : ""));
587     return;
588 }
589
590
591 /*******************************************************************
592  *
593  *                 PRINTDLG_ChangePrinter
594  *
595  */
596 static BOOL PRINTDLG_ChangePrinter(HWND hDlg, char *name,
597                                    PRINT_PTRA *PrintStructures)
598 {
599     LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
600     LPDEVMODEA lpdm = NULL;
601     LONG dmSize;
602     DWORD needed;
603     HANDLE hprn;
604
605     if(PrintStructures->lpPrinterInfo)
606         HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
607     if(PrintStructures->lpDriverInfo)
608         HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
609     if(!OpenPrinterA(name, &hprn, NULL)) {
610         ERR("Can't open printer %s\n", name);
611         return FALSE;
612     }
613     GetPrinterA(hprn, 2, NULL, 0, &needed);
614     PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
615     GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
616                 &needed);
617     GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
618     PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
619     if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
620             needed, &needed)) {
621         ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
622         return FALSE;
623     }
624     ClosePrinter(hprn);
625
626     PRINTDLG_UpdatePrinterInfoTexts(hDlg, PrintStructures->lpPrinterInfo);
627
628     if(PrintStructures->lpDevMode) {
629         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
630         PrintStructures->lpDevMode = NULL;
631     }
632
633     dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
634     if(dmSize == -1) {
635         ERR("DocumentProperties fails on %s\n", debugstr_a(name));
636         return FALSE;
637     }
638     PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
639     dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
640                                  DM_OUT_BUFFER);
641     if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
642                           !strcmp(lpdm->dmDeviceName,
643                                   PrintStructures->lpDevMode->dmDeviceName)) {
644       /* Supplied devicemode matches current printer so try to use it */
645         DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
646                             DM_OUT_BUFFER | DM_IN_BUFFER);
647     }
648     if(lpdm)
649         GlobalUnlock(lppd->hDevMode);
650
651     lpdm = PrintStructures->lpDevMode;  /* use this as a shortcut */
652
653     if(!(lppd->Flags & PD_PRINTSETUP)) {
654       /* Print range (All/Range/Selection) */
655         SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
656         SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
657         CheckRadioButton(hDlg, rad1, rad3, rad1);               /* default */
658         if (lppd->Flags & PD_NOSELECTION)
659             EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
660         else
661             if (lppd->Flags & PD_SELECTION)
662                 CheckRadioButton(hDlg, rad1, rad3, rad2);
663         if (lppd->Flags & PD_NOPAGENUMS) {
664             EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
665             EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
666             EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
667             EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
668             EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
669         } else {
670             if (lppd->Flags & PD_PAGENUMS)
671                 CheckRadioButton(hDlg, rad1, rad3, rad3);
672         }
673         /* "All xxx pages"... */
674         {
675             char        resourcestr[64];
676             char        result[64];
677             LoadStringA(COMDLG32_hInstance, PD32_PRINT_ALL_X_PAGES, 
678                         resourcestr, 49);
679             sprintf(result,resourcestr,lppd->nMaxPage - lppd->nMinPage + 1);
680             SendDlgItemMessageA(hDlg, rad1, WM_SETTEXT, 0, (LPARAM) result);
681         }
682         
683         /* Collate pages 
684          *
685          * FIXME: The ico3 is not displayed for some reason. I don't know why.
686          */
687         if (lppd->Flags & PD_COLLATE) {
688             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON, 
689                                 (LPARAM)PrintStructures->hCollateIcon);
690             CheckDlgButton(hDlg, chx2, 1);
691         } else {
692             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON, 
693                                 (LPARAM)PrintStructures->hNoCollateIcon);
694             CheckDlgButton(hDlg, chx2, 0);
695         }
696
697         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
698           /* if printer doesn't support it: no Collate */
699             if (!(lpdm->dmFields & DM_COLLATE)) {
700                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);    
701                 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);    
702             }
703         }
704
705         /* nCopies */
706         {
707           INT copies;
708           if (lppd->hDevMode == 0)
709               copies = lppd->nCopies;
710           else
711               copies = lpdm->dmCopies;
712           if(copies == 0) copies = 1;
713           else if(copies < 0) copies = MAX_COPIES;
714           SetDlgItemInt(hDlg, edt3, copies, FALSE);
715         }
716
717         if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
718           /* if printer doesn't support it: no nCopies */
719             if (!(lpdm->dmFields & DM_COPIES)) {
720                 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);    
721                 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);    
722             }
723         }
724
725         /* print to file */
726         CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
727         if (lppd->Flags & PD_DISABLEPRINTTOFILE)
728             EnableWindow(GetDlgItem(hDlg, chx1), FALSE);    
729         if (lppd->Flags & PD_HIDEPRINTTOFILE)
730             ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
731
732     } else { /* PD_PRINTSETUP */
733       BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
734
735       PRINTDLG_SetUpPaperComboBox(hDlg, cmb2,
736                                   PrintStructures->lpPrinterInfo->pPrinterName,
737                                   PrintStructures->lpPrinterInfo->pPortName,
738                                   lpdm);
739       PRINTDLG_SetUpPaperComboBox(hDlg, cmb3,
740                                   PrintStructures->lpPrinterInfo->pPrinterName,
741                                   PrintStructures->lpPrinterInfo->pPortName,
742                                   lpdm);
743       CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
744       SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
745                           (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
746                                    PrintStructures->hLandscapeIcon));
747       
748     }
749
750     /* help button */
751     if ((lppd->Flags & PD_SHOWHELP)==0) {
752         /* hide if PD_SHOWHELP not specified */
753         ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);         
754     }
755     return TRUE;
756 }
757
758 /***********************************************************************
759  *           PRINTDLG_WMInitDialog                      [internal]
760  */
761 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
762                                      PRINT_PTRA* PrintStructures)
763 {
764     LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
765     DEVNAMES *pdn;
766     DEVMODEA *pdm;
767     char *name = NULL;
768     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
769
770     /* load Collate ICONs */
771     /* We load these with LoadImage because they are not a standard
772        size and we don't want them rescaled */
773     PrintStructures->hCollateIcon =
774       LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
775     PrintStructures->hNoCollateIcon = 
776       LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
777
778     /* These can be done with LoadIcon */
779     PrintStructures->hPortraitIcon =
780       LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
781     PrintStructures->hLandscapeIcon =
782       LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
783
784     if(PrintStructures->hCollateIcon == 0 ||
785        PrintStructures->hNoCollateIcon == 0 ||
786        PrintStructures->hPortraitIcon == 0 ||
787        PrintStructures->hLandscapeIcon == 0) {
788         ERR("no icon in resourcefile\n");
789         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
790         EndDialog(hDlg, FALSE);
791     }
792
793     /*
794      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
795      * must be registered and the Help button must be shown.
796      */
797     if (lppd->Flags & PD_SHOWHELP) {
798         if((PrintStructures->HelpMessageID = 
799             RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
800             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
801             return FALSE;
802         }
803     } else
804         PrintStructures->HelpMessageID = 0;
805
806     if(!(lppd->Flags &PD_PRINTSETUP)) {
807         PrintStructures->hwndUpDown =
808           CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
809                               UDS_NOTHOUSANDS | UDS_ARROWKEYS |
810                               UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
811                               hDlg, UPDOWN_ID, COMDLG32_hInstance,
812                               GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
813     }
814
815     /* FIXME: I allow more freedom than either Win95 or WinNT,
816      *        which do not agree to what errors should be thrown or not
817      *        in case nToPage or nFromPage is out-of-range.
818      */
819     if (lppd->nMaxPage < lppd->nMinPage)
820         lppd->nMaxPage = lppd->nMinPage;
821     if (lppd->nMinPage == lppd->nMaxPage) 
822         lppd->Flags |= PD_NOPAGENUMS;        
823     if (lppd->nToPage < lppd->nMinPage)
824         lppd->nToPage = lppd->nMinPage;
825     if (lppd->nToPage > lppd->nMaxPage)
826         lppd->nToPage = lppd->nMaxPage;
827     if (lppd->nFromPage < lppd->nMinPage)
828         lppd->nFromPage = lppd->nMinPage;
829     if (lppd->nFromPage > lppd->nMaxPage)
830         lppd->nFromPage = lppd->nMaxPage;
831
832     /* if we have the combo box, fill it */
833     if (GetDlgItem(hDlg,comboID)) {
834         /* Fill Combobox 
835          */
836         pdn = GlobalLock(lppd->hDevNames);
837         pdm = GlobalLock(lppd->hDevMode);
838         if(pdn)
839             name = (char*)pdn + pdn->wDeviceOffset;
840         else if(pdm)
841             name = pdm->dmDeviceName;
842         PRINTDLG_SetUpPrinterListCombo(hDlg, comboID, name);
843         if(pdm) GlobalUnlock(lppd->hDevMode);
844         if(pdn) GlobalUnlock(lppd->hDevNames);
845
846         /* Now find selected printer and update rest of dlg */
847         name = HeapAlloc(GetProcessHeap(),0,256);
848         if (GetDlgItemTextA(hDlg, comboID, name, 255))
849             PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
850         HeapFree(GetProcessHeap(),0,name);
851     } else {
852         /* else use default printer */
853         char name[200];
854         BOOL ret = PRINTDLG_GetDefaultPrinterName(name, sizeof(name));
855
856         if (ret)
857             PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
858         else
859             FIXME("No default printer found, expect problems!\n");
860     }
861     return TRUE;
862 }
863
864 /***********************************************************************
865  *           PRINTDLG_WMInitDialog                      [internal]
866  */
867 static LRESULT PRINTDLG_WMInitDialog16(HWND hDlg, WPARAM wParam,
868                                      PRINT_PTRA* PrintStructures)
869 {
870     LPPRINTDLG16 lppd = PrintStructures->dlg.lpPrintDlg16;
871     DEVNAMES *pdn;
872     DEVMODEA *pdm;
873     char *name = NULL;
874     UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
875
876     /* load Collate ICONs */
877     PrintStructures->hCollateIcon =
878       LoadIconA(COMDLG32_hInstance, "PD32_COLLATE");
879     PrintStructures->hNoCollateIcon = 
880       LoadIconA(COMDLG32_hInstance, "PD32_NOCOLLATE");
881     if(PrintStructures->hCollateIcon == 0 ||
882        PrintStructures->hNoCollateIcon == 0) {
883         ERR("no icon in resourcefile\n");
884         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
885         EndDialog(hDlg, FALSE);
886     }
887
888     /* load Paper Orientation ICON */
889     /* FIXME: not implemented yet */
890
891     /*
892      * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
893      * must be registered and the Help button must be shown.
894      */
895     if (lppd->Flags & PD_SHOWHELP) {
896         if((PrintStructures->HelpMessageID = 
897             RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
898             COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
899             return FALSE;
900         }
901     } else
902         PrintStructures->HelpMessageID = 0;
903
904     if (!(lppd->Flags & PD_PRINTSETUP)) {
905         /* We have a print quality combo box. What shall we do? */
906         if (GetDlgItem(hDlg,cmb1)) {
907             char buf [20];
908
909             FIXME("Print quality only displaying currently.\n");
910
911             pdm = GlobalLock16(lppd->hDevMode);
912             if(pdm) {
913                 switch (pdm->dmPrintQuality) {
914                 case DMRES_HIGH         : strcpy(buf,"High");break;
915                 case DMRES_MEDIUM       : strcpy(buf,"Medium");break;
916                 case DMRES_LOW          : strcpy(buf,"Low");break;
917                 case DMRES_DRAFT        : strcpy(buf,"Draft");break;
918                 case 0                  : strcpy(buf,"Default");break;
919                 default                 : sprintf(buf,"%ddpi",pdm->dmPrintQuality);break;
920                 }
921                 GlobalUnlock16(lppd->hDevMode);
922             } else 
923                 strcpy(buf,"Default");
924             SendDlgItemMessageA(hDlg,cmb1,CB_ADDSTRING,0,(LPARAM)buf);
925             SendDlgItemMessageA(hDlg,cmb1,CB_SETCURSEL,0,0);
926             EnableWindow(GetDlgItem(hDlg,cmb1),FALSE);
927         }
928     }
929
930     /* FIXME: I allow more freedom than either Win95 or WinNT,
931      *        which do not agree to what errors should be thrown or not
932      *        in case nToPage or nFromPage is out-of-range.
933      */
934     if (lppd->nMaxPage < lppd->nMinPage)
935         lppd->nMaxPage = lppd->nMinPage;
936     if (lppd->nMinPage == lppd->nMaxPage) 
937         lppd->Flags |= PD_NOPAGENUMS;        
938     if (lppd->nToPage < lppd->nMinPage)
939         lppd->nToPage = lppd->nMinPage;
940     if (lppd->nToPage > lppd->nMaxPage)
941         lppd->nToPage = lppd->nMaxPage;
942     if (lppd->nFromPage < lppd->nMinPage)
943         lppd->nFromPage = lppd->nMinPage;
944     if (lppd->nFromPage > lppd->nMaxPage)
945         lppd->nFromPage = lppd->nMaxPage;
946
947     /* If the printer combo box is in the dialog, fill it */
948     if (GetDlgItem(hDlg,comboID)) {
949         /* Fill Combobox 
950          */
951         pdn = GlobalLock16(lppd->hDevNames);
952         pdm = GlobalLock16(lppd->hDevMode);
953         if(pdn)
954             name = (char*)pdn + pdn->wDeviceOffset;
955         else if(pdm)
956             name = pdm->dmDeviceName;
957         PRINTDLG_SetUpPrinterListCombo(hDlg, comboID, name);
958         if(pdm) GlobalUnlock16(lppd->hDevMode);
959         if(pdn) GlobalUnlock16(lppd->hDevNames);
960
961         /* Now find selected printer and update rest of dlg */
962         name = HeapAlloc(GetProcessHeap(),0,256);
963         if (GetDlgItemTextA(hDlg, comboID, name, 255))
964             PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
965     } else {
966         /* else just use default printer */
967         char name[200];
968         BOOL ret = PRINTDLG_GetDefaultPrinterName(name, sizeof(name));
969
970         if (ret)
971             PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
972         else
973             FIXME("No default printer found, expect problems!\n");
974     }
975     HeapFree(GetProcessHeap(),0,name);
976
977     return TRUE;
978 }
979
980 /***********************************************************************
981  *                              PRINTDLG_WMCommand               [internal]
982  */
983 static LRESULT PRINTDLG_WMCommand(HWND hDlg, WPARAM wParam, 
984                         LPARAM lParam, PRINT_PTRA* PrintStructures)
985 {
986     LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
987     UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
988     LPDEVMODEA lpdm = PrintStructures->lpDevMode;
989
990     switch (LOWORD(wParam))  {
991     case IDOK:
992         TRACE(" OK button was hit\n");
993         if (PRINTDLG_UpdatePrintDlg(hDlg, PrintStructures)!=TRUE) {
994             FIXME("Update printdlg was not successful!\n");
995             return(FALSE);
996         }
997         EndDialog(hDlg, TRUE);
998         return(TRUE);
999
1000     case IDCANCEL:
1001         TRACE(" CANCEL button was hit\n");
1002         EndDialog(hDlg, FALSE);
1003         return(FALSE);
1004
1005      case pshHelp:
1006         TRACE(" HELP button was hit\n");
1007         SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID, 
1008                                 (WPARAM) hDlg, (LPARAM) lppd);
1009         break;
1010
1011      case chx2:                         /* collate pages checkbox */
1012         if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1013             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON, 
1014                                     (LPARAM)PrintStructures->hCollateIcon);
1015         else
1016             SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON, 
1017                                     (LPARAM)PrintStructures->hNoCollateIcon);
1018         break;        
1019      case edt1:                         /* from page nr editbox */
1020      case edt2:                         /* to page nr editbox */
1021         if (HIWORD(wParam)==EN_CHANGE) {
1022             WORD nToPage;
1023             WORD nFromPage;
1024             nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1025             nToPage   = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1026             if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1027                 CheckRadioButton(hDlg, rad1, rad3, rad3);
1028         }
1029         break;
1030
1031     case edt3:
1032         if(HIWORD(wParam) == EN_CHANGE) {
1033             INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1034             if(copies <= 1)
1035                 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1036             else
1037                 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1038         }
1039         break;
1040
1041      case psh1:                       /* Print Setup */
1042         {
1043             PRINTDLG16  pdlg;
1044             
1045             if (!PrintStructures->dlg.lpPrintDlg16) {
1046                 FIXME("The 32bit print dialog does not have this button!?\n");
1047                 break;
1048             }
1049
1050             memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1051             pdlg.Flags |= PD_PRINTSETUP;
1052             pdlg.hwndOwner = hDlg;
1053             if (!PrintDlg16(&pdlg))
1054                 break;
1055         }
1056         break;
1057      case psh2:                       /* Properties button */
1058        {
1059          HANDLE hPrinter;
1060          char   PrinterName[256];
1061
1062          GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1063          if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1064              FIXME(" Call to OpenPrinter did not succeed!\n");
1065              break;
1066          }
1067          DocumentPropertiesA(hDlg, hPrinter, PrinterName, 
1068                              PrintStructures->lpDevMode,
1069                              PrintStructures->lpDevMode,
1070                              DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1071          ClosePrinter(hPrinter);
1072          break;
1073        }
1074
1075     case rad1: /* Paperorientation */
1076         if (lppd->Flags & PD_PRINTSETUP)
1077         {
1078               lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1079               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1080                           (LPARAM)(PrintStructures->hPortraitIcon));
1081         }
1082         break;
1083             
1084     case rad2: /* Paperorientation */
1085         if (lppd->Flags & PD_PRINTSETUP)
1086         {
1087               lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1088               SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1089                           (LPARAM)(PrintStructures->hLandscapeIcon));
1090         }
1091         break;
1092             
1093     case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT */
1094          if (PrinterComboID != wParam) {
1095              FIXME("No handling for print quality combo box yet.\n");
1096              break;
1097          }
1098          /* FALLTHROUGH */
1099     case cmb4:                         /* Printer combobox */
1100          if (HIWORD(wParam)==CBN_SELCHANGE) {
1101              char   PrinterName[256];
1102              GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1103              PRINTDLG_ChangePrinter(hDlg, PrinterName, PrintStructures);
1104          }
1105          break;
1106
1107     case cmb2: /* Papersize */
1108       {
1109           DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1110           if(Sel != CB_ERR)
1111               lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1112                                                             CB_GETITEMDATA,
1113                                                             Sel, 0);
1114       }
1115       break;
1116
1117     case cmb3: /* Bin */
1118       {
1119           DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1120           if(Sel != CB_ERR)
1121               lpdm->dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1122                                                           CB_GETITEMDATA, Sel,
1123                                                           0);
1124       }
1125       break; 
1126     }
1127     if(lppd->Flags & PD_PRINTSETUP) {
1128         switch (LOWORD(wParam)) {
1129         case rad1:                         /* orientation */
1130         case rad2:
1131             if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1132                 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1133                     lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1134                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1135                                         (WPARAM)IMAGE_ICON,
1136                                         (LPARAM)PrintStructures->hPortraitIcon);
1137                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1138                                         (WPARAM)IMAGE_ICON,
1139                                         (LPARAM)PrintStructures->hPortraitIcon);
1140                 }
1141             } else {
1142                 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1143                     lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1144                     SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1145                                         (WPARAM)IMAGE_ICON,
1146                                         (LPARAM)PrintStructures->hLandscapeIcon);
1147                     SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1148                                         (WPARAM)IMAGE_ICON,
1149                                         (LPARAM)PrintStructures->hLandscapeIcon);
1150                 }
1151             }
1152             break;
1153         }
1154     }
1155     return FALSE;
1156 }    
1157
1158 /***********************************************************************
1159  *           PrintDlgProcA                      [internal]
1160  */
1161 BOOL WINAPI PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1162                           LPARAM lParam)
1163 {
1164     PRINT_PTRA* PrintStructures;
1165     LRESULT res=FALSE;
1166
1167     if (uMsg!=WM_INITDIALOG) {
1168         PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);   
1169         if (!PrintStructures)
1170             return FALSE;
1171     } else {
1172         PrintStructures = (PRINT_PTRA*) lParam;
1173         SetWindowLongA(hDlg, DWL_USER, lParam); 
1174         res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1175
1176         if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1177             res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(
1178                 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg
1179             ); 
1180         return res;
1181     }
1182   
1183     if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1184         res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1185                                                          lParam);
1186         if(res) return res;
1187     }
1188
1189     switch (uMsg) {
1190     case WM_COMMAND:
1191         return PRINTDLG_WMCommand(hDlg, wParam, lParam, PrintStructures);
1192
1193     case WM_DESTROY:
1194         DestroyIcon(PrintStructures->hCollateIcon);
1195         DestroyIcon(PrintStructures->hNoCollateIcon);
1196         DestroyIcon(PrintStructures->hPortraitIcon);
1197         DestroyIcon(PrintStructures->hLandscapeIcon);
1198         if(PrintStructures->hwndUpDown)
1199             DestroyWindow(PrintStructures->hwndUpDown);
1200         return FALSE;
1201     }    
1202     return res;
1203 }
1204
1205
1206 /************************************************************
1207  *
1208  *      PRINTDLG_Get16TemplateFrom32             [Internal]
1209  *      Generates a 16 bits template from the Wine 32 bits resource
1210  *
1211  */
1212 static HGLOBAL16 PRINTDLG_Get16TemplateFrom32(char *PrintResourceName)
1213 {
1214         HANDLE hResInfo, hDlgTmpl32;
1215         LPCVOID template32;
1216         DWORD size;
1217         HGLOBAL16 hGlobal16;
1218         LPVOID template;
1219  
1220         if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
1221                PrintResourceName, RT_DIALOGA)))
1222         {
1223             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1224             return 0;
1225         }
1226         if (!(hDlgTmpl32 = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
1227             !(template32 = LockResource( hDlgTmpl32 )))
1228         {
1229             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1230             return 0;
1231         }
1232         size = SizeofResource(COMMDLG_hInstance32, hResInfo);
1233         hGlobal16 = GlobalAlloc16(0, size);
1234         if (!hGlobal16)
1235         {
1236             COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
1237             ERR("alloc failure for %ld bytes\n", size);
1238             return 0;
1239         }
1240         template = GlobalLock16(hGlobal16);
1241         if (!template)
1242         {
1243             COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
1244             ERR("global lock failure for %x handle\n", hGlobal16);
1245             GlobalFree16(hGlobal16);
1246             return 0;
1247         }
1248         ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
1249         GlobalUnlock16(hGlobal16);
1250         return hGlobal16;
1251 }
1252
1253 /************************************************************
1254  *
1255  *      PRINTDLG_GetDlgTemplate
1256  *
1257  */
1258 static HGLOBAL PRINTDLG_GetDlgTemplate(PRINTDLGA *lppd)
1259 {
1260     HGLOBAL hDlgTmpl, hResInfo;
1261
1262     if (lppd->Flags & PD_PRINTSETUP) {
1263         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1264             hDlgTmpl = lppd->hSetupTemplate;
1265         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {       
1266             hResInfo = FindResourceA(lppd->hInstance,
1267                                      lppd->lpSetupTemplateName, RT_DIALOGA);
1268             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1269         } else {
1270             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1271                                      RT_DIALOGA);
1272             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1273         }
1274     } else {
1275         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1276             hDlgTmpl = lppd->hPrintTemplate;
1277         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1278             hResInfo = FindResourceA(lppd->hInstance,
1279                                      lppd->lpPrintTemplateName,
1280                                      RT_DIALOGA);
1281             hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1282         } else {
1283             hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1284                                      RT_DIALOGA);
1285             hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1286         }
1287     }
1288     return hDlgTmpl;
1289 }
1290
1291
1292 /************************************************************
1293  *
1294  *      PRINTDLG_GetDlgTemplate
1295  *
1296  */
1297 static HGLOBAL16 PRINTDLG_GetDlgTemplate16(PRINTDLG16 *lppd)
1298 {
1299     HGLOBAL16 hDlgTmpl, hResInfo;
1300
1301     if (lppd->Flags & PD_PRINTSETUP) {
1302         if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1303             hDlgTmpl = lppd->hSetupTemplate;
1304         } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {       
1305             hResInfo = FindResource16(lppd->hInstance,
1306                                      MapSL(lppd->lpSetupTemplateName), RT_DIALOGA);
1307             hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1308         } else {
1309             hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32_SETUP");
1310         }
1311     } else {
1312         if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1313             hDlgTmpl = lppd->hPrintTemplate;
1314         } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1315             hResInfo = FindResource16(lppd->hInstance,
1316                                      MapSL(lppd->lpPrintTemplateName),
1317                                      RT_DIALOGA);
1318             hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1319         } else {
1320             hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32");
1321         }
1322     }
1323     return hDlgTmpl;
1324 }
1325
1326 /***********************************************************************
1327  *
1328  *      PRINTDLG_CreateDC
1329  *
1330  */
1331 static BOOL PRINTDLG_CreateDC(LPPRINTDLGA lppd)
1332 {
1333     DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1334     DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1335
1336     if(lppd->Flags & PD_RETURNDC) {
1337         lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1338                               (char*)pdn + pdn->wDeviceOffset,
1339                               (char*)pdn + pdn->wOutputOffset,
1340                               pdm );
1341     } else if(lppd->Flags & PD_RETURNIC) {
1342         lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1343                               (char*)pdn + pdn->wDeviceOffset,
1344                               (char*)pdn + pdn->wOutputOffset,
1345                               pdm );
1346     }
1347     GlobalUnlock(lppd->hDevNames);
1348     GlobalUnlock(lppd->hDevMode);
1349     return lppd->hDC ? TRUE : FALSE;
1350 }
1351
1352 static BOOL PRINTDLG_CreateDC16(LPPRINTDLG16 lppd)
1353 {
1354     DEVNAMES *pdn = GlobalLock16(lppd->hDevNames);
1355     DEVMODEA *pdm = GlobalLock16(lppd->hDevMode);
1356
1357     if(lppd->Flags & PD_RETURNDC) {
1358         lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1359                               (char*)pdn + pdn->wDeviceOffset,
1360                               (char*)pdn + pdn->wOutputOffset,
1361                               pdm );
1362     } else if(lppd->Flags & PD_RETURNIC) {
1363         lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1364                               (char*)pdn + pdn->wDeviceOffset,
1365                               (char*)pdn + pdn->wOutputOffset,
1366                               pdm );
1367     }
1368     GlobalUnlock16(lppd->hDevNames);
1369     GlobalUnlock16(lppd->hDevMode);
1370     return lppd->hDC ? TRUE : FALSE;
1371 }
1372
1373 /***********************************************************************
1374  *           PrintDlgA   (COMDLG32.@)
1375  *
1376  *  Displays the the PRINT dialog box, which enables the user to specify
1377  *  specific properties of the print job.
1378  *
1379  * RETURNS
1380  *  nonzero if the user pressed the OK button
1381  *  zero    if the user cancelled the window or an error occurred
1382  *
1383  * BUGS
1384  *  PrintDlg:
1385  *  * The Collate Icons do not display, even though they are in the code.
1386  *  * The Properties Button(s) should call DocumentPropertiesA().
1387  *  PrintSetupDlg:
1388  *  * The Paper Orientation Icons are not implemented yet.
1389  *  * The Properties Button(s) should call DocumentPropertiesA().
1390  *  * Settings are not yet taken from a provided DevMode or 
1391  *    default printer settings.
1392  */
1393 BOOL WINAPI PrintDlgA(
1394                       LPPRINTDLGA lppd /* [in/out] ptr to PRINTDLG32 struct */
1395                       )
1396 {
1397     BOOL      bRet = FALSE;
1398     LPVOID   ptr;
1399     HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1400
1401     if(TRACE_ON(commdlg)) {
1402         char flagstr[1000] = "";
1403         struct pd_flags *pflag = pd_flags;
1404         for( ; pflag->name; pflag++) {
1405             if(lppd->Flags & pflag->flag)
1406                 strcat(flagstr, pflag->name);
1407         }
1408         TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1409               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1410               "flags %08lx (%s)\n",
1411               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1412               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1413               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1414     }
1415
1416     if(lppd->lStructSize != sizeof(PRINTDLGA)) {
1417         WARN("structure size failure !!!\n");
1418         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1419         return FALSE; 
1420     }
1421
1422     if(lppd->Flags & PD_RETURNDEFAULT) {
1423         PRINTER_INFO_2A *pbuf;
1424         DRIVER_INFO_3A  *dbuf;
1425         HANDLE hprn;
1426         DWORD needed;
1427
1428         if(lppd->hDevMode || lppd->hDevNames) {
1429             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1430             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE); 
1431             return FALSE;
1432         }
1433         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1434             WARN("Can't find default printer\n");
1435             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN); 
1436             return FALSE;
1437         }
1438
1439         GetPrinterA(hprn, 2, NULL, 0, &needed);
1440         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1441         GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1442
1443         GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1444         dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1445         if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1446             ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",GetLastError(),pbuf->pPrinterName);
1447             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE); 
1448             return FALSE;
1449         }
1450         ClosePrinter(hprn);
1451
1452         PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1453                                   dbuf->pDriverPath,
1454                                   pbuf->pPrinterName,
1455                                   pbuf->pPortName);
1456         lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
1457                                      pbuf->pDevMode->dmDriverExtra);
1458         ptr = GlobalLock(lppd->hDevMode);
1459         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1460                pbuf->pDevMode->dmDriverExtra);
1461         GlobalUnlock(lppd->hDevMode);
1462         HeapFree(GetProcessHeap(), 0, pbuf);
1463         HeapFree(GetProcessHeap(), 0, dbuf);
1464         bRet = TRUE;
1465     } else {
1466         HGLOBAL hDlgTmpl;
1467         PRINT_PTRA *PrintStructures;
1468
1469     /* load Dialog resources, 
1470      * depending on Flags indicates Print32 or Print32_setup dialog 
1471      */
1472         hDlgTmpl = PRINTDLG_GetDlgTemplate(lppd);
1473         if (!hDlgTmpl) {
1474             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1475             return FALSE;
1476         }
1477         ptr = LockResource( hDlgTmpl );
1478         if (!ptr) {
1479             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1480             return FALSE;
1481         }
1482
1483         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1484                                     sizeof(PRINT_PTRA));
1485         PrintStructures->dlg.lpPrintDlg = lppd;
1486
1487         /* and create & process the dialog .
1488          * -1 is failure, 0 is broken hwnd, everything else is ok.
1489          */
1490         bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
1491                                            PrintDlgProcA,
1492                                            (LPARAM)PrintStructures));
1493
1494         if(bRet) {
1495             DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1496             PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1497             DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1498
1499             if (lppd->hDevMode == 0) {
1500                 TRACE(" No hDevMode yet... Need to create my own\n");
1501                 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
1502                                         lpdm->dmSize + lpdm->dmDriverExtra);
1503             } else {
1504                 WORD locks;
1505                 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
1506                     WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1507                     while(locks--) {
1508                         GlobalUnlock(lppd->hDevMode);
1509                         TRACE("Now got %d locks\n", locks);
1510                     }
1511                 }
1512                 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
1513                                                lpdm->dmSize + lpdm->dmDriverExtra,
1514                                                GMEM_MOVEABLE);
1515             }
1516             lpdmReturn = GlobalLock(lppd->hDevMode);
1517             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1518
1519             if (lppd->hDevNames != 0) {
1520                 WORD locks;
1521                 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
1522                     WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1523                     while(locks--)
1524                         GlobalUnlock(lppd->hDevNames);
1525                 }
1526             }
1527             PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1528                     di->pDriverPath,
1529                     pi->pPrinterName,
1530                     pi->pPortName
1531             );
1532             GlobalUnlock(lppd->hDevMode);
1533         }
1534         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1535         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1536         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1537         HeapFree(GetProcessHeap(), 0, PrintStructures);
1538     }
1539     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1540         bRet = PRINTDLG_CreateDC(lppd);
1541
1542     TRACE("exit! (%d)\n", bRet);        
1543     return bRet;    
1544 }
1545
1546 /***********************************************************************
1547  *           PrintDlg   (COMMDLG.20)
1548  * 
1549  *  Displays the the PRINT dialog box, which enables the user to specify
1550  *  specific properties of the print job.
1551  *
1552  * RETURNS
1553  *  nonzero if the user pressed the OK button
1554  *  zero    if the user cancelled the window or an error occurred
1555  *
1556  * BUGS
1557  *  * calls up to the 32-bit versions of the Dialogs, which look different
1558  *  * Customizing is *not* implemented.
1559  */
1560
1561 BOOL16 WINAPI PrintDlg16(
1562               LPPRINTDLG16 lppd /* [in/out] ptr to PRINTDLG struct */
1563 ) {
1564     BOOL      bRet = FALSE;
1565     LPVOID   ptr;
1566     HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1567
1568     if(TRACE_ON(commdlg)) {
1569         char flagstr[1000] = "";
1570         struct pd_flags *pflag = pd_flags;
1571         for( ; pflag->name; pflag++) {
1572             if(lppd->Flags & pflag->flag)
1573                 strcat(flagstr, pflag->name);
1574         }
1575         TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1576               "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1577               "flags %08lx (%s)\n",
1578               lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1579               lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1580               lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1581     }
1582
1583     if(lppd->lStructSize != sizeof(PRINTDLG16)) {
1584         ERR("structure size (%ld/%d)\n",lppd->lStructSize,sizeof(PRINTDLG16));
1585         COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1586         return FALSE;
1587     }
1588
1589     if(lppd->Flags & PD_RETURNDEFAULT) {
1590         PRINTER_INFO_2A *pbuf;
1591         DRIVER_INFO_3A  *dbuf;
1592         HANDLE hprn;
1593         DWORD needed;
1594
1595         if(lppd->hDevMode || lppd->hDevNames) {
1596             WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1597             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE); 
1598             return FALSE;
1599         }
1600         if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1601             WARN("Can't find default printer\n");
1602             COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN); 
1603             return FALSE;
1604         }
1605
1606         GetPrinterA(hprn, 2, NULL, 0, &needed);
1607         pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1608         GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1609         GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1610         dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1611         if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1612             ERR("GetPrinterDriverA failed for %s, le %ld, fix your config!\n",
1613                     pbuf->pPrinterName,GetLastError());
1614             COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE); 
1615             return FALSE;
1616         }
1617         ClosePrinter(hprn);
1618         PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1619                                 dbuf->pDriverPath,
1620                                 pbuf->pPrinterName,
1621                                 pbuf->pPortName);
1622         lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,pbuf->pDevMode->dmSize+
1623                                      pbuf->pDevMode->dmDriverExtra);
1624         ptr = GlobalLock16(lppd->hDevMode);
1625         memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1626                pbuf->pDevMode->dmDriverExtra);
1627         GlobalUnlock16(lppd->hDevMode);
1628         HeapFree(GetProcessHeap(), 0, pbuf);
1629         HeapFree(GetProcessHeap(), 0, dbuf);
1630         bRet = TRUE;
1631     } else {
1632         HGLOBAL hDlgTmpl;
1633         PRINT_PTRA *PrintStructures;
1634
1635     /* load Dialog resources, 
1636      * depending on Flags indicates Print32 or Print32_setup dialog 
1637      */
1638         hDlgTmpl = PRINTDLG_GetDlgTemplate16(lppd);
1639         if (!hDlgTmpl) {
1640             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1641             return FALSE;
1642         }
1643         PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1644                                     sizeof(PRINT_PTRA));
1645         PrintStructures->dlg.lpPrintDlg16 = lppd;
1646         PrintStructures->dlg.lpPrintDlg = (LPPRINTDLGA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(PRINTDLGA));
1647 #define CVAL(x) PrintStructures->dlg.lpPrintDlg->x = lppd->x;
1648 #define MVAL(x) PrintStructures->dlg.lpPrintDlg->x = MapSL(lppd->x);
1649         CVAL(Flags);CVAL(hwndOwner);CVAL(hDC);
1650         CVAL(nFromPage);CVAL(nToPage);CVAL(nMinPage);CVAL(nMaxPage);
1651         CVAL(nCopies);CVAL(hInstance);CVAL(lCustData);
1652         MVAL(lpPrintTemplateName);MVAL(lpSetupTemplateName);
1653         /* Don't copy rest, it is 16 bit specific */
1654 #undef MVAL
1655 #undef CVAL
1656
1657         PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(),0,sizeof(DEVMODEA));
1658
1659         /* and create & process the dialog .
1660          * -1 is failure, 0 is broken hwnd, everything else is ok.
1661          */
1662         bRet =  (0<DialogBoxIndirectParam16(
1663                  hInst, hDlgTmpl, lppd->hwndOwner,
1664                  (DLGPROC16)GetProcAddress16(GetModuleHandle16("COMMDLG"),(LPCSTR)21),
1665                  (LPARAM)PrintStructures
1666                 )
1667         );
1668         if (!PrintStructures->lpPrinterInfo) bRet = FALSE;
1669         if(bRet) {
1670             DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1671             PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1672             DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1673
1674             if (lppd->hDevMode == 0) {
1675                 TRACE(" No hDevMode yet... Need to create my own\n");
1676                 lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,
1677                                         lpdm->dmSize + lpdm->dmDriverExtra);
1678             } else {
1679                 WORD locks;
1680                 if((locks = (GlobalFlags16(lppd->hDevMode)&GMEM_LOCKCOUNT))) {
1681                     WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1682                     while(locks--) {
1683                         GlobalUnlock16(lppd->hDevMode);
1684                         TRACE("Now got %d locks\n", locks);
1685                     }
1686                 }
1687                 lppd->hDevMode = GlobalReAlloc16(lppd->hDevMode,
1688                                                lpdm->dmSize + lpdm->dmDriverExtra,
1689                                                GMEM_MOVEABLE);
1690             }
1691             lpdmReturn = GlobalLock16(lppd->hDevMode);
1692             memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1693
1694             if (lppd->hDevNames != 0) {
1695                 WORD locks;
1696                 if((locks = (GlobalFlags16(lppd->hDevNames)&GMEM_LOCKCOUNT))) {
1697                     WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1698                     while(locks--)
1699                         GlobalUnlock16(lppd->hDevNames);
1700                 }
1701             }
1702             PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1703                     di->pDriverPath,
1704                     pi->pPrinterName,
1705                     pi->pPortName
1706             );
1707             GlobalUnlock16(lppd->hDevMode);
1708         }
1709         if (!(lppd->Flags & (PD_ENABLESETUPTEMPLATEHANDLE | PD_ENABLESETUPTEMPLATE)))
1710             GlobalFree16(hDlgTmpl); /* created from the 32 bits resource */
1711         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1712         HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1713         HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1714         HeapFree(GetProcessHeap(), 0, PrintStructures);
1715     }
1716     if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1717         bRet = PRINTDLG_CreateDC16(lppd);
1718
1719     TRACE("exit! (%d)\n", bRet);        
1720     return bRet;    
1721 }
1722
1723
1724
1725 /***********************************************************************
1726  *           PrintDlgW   (COMDLG32.@)
1727  */
1728 BOOL WINAPI PrintDlgW( LPPRINTDLGW printdlg )
1729 {
1730     FIXME("A really empty stub\n" );
1731     return FALSE;
1732 }
1733
1734 /***********************************************************************
1735  *
1736  *          PageSetupDlg
1737  * rad1 - portrait
1738  * rad2 - landscape
1739  * cmb2 - paper size
1740  * cmb3 - source (tray?)
1741  * edt4 - border left
1742  * edt5 - border top
1743  * edt6 - border right
1744  * edt7 - border bottom
1745  * psh3 - "Printer..."
1746  */
1747
1748 typedef struct {
1749     LPPAGESETUPDLGA     dlga;
1750
1751     PRINTDLGA           pdlg;
1752 } PageSetupData;
1753
1754 static HGLOBAL PRINTDLG_GetPGSTemplate(PAGESETUPDLGA *lppd)
1755 {
1756     HGLOBAL hDlgTmpl, hResInfo;
1757
1758     if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATEHANDLE) {
1759         hDlgTmpl = lppd->hPageSetupTemplate;
1760     } else if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATE) {      
1761         hResInfo = FindResourceA(lppd->hInstance,
1762                                  lppd->lpPageSetupTemplateName, RT_DIALOGA);
1763         hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1764     } else {
1765         hResInfo = FindResourceA(COMDLG32_hInstance,(LPCSTR)PAGESETUPDLGORD,RT_DIALOGA);
1766         hDlgTmpl = LoadResource(COMDLG32_hInstance,hResInfo);
1767     }
1768     return hDlgTmpl;
1769 }
1770
1771 static DWORD
1772 _c_10mm2size(PAGESETUPDLGA *dlga,DWORD size) {
1773     if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1774         return 10*size*10/25.4;
1775     /* If we don't have a flag, we can choose one. Use millimeters
1776      * to avoid confusing me
1777      */
1778     dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1779     FIXME("returning %ld/100 mm \n",size);
1780     return 10*size;
1781 }
1782
1783
1784 static DWORD
1785 _c_inch2size(PAGESETUPDLGA *dlga,DWORD size) {
1786     if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1787         return size;
1788     if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1789         return (size*254)/10;
1790     /* if we don't have a flag, we can choose one. Use millimeters
1791      * to avoid confusing me
1792      */
1793     dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1794     return (size*254)/10;
1795 }
1796
1797 static void
1798 _c_size2str(PageSetupData *pda,DWORD size,LPSTR strout) {
1799     strcpy(strout,"<undef>");
1800     if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1801         sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1802         return;
1803     }
1804     if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1805         sprintf(strout,"%.2fin",(size*1.0)/1000.0);
1806         return;
1807     }
1808     pda->dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1809     sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1810     return;
1811 }
1812
1813 static DWORD
1814 _c_str2size(PageSetupData *pda,LPCSTR strin) {
1815     float       val;
1816     char        rest[200];
1817
1818     rest[0]='\0';
1819     if (!sscanf(strin,"%f%s",&val,rest))
1820         return 0;
1821
1822     if (!strcmp(rest,"in") || !strcmp(rest,"inch")) {
1823         if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1824             return 1000*val;
1825         else
1826             return val*25.4*100;
1827     }
1828     if (!strcmp(rest,"cm")) { rest[0]='m'; val = val*10.0; }
1829     if (!strcmp(rest,"m")) { strcpy(rest,"mm"); val = val*1000.0; }
1830
1831     if (!strcmp(rest,"mm")) {
1832         if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1833             return 100*val;
1834         else
1835             return 1000.0*val/25.4;
1836     }
1837     if (rest[0]=='\0') {
1838         /* use application supplied default */
1839         if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1840             /* 100*mm */
1841             return 100.0*val;
1842         }
1843         if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1844             /* 1000*inch */
1845             return 1000.0*val;
1846         }
1847     }
1848     ERR("Did not find a conversion for type '%s'!\n",rest);
1849     return 0;
1850 }
1851
1852
1853 /*
1854  * This is called on finish and will update the output fields of the
1855  * struct.
1856  */
1857 static BOOL
1858 PRINTDLG_PS_UpdateDlgStruct(HWND hDlg, PageSetupData *pda) {
1859     DEVNAMES    *dn;
1860     DEVMODEA    *dm;
1861     LPSTR       devname,portname;
1862     char        papername[64];
1863     char        buf[200];
1864     
1865     dn = GlobalLock(pda->pdlg.hDevNames);
1866     dm = GlobalLock(pda->pdlg.hDevMode);
1867     devname     = ((char*)dn)+dn->wDeviceOffset;
1868     portname    = ((char*)dn)+dn->wOutputOffset;
1869     PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1870     PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1871
1872     if (GetDlgItemTextA(hDlg,cmb2,papername,sizeof(papername))>0) {
1873         PRINTDLG_PaperSize(&(pda->pdlg),papername,&(pda->dlga->ptPaperSize));
1874         pda->dlga->ptPaperSize.x = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.x);
1875         pda->dlga->ptPaperSize.y = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.y);
1876     } else
1877         FIXME("could not get dialog text for papersize cmbbox?\n");
1878 #define GETVAL(id,val) if (GetDlgItemTextA(hDlg,id,buf,sizeof(buf))>0) { val = _c_str2size(pda,buf); } else { FIXME("could not get dlgitemtexta for %x\n",id); }
1879     GETVAL(edt4,pda->dlga->rtMargin.left);
1880     GETVAL(edt5,pda->dlga->rtMargin.top);
1881     GETVAL(edt6,pda->dlga->rtMargin.right);
1882     GETVAL(edt7,pda->dlga->rtMargin.bottom);
1883 #undef GETVAL
1884
1885     /* If we are in landscape, swap x and y of page size */
1886     if (IsDlgButtonChecked(hDlg, rad2)) {
1887         DWORD tmp;
1888         tmp = pda->dlga->ptPaperSize.x;
1889         pda->dlga->ptPaperSize.x = pda->dlga->ptPaperSize.y;
1890         pda->dlga->ptPaperSize.y = tmp;
1891     }
1892     GlobalUnlock(pda->pdlg.hDevNames);
1893     GlobalUnlock(pda->pdlg.hDevMode);
1894     return TRUE;
1895 }
1896
1897 /*
1898  * This is called after returning from PrintDlg().
1899  */
1900 static BOOL
1901 PRINTDLG_PS_ChangePrinter(HWND hDlg, PageSetupData *pda) {
1902     DEVNAMES    *dn;
1903     DEVMODEA    *dm;
1904     LPSTR       devname,portname;
1905     
1906     dn = GlobalLock(pda->pdlg.hDevNames);
1907     dm = GlobalLock(pda->pdlg.hDevMode);
1908     devname     = ((char*)dn)+dn->wDeviceOffset;
1909     portname    = ((char*)dn)+dn->wOutputOffset;
1910     PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1911     PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1912     GlobalUnlock(pda->pdlg.hDevNames);
1913     GlobalUnlock(pda->pdlg.hDevMode);
1914     return TRUE;
1915 }
1916
1917 static BOOL
1918 PRINTDLG_PS_WMCommand(
1919     HWND hDlg, WPARAM wParam, LPARAM lParam, PageSetupData *pda
1920 ) {
1921     switch (LOWORD(wParam))  {
1922     case IDOK:
1923         if (!PRINTDLG_PS_UpdateDlgStruct(hDlg, pda))
1924             return(FALSE);
1925         EndDialog(hDlg, TRUE);
1926         return TRUE ;
1927
1928     case IDCANCEL:
1929         EndDialog(hDlg, FALSE);
1930         return FALSE ;
1931
1932     case psh3: {
1933         pda->pdlg.Flags         = 0;
1934         pda->pdlg.hwndOwner     = hDlg;
1935         if (PrintDlgA(&(pda->pdlg)))
1936             PRINTDLG_PS_ChangePrinter(hDlg,pda);
1937         return TRUE;
1938     }
1939     }
1940     FIXME("loword (lparam) %d, wparam 0x%x, lparam %08lx, STUB mostly.\n",
1941             LOWORD(lParam),wParam,lParam
1942     );
1943     return FALSE;
1944 }
1945
1946
1947 static BOOL WINAPI
1948 PageDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1949 {
1950     PageSetupData       *pda;
1951     BOOL                res = FALSE;
1952
1953     if (uMsg==WM_INITDIALOG) {
1954         res = TRUE;
1955         pda = (PageSetupData*)lParam;
1956         SetPropA(hDlg,"__WINE_PAGESETUPDLGDATA",lParam);
1957         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
1958             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
1959             if (!res) {
1960                 FIXME("Setup page hook failed?\n");
1961                 res = TRUE;
1962             }
1963         }
1964         if (pda->dlga->Flags & PSD_ENABLEPAGEPAINTHOOK) {
1965             FIXME("PagePaintHook not yet implemented!\n");
1966         }
1967         if (pda->dlga->Flags & PSD_DISABLEPRINTER)
1968             EnableWindow(GetDlgItem(hDlg, psh3), FALSE);    
1969         if (pda->dlga->Flags & PSD_DISABLEMARGINS) {
1970             EnableWindow(GetDlgItem(hDlg, edt4), FALSE);    
1971             EnableWindow(GetDlgItem(hDlg, edt5), FALSE);    
1972             EnableWindow(GetDlgItem(hDlg, edt6), FALSE);    
1973             EnableWindow(GetDlgItem(hDlg, edt7), FALSE);    
1974         }
1975         /* width larger as height -> landscape */
1976         if (pda->dlga->ptPaperSize.x > pda->dlga->ptPaperSize.y)
1977             CheckRadioButton(hDlg, rad1, rad2, rad2);
1978         else /* this is default if papersize is not set */
1979             CheckRadioButton(hDlg, rad1, rad2, rad1);
1980         if (pda->dlga->Flags & PSD_DISABLEORIENTATION) {
1981             EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
1982             EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
1983         }
1984         /* We fill them out enabled or not */
1985         if (pda->dlga->Flags & PSD_MARGINS) {
1986             char str[100];
1987             _c_size2str(pda,pda->dlga->rtMargin.left,str);
1988             SetDlgItemTextA(hDlg,edt4,str);
1989             _c_size2str(pda,pda->dlga->rtMargin.top,str);
1990             SetDlgItemTextA(hDlg,edt5,str);
1991             _c_size2str(pda,pda->dlga->rtMargin.right,str);
1992             SetDlgItemTextA(hDlg,edt6,str);
1993             _c_size2str(pda,pda->dlga->rtMargin.bottom,str);
1994             SetDlgItemTextA(hDlg,edt7,str);
1995         } else {
1996             /* default is 1 inch */
1997             DWORD size = _c_inch2size(pda->dlga,1000);
1998             char        str[20];
1999             _c_size2str(pda,size,str);
2000             SetDlgItemTextA(hDlg,edt4,str);
2001             SetDlgItemTextA(hDlg,edt5,str);
2002             SetDlgItemTextA(hDlg,edt6,str);
2003             SetDlgItemTextA(hDlg,edt7,str);
2004         }
2005         PRINTDLG_PS_ChangePrinter(hDlg,pda);
2006         if (pda->dlga->Flags & PSD_DISABLEPAPER) {
2007             EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
2008             EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
2009         }
2010         return TRUE;
2011     } else {
2012         pda = (PageSetupData*)GetPropA(hDlg,"__WINE_PAGESETUPDLGDATA");
2013         if (!pda) {
2014             WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
2015             return FALSE;
2016         }
2017         if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2018             res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
2019             if (res) return res;
2020         }
2021     }
2022     switch (uMsg) {
2023     case WM_COMMAND:
2024         return PRINTDLG_PS_WMCommand(hDlg, wParam, lParam, pda);
2025     }
2026     return FALSE;
2027 }
2028
2029 /***********************************************************************
2030  *            PageSetupDlgA  (COMDLG32.@)
2031  */
2032 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg) {
2033     HGLOBAL             hDlgTmpl;
2034     LPVOID              ptr;
2035     BOOL                bRet;
2036     PageSetupData       *pda;
2037     PRINTDLGA           pdlg;
2038
2039     if(TRACE_ON(commdlg)) {
2040         char flagstr[1000] = "";
2041         struct pd_flags *pflag = psd_flags;
2042         for( ; pflag->name; pflag++) {
2043             if(setupdlg->Flags & pflag->flag) {
2044                 strcat(flagstr, pflag->name);
2045                 strcat(flagstr, "|");
2046             }
2047         }
2048         TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
2049               "hinst %08x, flags %08lx (%s)\n",
2050               setupdlg, setupdlg->hwndOwner, setupdlg->hDevMode,
2051               setupdlg->hDevNames, 
2052               setupdlg->hInstance, setupdlg->Flags, flagstr);
2053     }
2054
2055     /* First get default printer data, we need it right after that. */
2056     memset(&pdlg,0,sizeof(pdlg));
2057     pdlg.lStructSize    = sizeof(pdlg);
2058     pdlg.Flags          = PD_RETURNDEFAULT;
2059     bRet = PrintDlgA(&pdlg);
2060     if (!bRet) return FALSE;
2061
2062     /* short cut exit, just return default values */
2063     if (setupdlg->Flags & PSD_RETURNDEFAULT) {
2064         setupdlg->hDevMode      = pdlg.hDevMode;
2065         setupdlg->hDevNames     = pdlg.hDevNames;
2066         /* FIXME: Just return "A4" for now. */
2067         PRINTDLG_PaperSize(&pdlg,"A4",&setupdlg->ptPaperSize);
2068         setupdlg->ptPaperSize.x=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.x);
2069         setupdlg->ptPaperSize.y=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.y);
2070         return TRUE;
2071     }
2072     hDlgTmpl = PRINTDLG_GetPGSTemplate(setupdlg);
2073     if (!hDlgTmpl) {
2074         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2075         return FALSE;
2076     }
2077     ptr = LockResource( hDlgTmpl );
2078     if (!ptr) {
2079         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2080         return FALSE;
2081     }
2082     pda = HeapAlloc(GetProcessHeap(),0,sizeof(*pda));
2083     pda->dlga = setupdlg;
2084     memcpy(&pda->pdlg,&pdlg,sizeof(pdlg));
2085
2086     bRet = (0<DialogBoxIndirectParamA(
2087                 setupdlg->hInstance,
2088                 ptr,
2089                 setupdlg->hwndOwner,
2090                 PageDlgProcA,
2091                 (LPARAM)pda)
2092     );
2093     return bRet;
2094 }
2095 /***********************************************************************
2096  *            PageSetupDlgW  (COMDLG32.@)
2097  */
2098 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg) {
2099         FIXME("(%p), stub!\n",setupdlg);
2100         return FALSE;
2101 }
2102
2103 /**********************************************************************
2104  *
2105  *      16 bit commdlg
2106  */
2107
2108 /***********************************************************************
2109  *           PrintDlgProc   (COMMDLG.21)
2110  */
2111 LRESULT WINAPI PrintDlgProc16(HWND16 hDlg, UINT16 uMsg, WPARAM16 wParam,
2112                             LPARAM lParam)
2113 {
2114     PRINT_PTRA* PrintStructures;
2115     LRESULT res=FALSE;
2116
2117     if (uMsg!=WM_INITDIALOG) {
2118         PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);   
2119         if (!PrintStructures)
2120             return FALSE;
2121     } else {
2122         PrintStructures = (PRINT_PTRA*) lParam;
2123         SetWindowLongA(hDlg, DWL_USER, lParam); 
2124         res = PRINTDLG_WMInitDialog16(hDlg, wParam, PrintStructures);
2125
2126         if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2127             res = CallWindowProc16(
2128                 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2129                 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg16
2130             );
2131         }
2132         return res;
2133     }
2134   
2135     if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2136         res = CallWindowProc16(
2137                 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2138                 hDlg,uMsg, wParam, lParam
2139         );
2140         if(LOWORD(res)) return res;
2141     }
2142
2143     switch (uMsg) {
2144     case WM_COMMAND: {
2145          /* We need to map those for the 32bit window procedure, compare
2146           * with 32Ato16 mapper in winproc.c
2147           */
2148         return PRINTDLG_WMCommand(
2149                 hDlg,
2150                 MAKEWPARAM(wParam,HIWORD(lParam)),
2151                 LOWORD(lParam),
2152                 PrintStructures
2153         );
2154     }
2155     case WM_DESTROY:
2156         DestroyIcon(PrintStructures->hCollateIcon);
2157         DestroyIcon(PrintStructures->hNoCollateIcon);
2158     /* FIXME: don't forget to delete the paper orientation icons here! */
2159
2160         return FALSE;
2161     }    
2162     return res;
2163 }
2164
2165
2166 /***********************************************************************
2167  *           PrintSetupDlgProc   (COMMDLG.22)
2168  */
2169 LRESULT WINAPI PrintSetupDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
2170                                    LPARAM lParam)
2171 {
2172   switch (wMsg)
2173     {
2174     case WM_INITDIALOG:
2175       TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
2176       ShowWindow(hWnd, SW_SHOWNORMAL);
2177       return (TRUE);
2178     case WM_COMMAND:
2179       switch (wParam) {
2180       case IDOK:
2181         EndDialog(hWnd, TRUE);
2182         return(TRUE);
2183       case IDCANCEL:
2184         EndDialog(hWnd, FALSE);
2185         return(TRUE);
2186       }
2187       return(FALSE);
2188     }
2189   return FALSE;
2190 }
2191
2192
2193 /***********************************************************************
2194  *      PrintDlgExA (COMDLG32.@)
2195  */
2196 HRESULT WINAPI PrintDlgExA(LPVOID lpPrintDlgExA) /* [???] FIXME: LPPRINTDLGEXA */
2197 {
2198         FIXME("stub\n");
2199         return E_NOTIMPL;
2200 }
2201 /***********************************************************************
2202  *      PrintDlgExW (COMDLG32.@)
2203  */
2204 HRESULT WINAPI PrintDlgExW(LPVOID lpPrintDlgExW) /* [???] FIXME: LPPRINTDLGEXW */
2205 {
2206         FIXME("stub\n");
2207         return E_NOTIMPL;
2208 }