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