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