~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/comdlg32/printdlg.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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