winegstreamer: Implement ProcessSample for media sink.
[wine.git] / dlls / comdlg32 / printdlg.c
blob700119bc9fffda144faa379c1de489ad63c69c9e
1 /*
2 * COMMDLG - Print Dialog
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1999 Klaas van Gend
7 * Copyright 2000 Huw D M Davies
8 * Copyright 2010 Vitaly Perov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winspool.h"
37 #include "winerror.h"
38 #include "objbase.h"
39 #include "commdlg.h"
41 #include "wine/debug.h"
43 #include "dlgs.h"
44 #include "cderr.h"
45 #include "cdlg.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
49 /* Yes these constants are the same, but we're just copying win98 */
50 #define UPDOWN_ID 0x270f
51 #define MAX_COPIES 9999
53 /* This PRINTDLGA internal structure stores
54 * pointers to several throughout useful structures.
57 typedef struct
59 LPDEVMODEA lpDevMode;
60 LPPRINTDLGA lpPrintDlg;
61 LPPRINTER_INFO_2A lpPrinterInfo;
62 LPDRIVER_INFO_3A lpDriverInfo;
63 UINT HelpMessageID;
64 HICON hCollateIcon; /* PrintDlg only */
65 HICON hNoCollateIcon; /* PrintDlg only */
66 HICON hPortraitIcon; /* PrintSetupDlg only */
67 HICON hLandscapeIcon; /* PrintSetupDlg only */
68 HWND hwndUpDown;
69 } PRINT_PTRA;
71 typedef struct
73 LPDEVMODEW lpDevMode;
74 LPPRINTDLGW lpPrintDlg;
75 LPPRINTER_INFO_2W lpPrinterInfo;
76 LPDRIVER_INFO_3W lpDriverInfo;
77 UINT HelpMessageID;
78 HICON hCollateIcon; /* PrintDlg only */
79 HICON hNoCollateIcon; /* PrintDlg only */
80 HICON hPortraitIcon; /* PrintSetupDlg only */
81 HICON hLandscapeIcon; /* PrintSetupDlg only */
82 HWND hwndUpDown;
83 } PRINT_PTRW;
85 /* Debugging info */
86 struct pd_flags
88 DWORD flag;
89 LPCSTR name;
92 static const struct pd_flags psd_flags[] = {
93 {PSD_MINMARGINS,"PSD_MINMARGINS"},
94 {PSD_MARGINS,"PSD_MARGINS"},
95 {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
96 {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
97 {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
98 {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
99 {PSD_NOWARNING,"PSD_NOWARNING"},
100 {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
101 {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
102 {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
103 {PSD_SHOWHELP,"PSD_SHOWHELP"},
104 {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
105 {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
106 {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
107 {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
108 {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
109 {-1, NULL}
112 static const struct pd_flags pd_flags[] = {
113 {PD_SELECTION, "PD_SELECTION "},
114 {PD_PAGENUMS, "PD_PAGENUMS "},
115 {PD_NOSELECTION, "PD_NOSELECTION "},
116 {PD_NOPAGENUMS, "PD_NOPAGENUMS "},
117 {PD_COLLATE, "PD_COLLATE "},
118 {PD_PRINTTOFILE, "PD_PRINTTOFILE "},
119 {PD_PRINTSETUP, "PD_PRINTSETUP "},
120 {PD_NOWARNING, "PD_NOWARNING "},
121 {PD_RETURNDC, "PD_RETURNDC "},
122 {PD_RETURNIC, "PD_RETURNIC "},
123 {PD_RETURNDEFAULT, "PD_RETURNDEFAULT "},
124 {PD_SHOWHELP, "PD_SHOWHELP "},
125 {PD_ENABLEPRINTHOOK, "PD_ENABLEPRINTHOOK "},
126 {PD_ENABLESETUPHOOK, "PD_ENABLESETUPHOOK "},
127 {PD_ENABLEPRINTTEMPLATE, "PD_ENABLEPRINTTEMPLATE "},
128 {PD_ENABLESETUPTEMPLATE, "PD_ENABLESETUPTEMPLATE "},
129 {PD_ENABLEPRINTTEMPLATEHANDLE, "PD_ENABLEPRINTTEMPLATEHANDLE "},
130 {PD_ENABLESETUPTEMPLATEHANDLE, "PD_ENABLESETUPTEMPLATEHANDLE "},
131 {PD_USEDEVMODECOPIES, "PD_USEDEVMODECOPIES[ANDCOLLATE] "},
132 {PD_DISABLEPRINTTOFILE, "PD_DISABLEPRINTTOFILE "},
133 {PD_HIDEPRINTTOFILE, "PD_HIDEPRINTTOFILE "},
134 {PD_NONETWORKBUTTON, "PD_NONETWORKBUTTON "},
135 {-1, NULL}
137 /* address of wndproc for subclassed Static control */
138 static WNDPROC lpfnStaticWndProc;
139 static WNDPROC edit_wndproc;
140 /* the text of the fake document to render for the Page Setup dialog */
141 static WCHAR wszFakeDocumentText[1024];
142 static const WCHAR printdlg_prop[] = L"__WINE_PRINTDLGDATA";
143 static const WCHAR pagesetupdlg_prop[] = L"__WINE_PAGESETUPDLGDATA";
146 /***********************************************************************
147 * get_driver_info [internal]
149 * get DRIVER_INFO_3W for the current printer handle,
150 * alloc the buffer, when needed
152 static DRIVER_INFO_3W * get_driver_infoW(HANDLE hprn)
154 DRIVER_INFO_3W *di3 = NULL;
155 DWORD needed = 0;
156 BOOL res;
158 res = GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
159 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
160 di3 = malloc(needed);
161 res = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)di3, needed, &needed);
164 if (res)
165 return di3;
167 TRACE("GetPrinterDriverW failed with %lu\n", GetLastError());
168 free(di3);
169 return NULL;
172 static DRIVER_INFO_3A * get_driver_infoA(HANDLE hprn)
174 DRIVER_INFO_3A *di3 = NULL;
175 DWORD needed = 0;
176 BOOL res;
178 res = GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
179 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
180 di3 = malloc(needed);
181 res = GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)di3, needed, &needed);
184 if (res)
185 return di3;
187 TRACE("GetPrinterDriverA failed with %lu\n", GetLastError());
188 free(di3);
189 return NULL;
193 /***********************************************************************
194 * get_printer_info [internal]
196 * get PRINTER_INFO_2W for the current printer handle,
197 * alloc the buffer, when needed
199 static PRINTER_INFO_2W * get_printer_infoW(HANDLE hprn)
201 PRINTER_INFO_2W *pi2 = NULL;
202 DWORD needed = 0;
203 BOOL res;
205 res = GetPrinterW(hprn, 2, NULL, 0, &needed);
206 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
207 pi2 = malloc(needed);
208 res = GetPrinterW(hprn, 2, (LPBYTE)pi2, needed, &needed);
211 if (res)
212 return pi2;
214 TRACE("GetPrinterW failed with %lu\n", GetLastError());
215 free(pi2);
216 return NULL;
219 static PRINTER_INFO_2A * get_printer_infoA(HANDLE hprn)
221 PRINTER_INFO_2A *pi2 = NULL;
222 DWORD needed = 0;
223 BOOL res;
225 res = GetPrinterA(hprn, 2, NULL, 0, &needed);
226 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
227 pi2 = malloc(needed);
228 res = GetPrinterA(hprn, 2, (LPBYTE)pi2, needed, &needed);
231 if (res)
232 return pi2;
234 TRACE("GetPrinterA failed with %lu\n", GetLastError());
235 free(pi2);
236 return NULL;
240 /***********************************************************************
241 * update_devmode_handle [internal]
243 * update a devmode handle for the given DEVMODE, alloc the buffer, when needed
245 static HGLOBAL update_devmode_handleW(HGLOBAL hdm, DEVMODEW *dm)
247 SIZE_T size = GlobalSize(hdm);
248 LPVOID ptr;
250 /* Increase / alloc the global memory block, when needed */
251 if ((dm->dmSize + dm->dmDriverExtra) > size) {
252 if (hdm)
253 hdm = GlobalReAlloc(hdm, dm->dmSize + dm->dmDriverExtra, GMEM_MOVEABLE);
254 else
255 hdm = GlobalAlloc(GMEM_MOVEABLE, dm->dmSize + dm->dmDriverExtra);
258 if (hdm) {
259 ptr = GlobalLock(hdm);
260 if (ptr) {
261 memcpy(ptr, dm, dm->dmSize + dm->dmDriverExtra);
262 GlobalUnlock(hdm);
264 else
266 GlobalFree(hdm);
267 hdm = NULL;
270 return hdm;
273 static HGLOBAL update_devmode_handleA(HGLOBAL hdm, DEVMODEA *dm)
275 SIZE_T size = GlobalSize(hdm);
276 LPVOID ptr;
278 /* Increase / alloc the global memory block, when needed */
279 if ((dm->dmSize + dm->dmDriverExtra) > size) {
280 if (hdm)
281 hdm = GlobalReAlloc(hdm, dm->dmSize + dm->dmDriverExtra, GMEM_MOVEABLE);
282 else
283 hdm = GlobalAlloc(GMEM_MOVEABLE, dm->dmSize + dm->dmDriverExtra);
286 if (hdm) {
287 ptr = GlobalLock(hdm);
288 if (ptr) {
289 memcpy(ptr, dm, dm->dmSize + dm->dmDriverExtra);
290 GlobalUnlock(hdm);
292 else
294 GlobalFree(hdm);
295 hdm = NULL;
298 return hdm;
301 /***********************************************************
302 * convert_to_devmodeA
304 * Creates an ansi copy of supplied devmode
306 static DEVMODEA *convert_to_devmodeA(const DEVMODEW *dmW)
308 DEVMODEA *dmA;
309 DWORD size;
311 if (!dmW) return NULL;
312 size = dmW->dmSize - CCHDEVICENAME -
313 ((dmW->dmSize > FIELD_OFFSET(DEVMODEW, dmFormName)) ? CCHFORMNAME : 0);
315 dmA = malloc(size + dmW->dmDriverExtra);
316 if (!dmA) return NULL;
318 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1,
319 (LPSTR)dmA->dmDeviceName, CCHDEVICENAME, NULL, NULL);
321 if (FIELD_OFFSET(DEVMODEW, dmFormName) >= dmW->dmSize)
323 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
324 dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
326 else
328 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
329 FIELD_OFFSET(DEVMODEW, dmFormName) - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
330 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1,
331 (LPSTR)dmA->dmFormName, CCHFORMNAME, NULL, NULL);
333 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmLogPixels));
336 dmA->dmSize = size;
337 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize, dmW->dmDriverExtra);
338 return dmA;
341 /***********************************************************************
342 * PRINTDLG_OpenDefaultPrinter
344 * Returns a winspool printer handle to the default printer in *hprn
345 * Caller must call ClosePrinter on the handle
347 * Returns TRUE on success else FALSE
349 static BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
351 WCHAR buf[260];
352 DWORD dwBufLen = ARRAY_SIZE(buf);
353 BOOL res;
354 if(!GetDefaultPrinterW(buf, &dwBufLen))
355 return FALSE;
356 res = OpenPrinterW(buf, hprn, NULL);
357 if (!res)
358 WARN("Could not open printer %s\n", debugstr_w(buf));
359 return res;
362 /***********************************************************************
363 * PRINTDLG_SetUpPrinterListCombo
365 * Initializes printer list combox.
366 * hDlg: HWND of dialog
367 * id: Control id of combo
368 * name: Name of printer to select
370 * Initializes combo with list of available printers. Selects printer 'name'
371 * If name is NULL or does not exist select the default printer.
373 * Returns number of printers added to list.
375 static INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
377 DWORD needed, num;
378 INT i;
379 LPPRINTER_INFO_2A pi;
380 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
381 pi = malloc(needed);
382 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
383 &num);
385 SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
387 for(i = 0; i < num; i++) {
388 SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
389 (LPARAM)pi[i].pPrinterName );
391 free(pi);
392 if(!name ||
393 (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
394 (LPARAM)name)) == CB_ERR) {
396 char buf[260];
397 DWORD dwBufLen = ARRAY_SIZE(buf);
398 if (name != NULL)
399 WARN("Can't find %s in printer list so trying to find default\n",
400 debugstr_a(name));
401 if(!GetDefaultPrinterA(buf, &dwBufLen))
402 return num;
403 i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
404 if(i == CB_ERR)
405 FIXME("Can't find default printer in printer list\n");
407 SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
408 return num;
411 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
413 DWORD needed, num;
414 INT i;
415 LPPRINTER_INFO_2W pi;
416 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
417 pi = malloc(needed);
418 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
419 &num);
421 for(i = 0; i < num; i++) {
422 SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
423 (LPARAM)pi[i].pPrinterName );
425 free(pi);
426 if(!name ||
427 (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
428 (LPARAM)name)) == CB_ERR) {
429 WCHAR buf[260];
430 DWORD dwBufLen = ARRAY_SIZE(buf);
431 if (name != NULL)
432 WARN("Can't find %s in printer list so trying to find default\n",
433 debugstr_w(name));
434 if(!GetDefaultPrinterW(buf, &dwBufLen))
435 return num;
436 i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
437 if(i == CB_ERR)
438 TRACE("Can't find default printer in printer list\n");
440 SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
441 return num;
444 /***********************************************************************
445 * PRINTDLG_CreateDevNames [internal]
448 * creates a DevNames structure.
450 * (NB. when we handle unicode the offsets will be in wchars).
452 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
453 const char* DeviceName, const char* OutputPort)
455 long size;
456 char* pDevNamesSpace;
457 char* pTempPtr;
458 LPDEVNAMES lpDevNames;
459 char buf[260];
460 DWORD dwBufLen = ARRAY_SIZE(buf);
461 const char *p;
463 p = strrchr( DeviceDriverName, '\\' );
464 if (p) DeviceDriverName = p + 1;
466 size = strlen(DeviceDriverName) + 1
467 + strlen(DeviceName) + 1
468 + strlen(OutputPort) + 1
469 + sizeof(DEVNAMES);
471 if(*hmem)
472 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
473 else
474 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
475 if (*hmem == 0)
476 return FALSE;
478 pDevNamesSpace = GlobalLock(*hmem);
479 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
481 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
482 strcpy(pTempPtr, DeviceDriverName);
483 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
485 pTempPtr += strlen(DeviceDriverName) + 1;
486 strcpy(pTempPtr, DeviceName);
487 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
489 pTempPtr += strlen(DeviceName) + 1;
490 strcpy(pTempPtr, OutputPort);
491 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
493 GetDefaultPrinterA(buf, &dwBufLen);
494 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
495 GlobalUnlock(*hmem);
496 return TRUE;
499 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
500 LPCWSTR DeviceName, LPCWSTR OutputPort)
502 long size;
503 LPWSTR pDevNamesSpace;
504 LPWSTR pTempPtr;
505 LPDEVNAMES lpDevNames;
506 WCHAR bufW[260];
507 DWORD dwBufLen = ARRAY_SIZE(bufW);
508 const WCHAR *p;
510 p = wcsrchr( DeviceDriverName, '\\' );
511 if (p) DeviceDriverName = p + 1;
513 size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
514 + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
515 + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
516 + sizeof(DEVNAMES);
518 if(*hmem)
519 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
520 else
521 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
522 if (*hmem == 0)
523 return FALSE;
525 pDevNamesSpace = GlobalLock(*hmem);
526 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
528 pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
529 lstrcpyW(pTempPtr, DeviceDriverName);
530 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
532 pTempPtr += lstrlenW(DeviceDriverName) + 1;
533 lstrcpyW(pTempPtr, DeviceName);
534 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
536 pTempPtr += lstrlenW(DeviceName) + 1;
537 lstrcpyW(pTempPtr, OutputPort);
538 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
540 GetDefaultPrinterW(bufW, &dwBufLen);
541 lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
542 GlobalUnlock(*hmem);
543 return TRUE;
546 /***********************************************************************
547 * PRINTDLG_UpdatePrintDlg [internal]
550 * updates the PrintDlg structure for return values.
552 * RETURNS
553 * FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
554 * TRUE if successful.
556 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
557 PRINT_PTRA* PrintStructures)
559 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
560 PDEVMODEA lpdm = PrintStructures->lpDevMode;
561 LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
564 if(!lpdm) {
565 FIXME("No lpdm ptr?\n");
566 return FALSE;
570 if(!(lppd->Flags & PD_PRINTSETUP)) {
571 /* check whether nFromPage and nToPage are within range defined by
572 * nMinPage and nMaxPage
574 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
575 WORD nToPage;
576 WORD nFromPage;
577 BOOL translated;
578 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
579 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
581 /* if no ToPage value is entered, use the FromPage value */
582 if(!translated) nToPage = nFromPage;
584 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
585 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
586 WCHAR resourcestr[256];
587 WCHAR resultstr[256];
588 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
589 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
590 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
591 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
592 return FALSE;
594 lppd->nFromPage = nFromPage;
595 lppd->nToPage = nToPage;
596 lppd->Flags |= PD_PAGENUMS;
598 else
599 lppd->Flags &= ~PD_PAGENUMS;
601 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
602 lppd->Flags |= PD_SELECTION;
603 else
604 lppd->Flags &= ~PD_SELECTION;
606 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
607 static char file[] = "FILE:";
608 lppd->Flags |= PD_PRINTTOFILE;
609 pi->pPortName = file;
612 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
613 FIXME("Collate lppd not yet implemented as output\n");
616 /* set PD_Collate and nCopies */
617 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
618 /* The application doesn't support multiple copies or collate...
620 lppd->Flags &= ~PD_COLLATE;
621 lppd->nCopies = 1;
622 /* if the printer driver supports it... store info there
623 * otherwise no collate & multiple copies !
625 if (lpdm->dmFields & DM_COLLATE)
626 lpdm->dmCollate =
627 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
628 if (lpdm->dmFields & DM_COPIES)
629 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
630 } else {
631 /* Application is responsible for multiple copies */
632 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
633 lppd->Flags |= PD_COLLATE;
634 else
635 lppd->Flags &= ~PD_COLLATE;
636 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
637 /* multiple copies already included in the document. Driver must print only one copy */
638 lpdm->dmCopies = 1;
641 /* Print quality, PrintDlg16 */
642 if(GetDlgItem(hDlg, cmb1))
644 HWND hQuality = GetDlgItem(hDlg, cmb1);
645 int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
647 if(Sel != CB_ERR)
649 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
650 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
651 lpdm->dmPrintQuality = LOWORD(dpi);
652 lpdm->dmYResolution = HIWORD(dpi);
656 return TRUE;
659 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
660 PRINT_PTRW* PrintStructures)
662 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
663 PDEVMODEW lpdm = PrintStructures->lpDevMode;
664 LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
667 if(!lpdm) {
668 FIXME("No lpdm ptr?\n");
669 return FALSE;
673 if(!(lppd->Flags & PD_PRINTSETUP)) {
674 /* check whether nFromPage and nToPage are within range defined by
675 * nMinPage and nMaxPage
677 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
678 WORD nToPage;
679 WORD nFromPage;
680 BOOL translated;
681 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
682 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
684 /* if no ToPage value is entered, use the FromPage value */
685 if(!translated) nToPage = nFromPage;
687 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
688 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
689 WCHAR resourcestr[256];
690 WCHAR resultstr[256];
691 DWORD_PTR args[2];
692 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
693 resourcestr, 255);
694 args[0] = lppd->nMinPage;
695 args[1] = lppd->nMaxPage;
696 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
697 resourcestr, 0, 0, resultstr, ARRAY_SIZE(resultstr), (va_list *)args);
698 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
699 resourcestr, 255);
700 MessageBoxW(hDlg, resultstr, resourcestr,
701 MB_OK | MB_ICONWARNING);
702 return FALSE;
704 lppd->nFromPage = nFromPage;
705 lppd->nToPage = nToPage;
706 lppd->Flags |= PD_PAGENUMS;
708 else
709 lppd->Flags &= ~PD_PAGENUMS;
711 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
712 lppd->Flags |= PD_SELECTION;
713 else
714 lppd->Flags &= ~PD_SELECTION;
716 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
717 static WCHAR file[] = L"FILE:";
718 lppd->Flags |= PD_PRINTTOFILE;
719 pi->pPortName = file;
722 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
723 FIXME("Collate lppd not yet implemented as output\n");
726 /* set PD_Collate and nCopies */
727 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
728 /* The application doesn't support multiple copies or collate...
730 lppd->Flags &= ~PD_COLLATE;
731 lppd->nCopies = 1;
732 /* if the printer driver supports it... store info there
733 * otherwise no collate & multiple copies !
735 if (lpdm->dmFields & DM_COLLATE)
736 lpdm->dmCollate =
737 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
738 if (lpdm->dmFields & DM_COPIES)
739 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
740 } else {
741 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
742 lppd->Flags |= PD_COLLATE;
743 else
744 lppd->Flags &= ~PD_COLLATE;
745 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
748 return TRUE;
751 /************************************************************************
752 * PRINTDLG_SetUpPaperComboBox
754 * Initialize either the papersize or inputslot combos of the Printer Setup
755 * dialog. We store the associated word (eg DMPAPER_A4) as the item data.
756 * We also try to re-select the old selection.
758 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
759 int nIDComboBox,
760 char* PrinterName,
761 char* PortName,
762 LPDEVMODEA dm)
764 int i;
765 int NrOfEntries;
766 char* Names;
767 WORD* Words;
768 DWORD Sel, old_Sel;
769 WORD oldWord = 0, newWord = 0; /* DMPAPER_ and DMBIN_ start at 1 */
770 int NamesSize;
771 int fwCapability_Names;
772 int fwCapability_Words;
774 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
776 /* query the dialog box for the current selected value */
777 Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
778 if(Sel != CB_ERR) {
779 /* we enter here only if a different printer is selected after
780 * the Print Setup dialog is opened. The current settings are
781 * stored into the newly selected printer.
783 oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
784 Sel, 0);
785 if(oldWord >= DMPAPER_USER) /* DMPAPER_USER == DMBIN_USER */
786 oldWord = 0; /* There's no point in trying to keep custom
787 paper / bin sizes across printers */
790 if (dm)
791 newWord = (nIDComboBox == cmb2) ? dm->dmPaperSize : dm->dmDefaultSource;
793 if (nIDComboBox == cmb2) {
794 NamesSize = 64;
795 fwCapability_Names = DC_PAPERNAMES;
796 fwCapability_Words = DC_PAPERS;
797 } else {
798 nIDComboBox = cmb3;
799 NamesSize = 24;
800 fwCapability_Names = DC_BINNAMES;
801 fwCapability_Words = DC_BINS;
804 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
805 fwCapability_Names, NULL, dm);
806 if (NrOfEntries == 0)
807 WARN("no Name Entries found!\n");
808 else if (NrOfEntries < 0)
809 return FALSE;
811 if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
812 != NrOfEntries) {
813 ERR("Number of caps is different\n");
814 NrOfEntries = 0;
817 Names = malloc(NrOfEntries * sizeof(char) * NamesSize);
818 Words = malloc(NrOfEntries * sizeof(WORD));
819 DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Names, Names, dm);
820 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
821 fwCapability_Words, (LPSTR)Words, dm);
823 /* reset any current content in the combobox */
824 SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
826 /* store new content */
827 for (i = 0; i < NrOfEntries; i++) {
828 DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
829 (LPARAM)(&Names[i*NamesSize]) );
830 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
831 Words[i]);
834 /* Look for old selection or the new default.
835 Can't do this is previous loop since item order will change as more items are added */
836 Sel = 0;
837 old_Sel = NrOfEntries;
838 for (i = 0; i < NrOfEntries; i++) {
839 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
840 oldWord) {
841 old_Sel = i;
842 break;
844 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) == newWord)
845 Sel = i;
848 if(old_Sel < NrOfEntries)
850 if (dm)
852 if(nIDComboBox == cmb2)
853 dm->dmPaperSize = oldWord;
854 else
855 dm->dmDefaultSource = oldWord;
857 Sel = old_Sel;
860 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
862 free(Words);
863 free(Names);
864 return TRUE;
867 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
868 int nIDComboBox,
869 const WCHAR* PrinterName,
870 const WCHAR* PortName,
871 LPDEVMODEW dm)
873 int i;
874 int NrOfEntries;
875 WCHAR* Names;
876 WORD* Words;
877 DWORD Sel, old_Sel;
878 WORD oldWord = 0, newWord = 0; /* DMPAPER_ and DMBIN_ start at 1 */
879 int NamesSize;
880 int fwCapability_Names;
881 int fwCapability_Words;
883 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
885 /* query the dialog box for the current selected value */
886 Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
887 if(Sel != CB_ERR) {
888 /* we enter here only if a different printer is selected after
889 * the Print Setup dialog is opened. The current settings are
890 * stored into the newly selected printer.
892 oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
893 Sel, 0);
895 if(oldWord >= DMPAPER_USER) /* DMPAPER_USER == DMBIN_USER */
896 oldWord = 0; /* There's no point in trying to keep custom
897 paper / bin sizes across printers */
900 if (dm)
901 newWord = (nIDComboBox == cmb2) ? dm->dmPaperSize : dm->dmDefaultSource;
903 if (nIDComboBox == cmb2) {
904 NamesSize = 64;
905 fwCapability_Names = DC_PAPERNAMES;
906 fwCapability_Words = DC_PAPERS;
907 } else {
908 nIDComboBox = cmb3;
909 NamesSize = 24;
910 fwCapability_Names = DC_BINNAMES;
911 fwCapability_Words = DC_BINS;
914 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
915 fwCapability_Names, NULL, dm);
916 if (NrOfEntries == 0)
917 WARN("no Name Entries found!\n");
918 else if (NrOfEntries < 0)
919 return FALSE;
921 if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
922 != NrOfEntries) {
923 ERR("Number of caps is different\n");
924 NrOfEntries = 0;
927 Names = malloc(NrOfEntries * sizeof(WCHAR) * NamesSize);
928 Words = malloc(NrOfEntries * sizeof(WORD));
929 DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Names, Names, dm);
930 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
931 fwCapability_Words, Words, dm);
933 /* reset any current content in the combobox */
934 SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
936 /* store new content */
937 for (i = 0; i < NrOfEntries; i++) {
938 DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
939 (LPARAM)(&Names[i*NamesSize]) );
940 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
941 Words[i]);
944 /* Look for old selection or the new default.
945 Can't do this is previous loop since item order will change as more items are added */
946 Sel = 0;
947 old_Sel = NrOfEntries;
948 for (i = 0; i < NrOfEntries; i++) {
949 if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
950 oldWord) {
951 old_Sel = i;
952 break;
954 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) == newWord)
955 Sel = i;
958 if(old_Sel < NrOfEntries)
960 if (dm)
962 if(nIDComboBox == cmb2)
963 dm->dmPaperSize = oldWord;
964 else
965 dm->dmDefaultSource = oldWord;
967 Sel = old_Sel;
970 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
972 free(Words);
973 free(Names);
974 return TRUE;
978 /***********************************************************************
979 * PRINTDLG_UpdatePrinterInfoTexts [internal]
981 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
983 char StatusMsg[256];
984 char ResourceString[256];
985 int i;
987 /* Status Message */
988 StatusMsg[0]='\0';
990 /* add all status messages */
991 for (i = 0; i < 25; i++) {
992 if (pi->Status & (1<<i)) {
993 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
994 ResourceString, 255);
995 strcat(StatusMsg,ResourceString);
998 /* append "ready" */
999 /* FIXME: status==ready must only be appended if really so.
1000 but how to detect? */
1001 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
1002 ResourceString, 255);
1003 strcat(StatusMsg,ResourceString);
1004 SetDlgItemTextA(hDlg, stc12, StatusMsg);
1006 /* set all other printer info texts */
1007 SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
1009 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
1010 SetDlgItemTextA(hDlg, stc14, pi->pLocation);
1011 else
1012 SetDlgItemTextA(hDlg, stc14, pi->pPortName);
1013 SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
1014 return;
1017 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
1019 WCHAR StatusMsg[256];
1020 WCHAR ResourceString[256];
1021 int i;
1023 /* Status Message */
1024 StatusMsg[0]='\0';
1026 /* add all status messages */
1027 for (i = 0; i < 25; i++) {
1028 if (pi->Status & (1<<i)) {
1029 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
1030 ResourceString, 255);
1031 lstrcatW(StatusMsg,ResourceString);
1034 /* append "ready" */
1035 /* FIXME: status==ready must only be appended if really so.
1036 but how to detect? */
1037 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
1038 ResourceString, 255);
1039 lstrcatW(StatusMsg,ResourceString);
1040 SetDlgItemTextW(hDlg, stc12, StatusMsg);
1042 /* set all other printer info texts */
1043 SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
1044 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
1045 SetDlgItemTextW(hDlg, stc14, pi->pLocation);
1046 else
1047 SetDlgItemTextW(hDlg, stc14, pi->pPortName);
1048 SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : L"");
1052 /*******************************************************************
1054 * PRINTDLG_ChangePrinter
1057 static BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name, PRINT_PTRA *PrintStructures)
1059 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1060 LPDEVMODEA lpdm = NULL;
1061 LONG dmSize;
1062 DWORD needed;
1063 HANDLE hprn;
1065 free(PrintStructures->lpPrinterInfo);
1066 free(PrintStructures->lpDriverInfo);
1067 if(!OpenPrinterA(name, &hprn, NULL)) {
1068 ERR("Can't open printer %s\n", name);
1069 return FALSE;
1071 GetPrinterA(hprn, 2, NULL, 0, &needed);
1072 PrintStructures->lpPrinterInfo = malloc(needed);
1073 GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1074 &needed);
1075 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1076 PrintStructures->lpDriverInfo = malloc(needed);
1077 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1078 needed, &needed)) {
1079 ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
1080 return FALSE;
1082 ClosePrinter(hprn);
1084 PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
1086 free(PrintStructures->lpDevMode);
1087 PrintStructures->lpDevMode = NULL;
1089 dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
1090 if(dmSize == -1) {
1091 ERR("DocumentProperties fails on %s\n", debugstr_a(name));
1092 return FALSE;
1094 PrintStructures->lpDevMode = malloc(dmSize);
1095 dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
1096 DM_OUT_BUFFER);
1097 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1098 !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
1099 (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
1100 /* Supplied devicemode matches current printer so try to use it */
1101 DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
1102 DM_OUT_BUFFER | DM_IN_BUFFER);
1104 if(lpdm)
1105 GlobalUnlock(lppd->hDevMode);
1107 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1109 if(!(lppd->Flags & PD_PRINTSETUP)) {
1110 /* Print range (All/Range/Selection) */
1111 if(lppd->nFromPage != 0xffff)
1112 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1113 if(lppd->nToPage != 0xffff)
1114 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1116 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1117 if (lppd->Flags & PD_NOSELECTION)
1118 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1119 else
1120 if (lppd->Flags & PD_SELECTION)
1121 CheckRadioButton(hDlg, rad1, rad3, rad2);
1122 if (lppd->Flags & PD_NOPAGENUMS) {
1123 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1124 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1125 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1126 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1127 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1128 } else {
1129 if (lppd->Flags & PD_PAGENUMS)
1130 CheckRadioButton(hDlg, rad1, rad3, rad3);
1133 /* Collate pages
1135 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1137 if (lppd->Flags & PD_COLLATE) {
1138 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1139 (LPARAM)PrintStructures->hCollateIcon);
1140 CheckDlgButton(hDlg, chx2, 1);
1141 } else {
1142 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1143 (LPARAM)PrintStructures->hNoCollateIcon);
1144 CheckDlgButton(hDlg, chx2, 0);
1147 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1148 /* if printer doesn't support it: no Collate */
1149 if (!(lpdm->dmFields & DM_COLLATE)) {
1150 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1151 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1155 /* nCopies */
1157 INT copies;
1158 if (lppd->hDevMode == 0)
1159 copies = lppd->nCopies;
1160 else
1161 copies = lpdm->dmCopies;
1162 if(copies == 0) copies = 1;
1163 else if(copies < 0) copies = MAX_COPIES;
1164 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1167 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1168 /* if printer doesn't support it: no nCopies */
1169 if (!(lpdm->dmFields & DM_COPIES)) {
1170 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1171 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1175 /* print to file */
1176 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1177 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1178 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1179 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1180 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1182 /* Fill print quality combo, PrintDlg16 */
1183 if(GetDlgItem(hDlg, cmb1))
1185 DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1186 PrintStructures->lpPrinterInfo->pPortName,
1187 DC_ENUMRESOLUTIONS, NULL, lpdm);
1189 if(numResolutions != -1)
1191 HWND hQuality = GetDlgItem(hDlg, cmb1);
1192 LONG* Resolutions;
1193 char buf[255];
1194 DWORD i;
1195 int dpiX, dpiY;
1196 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
1197 PrintStructures->lpPrinterInfo->pPrinterName,
1198 0, lpdm);
1200 Resolutions = malloc(numResolutions * sizeof(LONG) * 2);
1201 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
1202 PrintStructures->lpPrinterInfo->pPortName,
1203 DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
1205 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
1206 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
1207 DeleteDC(hPrinterDC);
1209 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
1210 for(i = 0; i < (numResolutions * 2); i += 2)
1212 BOOL IsDefault = FALSE;
1213 LRESULT Index;
1215 if(Resolutions[i] == Resolutions[i+1])
1217 if(dpiX == Resolutions[i])
1218 IsDefault = TRUE;
1219 sprintf(buf, "%ld dpi", Resolutions[i]);
1220 } else
1222 if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1223 IsDefault = TRUE;
1224 sprintf(buf, "%ld dpi x %ld dpi", Resolutions[i], Resolutions[i+1]);
1227 Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1229 if(IsDefault)
1230 SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1232 SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1234 free(Resolutions);
1237 } else { /* PD_PRINTSETUP */
1238 BOOL bPortrait = (lpdm->dmOrientation == DMORIENT_PORTRAIT);
1240 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1241 PrintStructures->lpPrinterInfo->pPrinterName,
1242 PrintStructures->lpPrinterInfo->pPortName,
1243 lpdm);
1244 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1245 PrintStructures->lpPrinterInfo->pPrinterName,
1246 PrintStructures->lpPrinterInfo->pPortName,
1247 lpdm);
1248 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1249 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1250 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1251 PrintStructures->hLandscapeIcon));
1255 /* help button */
1256 if ((lppd->Flags & PD_SHOWHELP)==0) {
1257 /* hide if PD_SHOWHELP not specified */
1258 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1260 return TRUE;
1263 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1264 PRINT_PTRW *PrintStructures)
1266 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1267 LPDEVMODEW lpdm = NULL;
1268 LONG dmSize;
1269 DWORD needed;
1270 HANDLE hprn;
1272 free(PrintStructures->lpPrinterInfo);
1273 free(PrintStructures->lpDriverInfo);
1274 if(!OpenPrinterW(name, &hprn, NULL)) {
1275 ERR("Can't open printer %s\n", debugstr_w(name));
1276 return FALSE;
1278 GetPrinterW(hprn, 2, NULL, 0, &needed);
1279 PrintStructures->lpPrinterInfo = malloc(needed);
1280 GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1281 &needed);
1282 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1283 PrintStructures->lpDriverInfo = malloc(needed);
1284 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1285 needed, &needed)) {
1286 ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1287 return FALSE;
1289 ClosePrinter(hprn);
1291 PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1293 free(PrintStructures->lpDevMode);
1294 PrintStructures->lpDevMode = NULL;
1296 dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1297 if(dmSize == -1) {
1298 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1299 return FALSE;
1301 PrintStructures->lpDevMode = malloc(dmSize);
1302 dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1303 DM_OUT_BUFFER);
1304 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1305 !lstrcmpW(lpdm->dmDeviceName,
1306 PrintStructures->lpDevMode->dmDeviceName)) {
1307 /* Supplied devicemode matches current printer so try to use it */
1308 DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1309 DM_OUT_BUFFER | DM_IN_BUFFER);
1311 if(lpdm)
1312 GlobalUnlock(lppd->hDevMode);
1314 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1316 if(!(lppd->Flags & PD_PRINTSETUP)) {
1317 /* Print range (All/Range/Selection) */
1318 if(lppd->nFromPage != 0xffff)
1319 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1320 if(lppd->nToPage != 0xffff)
1321 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1323 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1324 if (lppd->Flags & PD_NOSELECTION)
1325 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1326 else
1327 if (lppd->Flags & PD_SELECTION)
1328 CheckRadioButton(hDlg, rad1, rad3, rad2);
1329 if (lppd->Flags & PD_NOPAGENUMS) {
1330 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1331 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1332 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1333 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1334 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1335 } else {
1336 if (lppd->Flags & PD_PAGENUMS)
1337 CheckRadioButton(hDlg, rad1, rad3, rad3);
1340 /* Collate pages
1342 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1344 if (lppd->Flags & PD_COLLATE) {
1345 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1346 (LPARAM)PrintStructures->hCollateIcon);
1347 CheckDlgButton(hDlg, chx2, 1);
1348 } else {
1349 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1350 (LPARAM)PrintStructures->hNoCollateIcon);
1351 CheckDlgButton(hDlg, chx2, 0);
1354 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1355 /* if printer doesn't support it: no Collate */
1356 if (!(lpdm->dmFields & DM_COLLATE)) {
1357 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1358 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1362 /* nCopies */
1364 INT copies;
1365 if (lppd->hDevMode == 0)
1366 copies = lppd->nCopies;
1367 else
1368 copies = lpdm->dmCopies;
1369 if(copies == 0) copies = 1;
1370 else if(copies < 0) copies = MAX_COPIES;
1371 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1374 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1375 /* if printer doesn't support it: no nCopies */
1376 if (!(lpdm->dmFields & DM_COPIES)) {
1377 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1378 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1382 /* print to file */
1383 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1384 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1385 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1386 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1387 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1389 } else { /* PD_PRINTSETUP */
1390 BOOL bPortrait = (lpdm->dmOrientation == DMORIENT_PORTRAIT);
1392 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1393 PrintStructures->lpPrinterInfo->pPrinterName,
1394 PrintStructures->lpPrinterInfo->pPortName,
1395 lpdm);
1396 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1397 PrintStructures->lpPrinterInfo->pPrinterName,
1398 PrintStructures->lpPrinterInfo->pPortName,
1399 lpdm);
1400 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1401 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1402 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1403 PrintStructures->hLandscapeIcon));
1407 /* help button */
1408 if ((lppd->Flags & PD_SHOWHELP)==0) {
1409 /* hide if PD_SHOWHELP not specified */
1410 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1412 return TRUE;
1415 /***********************************************************************
1416 * check_printer_setup [internal]
1418 static LRESULT check_printer_setup(HWND hDlg)
1420 DWORD needed,num;
1421 WCHAR resourcestr[256],resultstr[256];
1423 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1424 if(needed == 0)
1426 EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1428 if(needed > 0)
1429 return TRUE;
1430 else
1432 LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1433 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1434 MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1435 return FALSE;
1439 /***********************************************************************
1440 * PRINTDLG_WMInitDialog [internal]
1442 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg,
1443 PRINT_PTRA* PrintStructures)
1445 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1446 DEVNAMES *pdn;
1447 DEVMODEA *pdm;
1448 char *name = NULL;
1449 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1451 /* load Collate ICONs */
1452 /* We load these with LoadImage because they are not a standard
1453 size and we don't want them rescaled */
1454 PrintStructures->hCollateIcon =
1455 LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1456 PrintStructures->hNoCollateIcon =
1457 LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1459 /* These can be done with LoadIcon */
1460 PrintStructures->hPortraitIcon =
1461 LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1462 PrintStructures->hLandscapeIcon =
1463 LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1465 /* display the collate/no_collate icon */
1466 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1467 (LPARAM)PrintStructures->hNoCollateIcon);
1469 if(PrintStructures->hCollateIcon == 0 ||
1470 PrintStructures->hNoCollateIcon == 0 ||
1471 PrintStructures->hPortraitIcon == 0 ||
1472 PrintStructures->hLandscapeIcon == 0) {
1473 ERR("no icon in resource file\n");
1474 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1475 EndDialog(hDlg, FALSE);
1479 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1480 * must be registered and the Help button must be shown.
1482 if (lppd->Flags & PD_SHOWHELP) {
1483 if((PrintStructures->HelpMessageID =
1484 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1485 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1486 return FALSE;
1488 } else
1489 PrintStructures->HelpMessageID = 0;
1491 if(!(lppd->Flags &PD_PRINTSETUP)) {
1492 PrintStructures->hwndUpDown =
1493 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1494 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1495 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1496 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1497 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1500 /* FIXME: I allow more freedom than either Win95 or WinNT,
1501 * which do not agree on what errors should be thrown or not
1502 * in case nToPage or nFromPage is out-of-range.
1504 if (lppd->nMaxPage < lppd->nMinPage)
1505 lppd->nMaxPage = lppd->nMinPage;
1506 if (lppd->nMinPage == lppd->nMaxPage)
1507 lppd->Flags |= PD_NOPAGENUMS;
1508 if (lppd->nToPage < lppd->nMinPage)
1509 lppd->nToPage = lppd->nMinPage;
1510 if (lppd->nToPage > lppd->nMaxPage)
1511 lppd->nToPage = lppd->nMaxPage;
1512 if (lppd->nFromPage < lppd->nMinPage)
1513 lppd->nFromPage = lppd->nMinPage;
1514 if (lppd->nFromPage > lppd->nMaxPage)
1515 lppd->nFromPage = lppd->nMaxPage;
1517 /* if we have the combo box, fill it */
1518 if (GetDlgItem(hDlg,comboID)) {
1519 /* Fill Combobox
1521 pdn = GlobalLock(lppd->hDevNames);
1522 pdm = GlobalLock(lppd->hDevMode);
1523 if(pdn)
1524 name = (char*)pdn + pdn->wDeviceOffset;
1525 else if(pdm)
1526 name = (char*)pdm->dmDeviceName;
1527 PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1528 if(pdm) GlobalUnlock(lppd->hDevMode);
1529 if(pdn) GlobalUnlock(lppd->hDevNames);
1531 /* Now find selected printer and update rest of dlg */
1532 name = malloc(256);
1533 if (GetDlgItemTextA(hDlg, comboID, name, 255))
1534 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1535 free(name);
1536 } else {
1537 /* else use default printer */
1538 char name[200];
1539 DWORD dwBufLen = ARRAY_SIZE(name);
1540 BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1542 if (ret)
1543 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1544 else
1545 FIXME("No default printer found, expect problems!\n");
1547 return TRUE;
1550 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg,
1551 PRINT_PTRW* PrintStructures)
1553 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1554 DEVNAMES *pdn;
1555 DEVMODEW *pdm;
1556 WCHAR *name = NULL;
1557 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1559 /* load Collate ICONs */
1560 /* We load these with LoadImage because they are not a standard
1561 size and we don't want them rescaled */
1562 PrintStructures->hCollateIcon =
1563 LoadImageW(COMDLG32_hInstance, L"PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1564 PrintStructures->hNoCollateIcon =
1565 LoadImageW(COMDLG32_hInstance, L"PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1567 /* These can be done with LoadIcon */
1568 PrintStructures->hPortraitIcon =
1569 LoadIconW(COMDLG32_hInstance, L"PD32_PORTRAIT");
1570 PrintStructures->hLandscapeIcon =
1571 LoadIconW(COMDLG32_hInstance, L"PD32_LANDSCAPE");
1573 /* display the collate/no_collate icon */
1574 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1575 (LPARAM)PrintStructures->hNoCollateIcon);
1577 if(PrintStructures->hCollateIcon == 0 ||
1578 PrintStructures->hNoCollateIcon == 0 ||
1579 PrintStructures->hPortraitIcon == 0 ||
1580 PrintStructures->hLandscapeIcon == 0) {
1581 ERR("no icon in resource file\n");
1582 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1583 EndDialog(hDlg, FALSE);
1587 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1588 * must be registered and the Help button must be shown.
1590 if (lppd->Flags & PD_SHOWHELP) {
1591 if((PrintStructures->HelpMessageID =
1592 RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1593 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1594 return FALSE;
1596 } else
1597 PrintStructures->HelpMessageID = 0;
1599 if(!(lppd->Flags &PD_PRINTSETUP)) {
1600 PrintStructures->hwndUpDown =
1601 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1602 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1603 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1604 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1605 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1608 /* FIXME: I allow more freedom than either Win95 or WinNT,
1609 * which do not agree to what errors should be thrown or not
1610 * in case nToPage or nFromPage is out-of-range.
1612 if (lppd->nMaxPage < lppd->nMinPage)
1613 lppd->nMaxPage = lppd->nMinPage;
1614 if (lppd->nMinPage == lppd->nMaxPage)
1615 lppd->Flags |= PD_NOPAGENUMS;
1616 if (lppd->nToPage < lppd->nMinPage)
1617 lppd->nToPage = lppd->nMinPage;
1618 if (lppd->nToPage > lppd->nMaxPage)
1619 lppd->nToPage = lppd->nMaxPage;
1620 if (lppd->nFromPage < lppd->nMinPage)
1621 lppd->nFromPage = lppd->nMinPage;
1622 if (lppd->nFromPage > lppd->nMaxPage)
1623 lppd->nFromPage = lppd->nMaxPage;
1625 /* if we have the combo box, fill it */
1626 if (GetDlgItem(hDlg,comboID)) {
1627 /* Fill Combobox
1629 pdn = GlobalLock(lppd->hDevNames);
1630 pdm = GlobalLock(lppd->hDevMode);
1631 if(pdn)
1632 name = (WCHAR*)pdn + pdn->wDeviceOffset;
1633 else if(pdm)
1634 name = pdm->dmDeviceName;
1635 PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1636 if(pdm) GlobalUnlock(lppd->hDevMode);
1637 if(pdn) GlobalUnlock(lppd->hDevNames);
1639 /* Now find selected printer and update rest of dlg */
1640 /* ansi is ok here */
1641 name = malloc(256 * sizeof(WCHAR));
1642 if (GetDlgItemTextW(hDlg, comboID, name, 255))
1643 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1644 free(name);
1645 } else {
1646 /* else use default printer */
1647 WCHAR name[200];
1648 DWORD dwBufLen = ARRAY_SIZE(name);
1649 BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1651 if (ret)
1652 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1653 else
1654 FIXME("No default printer found, expect problems!\n");
1656 return TRUE;
1659 /***********************************************************************
1660 * PRINTDLG_WMCommand [internal]
1662 static LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1663 PRINT_PTRA* PrintStructures)
1665 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1666 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1667 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1669 switch (LOWORD(wParam)) {
1670 case IDOK:
1671 TRACE(" OK button was hit\n");
1672 if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1673 FIXME("Update printdlg was not successful!\n");
1674 return(FALSE);
1676 EndDialog(hDlg, TRUE);
1677 return(TRUE);
1679 case IDCANCEL:
1680 TRACE(" CANCEL button was hit\n");
1681 EndDialog(hDlg, FALSE);
1682 return(FALSE);
1684 case pshHelp:
1685 TRACE(" HELP button was hit\n");
1686 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1687 (WPARAM) hDlg, (LPARAM) lppd);
1688 break;
1690 case chx2: /* collate pages checkbox */
1691 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1692 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1693 (LPARAM)PrintStructures->hCollateIcon);
1694 else
1695 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1696 (LPARAM)PrintStructures->hNoCollateIcon);
1697 break;
1698 case edt1: /* from page nr editbox */
1699 case edt2: /* to page nr editbox */
1700 if (HIWORD(wParam)==EN_CHANGE) {
1701 WORD nToPage;
1702 WORD nFromPage;
1703 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1704 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1705 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1706 CheckRadioButton(hDlg, rad1, rad3, rad3);
1708 break;
1710 case edt3:
1711 if(HIWORD(wParam) == EN_CHANGE) {
1712 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1713 if(copies <= 1)
1714 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1715 else
1716 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1718 break;
1720 case psh2: /* Properties button */
1722 HANDLE hPrinter;
1723 char PrinterName[256];
1725 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1726 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1727 FIXME(" Call to OpenPrinter did not succeed!\n");
1728 break;
1730 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1731 PrintStructures->lpDevMode,
1732 PrintStructures->lpDevMode,
1733 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1734 ClosePrinter(hPrinter);
1735 break;
1738 case rad1: /* Paperorientation */
1739 if (lppd->Flags & PD_PRINTSETUP)
1741 lpdm->dmOrientation = DMORIENT_PORTRAIT;
1742 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1743 (LPARAM)(PrintStructures->hPortraitIcon));
1745 break;
1747 case rad2: /* Paperorientation */
1748 if (lppd->Flags & PD_PRINTSETUP)
1750 lpdm->dmOrientation = DMORIENT_LANDSCAPE;
1751 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1752 (LPARAM)(PrintStructures->hLandscapeIcon));
1754 break;
1756 case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1757 if (PrinterComboID != LOWORD(wParam)) {
1758 break;
1760 /* FALLTHROUGH */
1761 case cmb4: /* Printer combobox */
1762 if (HIWORD(wParam)==CBN_SELCHANGE) {
1763 char *PrinterName;
1764 INT index = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETCURSEL, 0, 0);
1765 INT length = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXTLEN, index, 0);
1766 PrinterName = malloc(length + 1);
1767 SendDlgItemMessageA(hDlg, LOWORD(wParam), CB_GETLBTEXT, index, (LPARAM)PrinterName);
1768 PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1769 free(PrinterName);
1771 break;
1773 case cmb2: /* Papersize */
1775 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1776 if(Sel != CB_ERR) {
1777 lpdm->dmPaperSize = SendDlgItemMessageA(hDlg, cmb2, CB_GETITEMDATA, Sel, 0);
1778 GetDlgItemTextA(hDlg, cmb2, (char *)lpdm->dmFormName, CCHFORMNAME);
1781 break;
1783 case cmb3: /* Bin */
1785 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1786 if(Sel != CB_ERR)
1787 lpdm->dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3, CB_GETITEMDATA, Sel, 0);
1789 break;
1791 if(lppd->Flags & PD_PRINTSETUP) {
1792 switch (LOWORD(wParam)) {
1793 case rad1: /* orientation */
1794 case rad2:
1795 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1796 if(lpdm->dmOrientation != DMORIENT_PORTRAIT) {
1797 lpdm->dmOrientation = DMORIENT_PORTRAIT;
1798 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1799 (LPARAM)PrintStructures->hPortraitIcon);
1800 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1801 (LPARAM)PrintStructures->hPortraitIcon);
1803 } else {
1804 if(lpdm->dmOrientation != DMORIENT_LANDSCAPE) {
1805 lpdm->dmOrientation = DMORIENT_LANDSCAPE;
1806 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1807 (LPARAM)PrintStructures->hLandscapeIcon);
1808 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1809 (LPARAM)PrintStructures->hLandscapeIcon);
1812 break;
1815 return FALSE;
1818 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1819 PRINT_PTRW* PrintStructures)
1821 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1822 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1823 LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1825 switch (LOWORD(wParam)) {
1826 case IDOK:
1827 TRACE(" OK button was hit\n");
1828 if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1829 FIXME("Update printdlg was not successful!\n");
1830 return(FALSE);
1832 EndDialog(hDlg, TRUE);
1833 return(TRUE);
1835 case IDCANCEL:
1836 TRACE(" CANCEL button was hit\n");
1837 EndDialog(hDlg, FALSE);
1838 return(FALSE);
1840 case pshHelp:
1841 TRACE(" HELP button was hit\n");
1842 SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1843 (WPARAM) hDlg, (LPARAM) lppd);
1844 break;
1846 case chx2: /* collate pages checkbox */
1847 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1848 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1849 (LPARAM)PrintStructures->hCollateIcon);
1850 else
1851 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, IMAGE_ICON,
1852 (LPARAM)PrintStructures->hNoCollateIcon);
1853 break;
1854 case edt1: /* from page nr editbox */
1855 case edt2: /* to page nr editbox */
1856 if (HIWORD(wParam)==EN_CHANGE) {
1857 WORD nToPage;
1858 WORD nFromPage;
1859 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1860 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1861 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1862 CheckRadioButton(hDlg, rad1, rad3, rad3);
1864 break;
1866 case edt3:
1867 if(HIWORD(wParam) == EN_CHANGE) {
1868 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1869 if(copies <= 1)
1870 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1871 else
1872 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1874 break;
1876 case psh2: /* Properties button */
1878 HANDLE hPrinter;
1879 WCHAR PrinterName[256];
1881 if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1882 if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1883 FIXME(" Call to OpenPrinter did not succeed!\n");
1884 break;
1886 DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1887 PrintStructures->lpDevMode,
1888 PrintStructures->lpDevMode,
1889 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1890 ClosePrinter(hPrinter);
1891 break;
1894 case rad1: /* Paperorientation */
1895 if (lppd->Flags & PD_PRINTSETUP)
1897 lpdm->dmOrientation = DMORIENT_PORTRAIT;
1898 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1899 (LPARAM)(PrintStructures->hPortraitIcon));
1901 break;
1903 case rad2: /* Paperorientation */
1904 if (lppd->Flags & PD_PRINTSETUP)
1906 lpdm->dmOrientation = DMORIENT_LANDSCAPE;
1907 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1908 (LPARAM)(PrintStructures->hLandscapeIcon));
1910 break;
1912 case cmb1: /* Printer Combobox in PRINT SETUP */
1913 /* FALLTHROUGH */
1914 case cmb4: /* Printer combobox */
1915 if (HIWORD(wParam)==CBN_SELCHANGE) {
1916 WCHAR *PrinterName;
1917 INT index = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETCURSEL, 0, 0);
1918 INT length = SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXTLEN, index, 0);
1920 PrinterName = malloc(sizeof(WCHAR) * (length + 1));
1921 SendDlgItemMessageW(hDlg, LOWORD(wParam), CB_GETLBTEXT, index, (LPARAM)PrinterName);
1922 PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1923 free(PrinterName);
1925 break;
1927 case cmb2: /* Papersize */
1929 DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1930 if(Sel != CB_ERR) {
1931 lpdm->dmPaperSize = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, Sel, 0);
1932 GetDlgItemTextW(hDlg, cmb2, lpdm->dmFormName, CCHFORMNAME);
1935 break;
1937 case cmb3: /* Bin */
1939 DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1940 if(Sel != CB_ERR)
1941 lpdm->dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA, Sel, 0);
1943 break;
1945 if(lppd->Flags & PD_PRINTSETUP) {
1946 switch (LOWORD(wParam)) {
1947 case rad1: /* orientation */
1948 case rad2:
1949 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1950 if(lpdm->dmOrientation != DMORIENT_PORTRAIT) {
1951 lpdm->dmOrientation = DMORIENT_PORTRAIT;
1952 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1953 (LPARAM)PrintStructures->hPortraitIcon);
1954 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1955 (LPARAM)PrintStructures->hPortraitIcon);
1957 } else {
1958 if(lpdm->dmOrientation != DMORIENT_LANDSCAPE) {
1959 lpdm->dmOrientation = DMORIENT_LANDSCAPE;
1960 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE, IMAGE_ICON,
1961 (LPARAM)PrintStructures->hLandscapeIcon);
1962 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, IMAGE_ICON,
1963 (LPARAM)PrintStructures->hLandscapeIcon);
1966 break;
1969 return FALSE;
1972 /***********************************************************************
1973 * PrintDlgProcA [internal]
1975 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1976 LPARAM lParam)
1978 PRINT_PTRA* PrintStructures;
1979 INT_PTR res = FALSE;
1981 if (uMsg!=WM_INITDIALOG) {
1982 PrintStructures = GetPropW(hDlg, printdlg_prop);
1983 if (!PrintStructures)
1984 return FALSE;
1985 } else {
1986 PrintStructures = (PRINT_PTRA*) lParam;
1987 SetPropW(hDlg, printdlg_prop, PrintStructures);
1988 if(!check_printer_setup(hDlg))
1990 EndDialog(hDlg,FALSE);
1991 return FALSE;
1993 res = PRINTDLG_WMInitDialog(hDlg, PrintStructures);
1995 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1996 res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1997 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1999 return res;
2002 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
2003 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
2004 lParam);
2005 if(res) return res;
2008 switch (uMsg) {
2009 case WM_COMMAND:
2010 return PRINTDLG_WMCommandA(hDlg, wParam, PrintStructures);
2012 case WM_DESTROY:
2013 DestroyIcon(PrintStructures->hCollateIcon);
2014 DestroyIcon(PrintStructures->hNoCollateIcon);
2015 DestroyIcon(PrintStructures->hPortraitIcon);
2016 DestroyIcon(PrintStructures->hLandscapeIcon);
2017 if(PrintStructures->hwndUpDown)
2018 DestroyWindow(PrintStructures->hwndUpDown);
2019 return FALSE;
2021 return res;
2024 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
2025 LPARAM lParam)
2027 PRINT_PTRW* PrintStructures;
2028 INT_PTR res = FALSE;
2030 if (uMsg!=WM_INITDIALOG) {
2031 PrintStructures = GetPropW(hDlg, printdlg_prop);
2032 if (!PrintStructures)
2033 return FALSE;
2034 } else {
2035 PrintStructures = (PRINT_PTRW*) lParam;
2036 SetPropW(hDlg, printdlg_prop, PrintStructures);
2037 if(!check_printer_setup(hDlg))
2039 EndDialog(hDlg,FALSE);
2040 return FALSE;
2042 res = PRINTDLG_WMInitDialogW(hDlg, PrintStructures);
2044 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
2045 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
2046 return res;
2049 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
2050 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
2051 if(res) return res;
2054 switch (uMsg) {
2055 case WM_COMMAND:
2056 return PRINTDLG_WMCommandW(hDlg, wParam, PrintStructures);
2058 case WM_DESTROY:
2059 DestroyIcon(PrintStructures->hCollateIcon);
2060 DestroyIcon(PrintStructures->hNoCollateIcon);
2061 DestroyIcon(PrintStructures->hPortraitIcon);
2062 DestroyIcon(PrintStructures->hLandscapeIcon);
2063 if(PrintStructures->hwndUpDown)
2064 DestroyWindow(PrintStructures->hwndUpDown);
2065 return FALSE;
2067 return res;
2070 /************************************************************
2072 * PRINTDLG_GetDlgTemplate
2075 static HGLOBAL PRINTDLG_GetDlgTemplateA(const PRINTDLGA *lppd)
2077 HRSRC hResInfo;
2078 HGLOBAL hDlgTmpl;
2080 if (lppd->Flags & PD_PRINTSETUP) {
2081 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
2082 hDlgTmpl = lppd->hSetupTemplate;
2083 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
2084 hResInfo = FindResourceA(lppd->hInstance,
2085 lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
2086 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2087 } else {
2088 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
2089 (LPSTR)RT_DIALOG);
2090 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2092 } else {
2093 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
2094 hDlgTmpl = lppd->hPrintTemplate;
2095 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
2096 hResInfo = FindResourceA(lppd->hInstance,
2097 lppd->lpPrintTemplateName,
2098 (LPSTR)RT_DIALOG);
2099 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2100 } else {
2101 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
2102 (LPSTR)RT_DIALOG);
2103 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2106 return hDlgTmpl;
2109 static HGLOBAL PRINTDLG_GetDlgTemplateW(const PRINTDLGW *lppd)
2111 HRSRC hResInfo;
2112 HGLOBAL hDlgTmpl;
2114 if (lppd->Flags & PD_PRINTSETUP) {
2115 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
2116 hDlgTmpl = lppd->hSetupTemplate;
2117 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
2118 hResInfo = FindResourceW(lppd->hInstance,
2119 lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
2120 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2121 } else {
2122 hResInfo = FindResourceW(COMDLG32_hInstance, L"PRINT32_SETUP", (LPWSTR)RT_DIALOG);
2123 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2125 } else {
2126 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
2127 hDlgTmpl = lppd->hPrintTemplate;
2128 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
2129 hResInfo = FindResourceW(lppd->hInstance,
2130 lppd->lpPrintTemplateName,
2131 (LPWSTR)RT_DIALOG);
2132 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
2133 } else {
2134 hResInfo = FindResourceW(COMDLG32_hInstance, L"PRINT32", (LPWSTR)RT_DIALOG);
2135 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
2138 return hDlgTmpl;
2141 /***********************************************************************
2143 * PRINTDLG_CreateDC
2146 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
2148 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
2149 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
2151 if(lppd->Flags & PD_RETURNDC) {
2152 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
2153 (char*)pdn + pdn->wDeviceOffset,
2154 (char*)pdn + pdn->wOutputOffset,
2155 pdm );
2156 } else if(lppd->Flags & PD_RETURNIC) {
2157 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
2158 (char*)pdn + pdn->wDeviceOffset,
2159 (char*)pdn + pdn->wOutputOffset,
2160 pdm );
2162 GlobalUnlock(lppd->hDevNames);
2163 GlobalUnlock(lppd->hDevMode);
2164 return lppd->hDC != NULL;
2167 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
2169 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
2170 DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
2172 if(lppd->Flags & PD_RETURNDC) {
2173 lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
2174 (WCHAR*)pdn + pdn->wDeviceOffset,
2175 (WCHAR*)pdn + pdn->wOutputOffset,
2176 pdm );
2177 } else if(lppd->Flags & PD_RETURNIC) {
2178 lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
2179 (WCHAR*)pdn + pdn->wDeviceOffset,
2180 (WCHAR*)pdn + pdn->wOutputOffset,
2181 pdm );
2183 GlobalUnlock(lppd->hDevNames);
2184 GlobalUnlock(lppd->hDevMode);
2185 return lppd->hDC != NULL;
2188 /***********************************************************************
2189 * PrintDlgA (COMDLG32.@)
2191 * Displays the PRINT dialog box, which enables the user to specify
2192 * specific properties of the print job.
2194 * PARAMS
2195 * lppd [IO] ptr to PRINTDLG32 struct
2197 * RETURNS
2198 * nonzero if the user pressed the OK button
2199 * zero if the user cancelled the window or an error occurred
2201 * BUGS
2202 * PrintDlg:
2203 * * The Collate Icons do not display, even though they are in the code.
2204 * * The Properties Button(s) should call DocumentPropertiesA().
2207 BOOL WINAPI PrintDlgA(LPPRINTDLGA lppd)
2209 BOOL bRet = FALSE;
2210 LPVOID ptr;
2211 HINSTANCE hInst;
2213 if (!lppd)
2215 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2216 return FALSE;
2219 if(TRACE_ON(commdlg)) {
2220 char flagstr[1000] = "";
2221 const struct pd_flags *pflag = pd_flags;
2222 for( ; pflag->name; pflag++) {
2223 if(lppd->Flags & pflag->flag)
2224 strcat(flagstr, pflag->name);
2226 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2227 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2228 "flags %08lx (%s)\n",
2229 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2230 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2231 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2234 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2235 WARN("structure size failure!!!\n");
2236 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2237 return FALSE;
2240 if(lppd->Flags & PD_RETURNDEFAULT) {
2241 PRINTER_INFO_2A *pbuf;
2242 DRIVER_INFO_3A *dbuf;
2243 HANDLE hprn;
2244 DWORD needed;
2246 if(lppd->hDevMode || lppd->hDevNames) {
2247 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2248 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2249 return FALSE;
2251 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2252 WARN("Can't find default printer\n");
2253 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2254 return FALSE;
2257 GetPrinterA(hprn, 2, NULL, 0, &needed);
2258 pbuf = malloc(needed);
2259 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2261 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2262 dbuf = malloc(needed);
2263 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2264 ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",
2265 GetLastError(),pbuf->pPrinterName);
2266 free(dbuf);
2267 free(pbuf);
2268 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2269 return FALSE;
2271 ClosePrinter(hprn);
2273 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2274 dbuf->pDriverPath,
2275 pbuf->pPrinterName,
2276 pbuf->pPortName);
2277 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2278 pbuf->pDevMode->dmDriverExtra);
2279 ptr = GlobalLock(lppd->hDevMode);
2280 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2281 pbuf->pDevMode->dmDriverExtra);
2282 GlobalUnlock(lppd->hDevMode);
2283 free(pbuf);
2284 free(dbuf);
2285 bRet = TRUE;
2286 } else {
2287 HGLOBAL hDlgTmpl;
2288 PRINT_PTRA *PrintStructures;
2290 /* load Dialog resources,
2291 * depending on Flags indicates Print32 or Print32_setup dialog
2293 hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2294 if (!hDlgTmpl) {
2295 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2296 return FALSE;
2298 ptr = LockResource( hDlgTmpl );
2299 if (!ptr) {
2300 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2301 return FALSE;
2304 PrintStructures = calloc(1, sizeof(PRINT_PTRA));
2305 PrintStructures->lpPrintDlg = lppd;
2307 /* and create & process the dialog .
2308 * -1 is failure, 0 is broken hwnd, everything else is ok.
2310 hInst = COMDLG32_hInstance;
2311 if (lppd->Flags & (PD_ENABLESETUPTEMPLATE | PD_ENABLEPRINTTEMPLATE)) hInst = lppd->hInstance;
2312 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2313 PrintDlgProcA,
2314 (LPARAM)PrintStructures));
2316 if(bRet) {
2317 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2318 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2319 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2321 if (lppd->hDevMode == 0) {
2322 TRACE(" No hDevMode yet... Need to create my own\n");
2323 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2324 lpdm->dmSize + lpdm->dmDriverExtra);
2325 } else {
2326 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2327 lpdm->dmSize + lpdm->dmDriverExtra,
2328 GMEM_MOVEABLE);
2330 lpdmReturn = GlobalLock(lppd->hDevMode);
2331 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2333 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2334 di->pDriverPath,
2335 pi->pPrinterName,
2336 pi->pPortName
2338 GlobalUnlock(lppd->hDevMode);
2340 free(PrintStructures->lpDevMode);
2341 free(PrintStructures->lpPrinterInfo);
2342 free(PrintStructures->lpDriverInfo);
2343 free(PrintStructures);
2345 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2346 bRet = PRINTDLG_CreateDCA(lppd);
2348 TRACE("exit! (%d)\n", bRet);
2349 return bRet;
2352 /***********************************************************************
2353 * PrintDlgW (COMDLG32.@)
2355 * See PrintDlgA.
2357 BOOL WINAPI PrintDlgW(LPPRINTDLGW lppd)
2359 BOOL bRet = FALSE;
2360 LPVOID ptr;
2361 HINSTANCE hInst;
2363 if (!lppd)
2365 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2366 return FALSE;
2369 if(TRACE_ON(commdlg)) {
2370 char flagstr[1000] = "";
2371 const struct pd_flags *pflag = pd_flags;
2372 for( ; pflag->name; pflag++) {
2373 if(lppd->Flags & pflag->flag)
2374 strcat(flagstr, pflag->name);
2376 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2377 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2378 "flags %08lx (%s)\n",
2379 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2380 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2381 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2384 if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2385 WARN("structure size failure!!!\n");
2386 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2387 return FALSE;
2390 if(lppd->Flags & PD_RETURNDEFAULT) {
2391 PRINTER_INFO_2W *pbuf;
2392 DRIVER_INFO_3W *dbuf;
2393 HANDLE hprn;
2394 DWORD needed;
2396 if(lppd->hDevMode || lppd->hDevNames) {
2397 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2398 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2399 return FALSE;
2401 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2402 WARN("Can't find default printer\n");
2403 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2404 return FALSE;
2407 GetPrinterW(hprn, 2, NULL, 0, &needed);
2408 pbuf = malloc(needed);
2409 GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2411 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2412 dbuf = malloc(needed);
2413 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2414 ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",
2415 GetLastError(),debugstr_w(pbuf->pPrinterName));
2416 free(dbuf);
2417 free(pbuf);
2418 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2419 return FALSE;
2421 ClosePrinter(hprn);
2423 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2424 dbuf->pDriverPath,
2425 pbuf->pPrinterName,
2426 pbuf->pPortName);
2427 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2428 pbuf->pDevMode->dmDriverExtra);
2429 ptr = GlobalLock(lppd->hDevMode);
2430 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2431 pbuf->pDevMode->dmDriverExtra);
2432 GlobalUnlock(lppd->hDevMode);
2433 free(pbuf);
2434 free(dbuf);
2435 bRet = TRUE;
2436 } else {
2437 HGLOBAL hDlgTmpl;
2438 PRINT_PTRW *PrintStructures;
2440 /* load Dialog resources,
2441 * depending on Flags indicates Print32 or Print32_setup dialog
2443 hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2444 if (!hDlgTmpl) {
2445 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2446 return FALSE;
2448 ptr = LockResource( hDlgTmpl );
2449 if (!ptr) {
2450 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2451 return FALSE;
2454 PrintStructures = calloc(1, sizeof(PRINT_PTRW));
2455 PrintStructures->lpPrintDlg = lppd;
2457 /* and create & process the dialog .
2458 * -1 is failure, 0 is broken hwnd, everything else is ok.
2460 hInst = COMDLG32_hInstance;
2461 if (lppd->Flags & (PD_ENABLESETUPTEMPLATE | PD_ENABLEPRINTTEMPLATE)) hInst = lppd->hInstance;
2462 bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2463 PrintDlgProcW,
2464 (LPARAM)PrintStructures));
2466 if(bRet) {
2467 DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2468 PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2469 DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2471 if (lppd->hDevMode == 0) {
2472 TRACE(" No hDevMode yet... Need to create my own\n");
2473 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2474 lpdm->dmSize + lpdm->dmDriverExtra);
2475 } else {
2476 WORD locks;
2477 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2478 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2479 while(locks--) {
2480 GlobalUnlock(lppd->hDevMode);
2481 TRACE("Now got %d locks\n", locks);
2484 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2485 lpdm->dmSize + lpdm->dmDriverExtra,
2486 GMEM_MOVEABLE);
2488 lpdmReturn = GlobalLock(lppd->hDevMode);
2489 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2491 if (lppd->hDevNames != 0) {
2492 WORD locks;
2493 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2494 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2495 while(locks--)
2496 GlobalUnlock(lppd->hDevNames);
2499 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2500 di->pDriverPath,
2501 pi->pPrinterName,
2502 pi->pPortName
2504 GlobalUnlock(lppd->hDevMode);
2506 free(PrintStructures->lpDevMode);
2507 free(PrintStructures->lpPrinterInfo);
2508 free(PrintStructures->lpDriverInfo);
2509 free(PrintStructures);
2511 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2512 bRet = PRINTDLG_CreateDCW(lppd);
2514 TRACE("exit! (%d)\n", bRet);
2515 return bRet;
2518 /***********************************************************************
2520 * PageSetupDlg
2521 * rad1 - portrait
2522 * rad2 - landscape
2523 * cmb1 - printer select (not in standard dialog template)
2524 * cmb2 - paper size
2525 * cmb3 - source (tray?)
2526 * edt4 - border left
2527 * edt5 - border top
2528 * edt6 - border right
2529 * edt7 - border bottom
2530 * psh3 - "Printer..."
2533 typedef struct
2535 BOOL unicode;
2536 union
2538 LPPAGESETUPDLGA dlga;
2539 LPPAGESETUPDLGW dlgw;
2540 } u;
2541 HWND hDlg; /* Page Setup dialog handle */
2542 RECT rtDrawRect; /* Drawing rect for page */
2543 } pagesetup_data;
2545 static inline DWORD pagesetup_get_flags(const pagesetup_data *data)
2547 return data->u.dlgw->Flags;
2550 static inline BOOL is_metric(const pagesetup_data *data)
2552 return pagesetup_get_flags(data) & PSD_INHUNDREDTHSOFMILLIMETERS;
2555 static inline LONG tenths_mm_to_size(const pagesetup_data *data, LONG size)
2557 if (is_metric(data))
2558 return 10 * size;
2559 else
2560 return 10 * size * 100 / 254;
2563 static inline LONG thousandths_inch_to_size(const pagesetup_data *data, LONG size)
2565 if (is_metric(data))
2566 return size * 254 / 100;
2567 else
2568 return size;
2571 static WCHAR get_decimal_sep(void)
2573 static WCHAR sep;
2575 if(!sep)
2577 WCHAR buf[] = L".";
2578 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, ARRAY_SIZE(buf));
2579 sep = buf[0];
2581 return sep;
2584 static void size2str(const pagesetup_data *data, DWORD size, LPWSTR strout)
2586 /* FIXME use LOCALE_SDECIMAL when the edit parsing code can cope */
2588 if (is_metric(data))
2590 if(size % 100)
2591 wsprintfW(strout, L"%d%c%02d", size / 100, get_decimal_sep(), size % 100);
2592 else
2593 wsprintfW(strout, L"%d", size / 100);
2595 else
2597 if(size % 1000)
2598 wsprintfW(strout, L"%d%c%03d", size / 1000, get_decimal_sep(), size % 1000);
2599 else
2600 wsprintfW(strout, L"%d", size / 1000);
2605 static inline BOOL is_default_metric(void)
2607 DWORD system;
2608 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
2609 (LPWSTR)&system, sizeof(system));
2610 return system == 0;
2613 /**********************************************
2614 * rotate_rect
2615 * Cyclically permute the four members of rc
2616 * If sense is TRUE l -> t -> r -> b
2617 * otherwise l <- t <- r <- b
2619 static inline void rotate_rect(RECT *rc, BOOL sense)
2621 INT tmp;
2622 if(sense)
2624 tmp = rc->bottom;
2625 rc->bottom = rc->right;
2626 rc->right = rc->top;
2627 rc->top = rc->left;
2628 rc->left = tmp;
2630 else
2632 tmp = rc->left;
2633 rc->left = rc->top;
2634 rc->top = rc->right;
2635 rc->right = rc->bottom;
2636 rc->bottom = tmp;
2640 static void pagesetup_set_orientation(pagesetup_data *data, WORD orient)
2642 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2644 assert(orient == DMORIENT_PORTRAIT || orient == DMORIENT_LANDSCAPE);
2646 if(data->unicode)
2647 dm->dmOrientation = orient;
2648 else
2650 DEVMODEA *dmA = (DEVMODEA *)dm;
2651 dmA->dmOrientation = orient;
2653 GlobalUnlock(data->u.dlgw->hDevMode);
2656 static WORD pagesetup_get_orientation(const pagesetup_data *data)
2658 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2659 WORD orient;
2661 if(data->unicode)
2662 orient = dm->dmOrientation;
2663 else
2665 DEVMODEA *dmA = (DEVMODEA *)dm;
2666 orient = dmA->dmOrientation;
2668 GlobalUnlock(data->u.dlgw->hDevMode);
2669 return orient;
2672 static void pagesetup_set_papersize(pagesetup_data *data, WORD paper)
2674 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2676 if(data->unicode)
2677 dm->dmPaperSize = paper;
2678 else
2680 DEVMODEA *dmA = (DEVMODEA *)dm;
2681 dmA->dmPaperSize = paper;
2683 GlobalUnlock(data->u.dlgw->hDevMode);
2686 static WORD pagesetup_get_papersize(const pagesetup_data *data)
2688 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2689 WORD paper;
2691 if(data->unicode)
2692 paper = dm->dmPaperSize;
2693 else
2695 DEVMODEA *dmA = (DEVMODEA *)dm;
2696 paper = dmA->dmPaperSize;
2698 GlobalUnlock(data->u.dlgw->hDevMode);
2699 return paper;
2702 static void pagesetup_set_defaultsource(pagesetup_data *data, WORD source)
2704 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2706 if(data->unicode)
2707 dm->dmDefaultSource = source;
2708 else
2710 DEVMODEA *dmA = (DEVMODEA *)dm;
2711 dmA->dmDefaultSource = source;
2713 GlobalUnlock(data->u.dlgw->hDevMode);
2716 typedef enum
2718 devnames_driver_name,
2719 devnames_device_name,
2720 devnames_output_name
2721 } devnames_name;
2724 static inline WORD get_devname_offset(const DEVNAMES *dn, devnames_name which)
2726 switch(which)
2728 case devnames_driver_name: return dn->wDriverOffset;
2729 case devnames_device_name: return dn->wDeviceOffset;
2730 case devnames_output_name: return dn->wOutputOffset;
2732 ERR("Shouldn't be here\n");
2733 return 0;
2736 static WCHAR *pagesetup_get_a_devname(const pagesetup_data *data, devnames_name which)
2738 DEVNAMES *dn;
2739 WCHAR *name;
2741 dn = GlobalLock(data->u.dlgw->hDevNames);
2742 if(data->unicode)
2743 name = wcsdup((WCHAR *)dn + get_devname_offset(dn, which));
2744 else
2746 int len = MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, NULL, 0);
2747 name = malloc(len * sizeof(WCHAR));
2748 MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, name, len);
2750 GlobalUnlock(data->u.dlgw->hDevNames);
2751 return name;
2754 static WCHAR *pagesetup_get_drvname(const pagesetup_data *data)
2756 return pagesetup_get_a_devname(data, devnames_driver_name);
2759 static WCHAR *pagesetup_get_devname(const pagesetup_data *data)
2761 return pagesetup_get_a_devname(data, devnames_device_name);
2764 static WCHAR *pagesetup_get_portname(const pagesetup_data *data)
2766 return pagesetup_get_a_devname(data, devnames_output_name);
2769 static void pagesetup_release_a_devname(const pagesetup_data *data, WCHAR *name)
2771 free(name);
2774 static void pagesetup_set_devnames(pagesetup_data *data, LPCWSTR drv, LPCWSTR devname, LPCWSTR port)
2776 DEVNAMES *dn;
2777 WCHAR def[256];
2778 DWORD len = sizeof(DEVNAMES), drv_len, dev_len, port_len;
2780 if(data->unicode)
2782 drv_len = (lstrlenW(drv) + 1) * sizeof(WCHAR);
2783 dev_len = (lstrlenW(devname) + 1) * sizeof(WCHAR);
2784 port_len = (lstrlenW(port) + 1) * sizeof(WCHAR);
2786 else
2788 drv_len = WideCharToMultiByte(CP_ACP, 0, drv, -1, NULL, 0, NULL, NULL);
2789 dev_len = WideCharToMultiByte(CP_ACP, 0, devname, -1, NULL, 0, NULL, NULL);
2790 port_len = WideCharToMultiByte(CP_ACP, 0, port, -1, NULL, 0, NULL, NULL);
2792 len += drv_len + dev_len + port_len;
2794 if(data->u.dlgw->hDevNames)
2795 data->u.dlgw->hDevNames = GlobalReAlloc(data->u.dlgw->hDevNames, len, GMEM_MOVEABLE);
2796 else
2797 data->u.dlgw->hDevNames = GlobalAlloc(GMEM_MOVEABLE, len);
2799 dn = GlobalLock(data->u.dlgw->hDevNames);
2801 if(data->unicode)
2803 WCHAR *ptr = (WCHAR *)(dn + 1);
2804 len = sizeof(DEVNAMES) / sizeof(WCHAR);
2805 dn->wDriverOffset = len;
2806 lstrcpyW(ptr, drv);
2807 ptr += drv_len / sizeof(WCHAR);
2808 len += drv_len / sizeof(WCHAR);
2809 dn->wDeviceOffset = len;
2810 lstrcpyW(ptr, devname);
2811 ptr += dev_len / sizeof(WCHAR);
2812 len += dev_len / sizeof(WCHAR);
2813 dn->wOutputOffset = len;
2814 lstrcpyW(ptr, port);
2816 else
2818 char *ptr = (char *)(dn + 1);
2819 len = sizeof(DEVNAMES);
2820 dn->wDriverOffset = len;
2821 WideCharToMultiByte(CP_ACP, 0, drv, -1, ptr, drv_len, NULL, NULL);
2822 ptr += drv_len;
2823 len += drv_len;
2824 dn->wDeviceOffset = len;
2825 WideCharToMultiByte(CP_ACP, 0, devname, -1, ptr, dev_len, NULL, NULL);
2826 ptr += dev_len;
2827 len += dev_len;
2828 dn->wOutputOffset = len;
2829 WideCharToMultiByte(CP_ACP, 0, port, -1, ptr, port_len, NULL, NULL);
2832 dn->wDefault = 0;
2833 len = ARRAY_SIZE(def);
2834 GetDefaultPrinterW(def, &len);
2835 if(!lstrcmpW(def, devname))
2836 dn->wDefault = 1;
2838 GlobalUnlock(data->u.dlgw->hDevNames);
2841 static DEVMODEW *pagesetup_get_devmode(const pagesetup_data *data)
2843 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2844 DEVMODEW *ret;
2846 if(data->unicode)
2848 /* We make a copy even in the unicode case because the ptr
2849 may get passed back to us in pagesetup_set_devmode. */
2850 ret = malloc(dm->dmSize + dm->dmDriverExtra);
2851 memcpy(ret, dm, dm->dmSize + dm->dmDriverExtra);
2853 else
2854 ret = GdiConvertToDevmodeW((DEVMODEA *)dm);
2856 GlobalUnlock(data->u.dlgw->hDevMode);
2857 return ret;
2860 static void pagesetup_release_devmode(const pagesetup_data *data, DEVMODEW *dm)
2862 free(dm);
2865 static void pagesetup_set_devmode(pagesetup_data *data, DEVMODEW *dm)
2867 DEVMODEA *dmA = NULL;
2868 void *src, *dst;
2869 DWORD size;
2871 if(data->unicode)
2873 size = dm->dmSize + dm->dmDriverExtra;
2874 src = dm;
2876 else
2878 dmA = convert_to_devmodeA(dm);
2879 size = dmA->dmSize + dmA->dmDriverExtra;
2880 src = dmA;
2883 if(data->u.dlgw->hDevMode)
2884 data->u.dlgw->hDevMode = GlobalReAlloc(data->u.dlgw->hDevMode, size,
2885 GMEM_MOVEABLE);
2886 else
2887 data->u.dlgw->hDevMode = GlobalAlloc(GMEM_MOVEABLE, size);
2889 dst = GlobalLock(data->u.dlgw->hDevMode);
2890 memcpy(dst, src, size);
2891 GlobalUnlock(data->u.dlgw->hDevMode);
2892 free(dmA);
2895 static inline POINT *pagesetup_get_papersize_pt(const pagesetup_data *data)
2897 return &data->u.dlgw->ptPaperSize;
2900 static inline RECT *pagesetup_get_margin_rect(const pagesetup_data *data)
2902 return &data->u.dlgw->rtMargin;
2905 typedef enum
2907 page_setup_hook,
2908 page_paint_hook
2909 } hook_type;
2911 static inline LPPAGESETUPHOOK pagesetup_get_hook(const pagesetup_data *data, hook_type which)
2913 switch(which)
2915 case page_setup_hook: return data->u.dlgw->lpfnPageSetupHook;
2916 case page_paint_hook: return data->u.dlgw->lpfnPagePaintHook;
2918 return NULL;
2921 /* This should only be used in calls to hook procs so we return the ptr
2922 already cast to LPARAM */
2923 static inline LPARAM pagesetup_get_dlg_struct(const pagesetup_data *data)
2925 return (LPARAM)data->u.dlgw;
2928 static inline void swap_point(POINT *pt)
2930 LONG tmp = pt->x;
2931 pt->x = pt->y;
2932 pt->y = tmp;
2935 static BOOL pagesetup_update_papersize(pagesetup_data *data)
2937 DEVMODEW *dm;
2938 LPWSTR devname, portname;
2939 int i, num;
2940 WORD *words = NULL, paperword;
2941 POINT *points = NULL;
2942 BOOL retval = FALSE;
2944 dm = pagesetup_get_devmode(data);
2945 devname = pagesetup_get_devname(data);
2946 portname = pagesetup_get_portname(data);
2948 num = DeviceCapabilitiesW(devname, portname, DC_PAPERS, NULL, dm);
2949 if (num <= 0)
2951 FIXME("No papernames found for %s/%s\n", debugstr_w(devname), debugstr_w(portname));
2952 goto end;
2955 words = malloc(num * sizeof(WORD));
2956 points = malloc(num * sizeof(POINT));
2958 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERS, (LPWSTR)words, dm))
2960 FIXME("Number of returned words is not %d\n", num);
2961 goto end;
2964 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERSIZE, (LPWSTR)points, dm))
2966 FIXME("Number of returned sizes is not %d\n", num);
2967 goto end;
2970 paperword = pagesetup_get_papersize(data);
2972 for (i = 0; i < num; i++)
2973 if (words[i] == paperword)
2974 break;
2976 if (i == num)
2978 FIXME("Papersize %d not found in list?\n", paperword);
2979 goto end;
2982 /* this is _10ths_ of a millimeter */
2983 pagesetup_get_papersize_pt(data)->x = tenths_mm_to_size(data, points[i].x);
2984 pagesetup_get_papersize_pt(data)->y = tenths_mm_to_size(data, points[i].y);
2986 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2987 swap_point(pagesetup_get_papersize_pt(data));
2989 retval = TRUE;
2991 end:
2992 free(words);
2993 free(points);
2994 pagesetup_release_a_devname(data, portname);
2995 pagesetup_release_a_devname(data, devname);
2996 pagesetup_release_devmode(data, dm);
2998 return retval;
3001 /**********************************************************************************************
3002 * pagesetup_change_printer
3004 * Redefines hDevMode and hDevNames HANDLES and initialises it.
3007 static BOOL pagesetup_change_printer(LPWSTR name, pagesetup_data *data)
3009 HANDLE hprn;
3010 DWORD needed;
3011 PRINTER_INFO_2W *prn_info = NULL;
3012 DRIVER_INFO_3W *drv_info = NULL;
3013 DEVMODEW *dm = NULL;
3014 BOOL retval = FALSE;
3016 if(!OpenPrinterW(name, &hprn, NULL))
3018 ERR("Can't open printer %s\n", debugstr_w(name));
3019 goto end;
3022 GetPrinterW(hprn, 2, NULL, 0, &needed);
3023 prn_info = malloc(needed);
3024 GetPrinterW(hprn, 2, (LPBYTE)prn_info, needed, &needed);
3025 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
3026 drv_info = malloc(needed);
3027 if(!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)drv_info, needed, &needed))
3029 ERR("GetPrinterDriverA failed for %s, fix your config!\n", debugstr_w(prn_info->pPrinterName));
3030 goto end;
3032 ClosePrinter(hprn);
3034 needed = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
3035 if(needed == -1)
3037 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
3038 goto end;
3041 dm = malloc(needed);
3042 DocumentPropertiesW(0, 0, name, dm, NULL, DM_OUT_BUFFER);
3044 pagesetup_set_devmode(data, dm);
3045 pagesetup_set_devnames(data, drv_info->pDriverPath, prn_info->pPrinterName,
3046 prn_info->pPortName);
3048 retval = TRUE;
3049 end:
3050 free(dm);
3051 free(prn_info);
3052 free(drv_info);
3053 return retval;
3056 /****************************************************************************************
3057 * pagesetup_init_combos
3059 * Fills Printers, Paper and Source combos
3062 static void pagesetup_init_combos(HWND hDlg, pagesetup_data *data)
3064 DEVMODEW *dm;
3065 LPWSTR devname, portname;
3067 dm = pagesetup_get_devmode(data);
3068 devname = pagesetup_get_devname(data);
3069 portname = pagesetup_get_portname(data);
3071 PRINTDLG_SetUpPrinterListComboW(hDlg, cmb1, devname);
3072 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2, devname, portname, dm);
3073 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3, devname, portname, dm);
3075 pagesetup_release_a_devname(data, portname);
3076 pagesetup_release_a_devname(data, devname);
3077 pagesetup_release_devmode(data, dm);
3081 /****************************************************************************************
3082 * pagesetup_change_printer_dialog
3084 * Pops up another dialog that lets the user pick another printer.
3086 * For now we display the PrintDlg, this should display a striped down version of it.
3088 static void pagesetup_change_printer_dialog(HWND hDlg, pagesetup_data *data)
3090 PRINTDLGW prnt;
3091 LPWSTR drvname, devname, portname;
3092 DEVMODEW *tmp_dm, *dm;
3094 memset(&prnt, 0, sizeof(prnt));
3095 prnt.lStructSize = sizeof(prnt);
3096 prnt.Flags = 0;
3097 prnt.hwndOwner = hDlg;
3099 drvname = pagesetup_get_drvname(data);
3100 devname = pagesetup_get_devname(data);
3101 portname = pagesetup_get_portname(data);
3102 prnt.hDevNames = 0;
3103 PRINTDLG_CreateDevNamesW(&prnt.hDevNames, drvname, devname, portname);
3104 pagesetup_release_a_devname(data, portname);
3105 pagesetup_release_a_devname(data, devname);
3106 pagesetup_release_a_devname(data, drvname);
3108 tmp_dm = pagesetup_get_devmode(data);
3109 prnt.hDevMode = GlobalAlloc(GMEM_MOVEABLE, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
3110 dm = GlobalLock(prnt.hDevMode);
3111 memcpy(dm, tmp_dm, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
3112 GlobalUnlock(prnt.hDevMode);
3113 pagesetup_release_devmode(data, tmp_dm);
3115 if (PrintDlgW(&prnt))
3117 DEVMODEW *dm = GlobalLock(prnt.hDevMode);
3118 DEVNAMES *dn = GlobalLock(prnt.hDevNames);
3120 pagesetup_set_devnames(data, (WCHAR*)dn + dn->wDriverOffset,
3121 (WCHAR*)dn + dn->wDeviceOffset, (WCHAR *)dn + dn->wOutputOffset);
3122 pagesetup_set_devmode(data, dm);
3123 GlobalUnlock(prnt.hDevNames);
3124 GlobalUnlock(prnt.hDevMode);
3125 pagesetup_init_combos(hDlg, data);
3128 GlobalFree(prnt.hDevMode);
3129 GlobalFree(prnt.hDevNames);
3133 /******************************************************************************************
3134 * pagesetup_change_preview
3136 * Changes paper preview size / position
3139 static void pagesetup_change_preview(const pagesetup_data *data)
3141 LONG width, height, x, y;
3142 RECT tmp;
3143 const int shadow = 4;
3145 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3147 width = data->rtDrawRect.right - data->rtDrawRect.left;
3148 height = pagesetup_get_papersize_pt(data)->y * width / pagesetup_get_papersize_pt(data)->x;
3150 else
3152 height = data->rtDrawRect.bottom - data->rtDrawRect.top;
3153 width = pagesetup_get_papersize_pt(data)->x * height / pagesetup_get_papersize_pt(data)->y;
3155 x = (data->rtDrawRect.right + data->rtDrawRect.left - width) / 2;
3156 y = (data->rtDrawRect.bottom + data->rtDrawRect.top - height) / 2;
3157 TRACE("draw rect %s x=%ld, y=%ld, w=%ld, h=%ld\n",
3158 wine_dbgstr_rect(&data->rtDrawRect), x, y, width, height);
3160 MoveWindow(GetDlgItem(data->hDlg, rct2), x + width, y + shadow, shadow, height, FALSE);
3161 MoveWindow(GetDlgItem(data->hDlg, rct3), x + shadow, y + height, width, shadow, FALSE);
3162 MoveWindow(GetDlgItem(data->hDlg, rct1), x, y, width, height, FALSE);
3164 tmp = data->rtDrawRect;
3165 tmp.right += shadow;
3166 tmp.bottom += shadow;
3167 InvalidateRect(data->hDlg, &tmp, TRUE);
3170 static inline LONG *element_from_margin_id(RECT *rc, WORD id)
3172 switch(id)
3174 case edt4: return &rc->left;
3175 case edt5: return &rc->top;
3176 case edt6: return &rc->right;
3177 case edt7: return &rc->bottom;
3179 return NULL;
3182 static void update_margin_edits(HWND hDlg, const pagesetup_data *data, WORD id)
3184 WCHAR str[100];
3185 WORD idx;
3187 for(idx = edt4; idx <= edt7; idx++)
3189 if(id == 0 || id == idx)
3191 size2str(data, *element_from_margin_id(pagesetup_get_margin_rect(data), idx), str);
3192 SetDlgItemTextW(hDlg, idx, str);
3197 static void margin_edit_notification(HWND hDlg, const pagesetup_data *data, WORD msg, WORD id)
3199 switch (msg)
3201 case EN_CHANGE:
3203 WCHAR buf[10];
3204 LONG val = 0;
3205 LONG *value = element_from_margin_id(pagesetup_get_margin_rect(data), id);
3207 if (GetDlgItemTextW(hDlg, id, buf, ARRAY_SIZE(buf)) != 0)
3209 WCHAR *end;
3210 WCHAR decimal = get_decimal_sep();
3212 val = wcstol(buf, &end, 10);
3213 if(end != buf || *end == decimal)
3215 int mult = is_metric(data) ? 100 : 1000;
3216 val *= mult;
3217 if(*end == decimal)
3219 while(mult > 1)
3221 end++;
3222 mult /= 10;
3223 if(iswdigit(*end))
3224 val += (*end - '0') * mult;
3225 else
3226 break;
3231 *value = val;
3232 return;
3235 case EN_KILLFOCUS:
3236 update_margin_edits(hDlg, data, id);
3237 return;
3241 static void set_margin_groupbox_title(HWND hDlg, const pagesetup_data *data)
3243 WCHAR title[256];
3245 if(LoadStringW(COMDLG32_hInstance, is_metric(data) ? PD32_MARGINS_IN_MILLIMETERS : PD32_MARGINS_IN_INCHES,
3246 title, ARRAY_SIZE(title)))
3247 SetDlgItemTextW(hDlg, grp4, title);
3250 static void pagesetup_update_orientation_buttons(HWND hDlg, const pagesetup_data *data)
3252 if (pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3253 CheckRadioButton(hDlg, rad1, rad2, rad2);
3254 else
3255 CheckRadioButton(hDlg, rad1, rad2, rad1);
3258 /****************************************************************************************
3259 * pagesetup_printer_properties
3261 * Handle invocation of the 'Properties' button (not present in the default template).
3263 static void pagesetup_printer_properties(HWND hDlg, pagesetup_data *data)
3265 HANDLE hprn;
3266 LPWSTR devname;
3267 DEVMODEW *dm;
3268 LRESULT count;
3269 int i;
3271 devname = pagesetup_get_devname(data);
3273 if (!OpenPrinterW(devname, &hprn, NULL))
3275 FIXME("Call to OpenPrinter did not succeed!\n");
3276 pagesetup_release_a_devname(data, devname);
3277 return;
3280 dm = pagesetup_get_devmode(data);
3281 DocumentPropertiesW(hDlg, hprn, devname, dm, dm, DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
3282 pagesetup_set_devmode(data, dm);
3283 pagesetup_release_devmode(data, dm);
3284 pagesetup_release_a_devname(data, devname);
3285 ClosePrinter(hprn);
3287 /* Changing paper */
3288 pagesetup_update_papersize(data);
3289 pagesetup_update_orientation_buttons(hDlg, data);
3291 /* Changing paper preview */
3292 pagesetup_change_preview(data);
3294 /* Selecting paper in combo */
3295 count = SendDlgItemMessageW(hDlg, cmb2, CB_GETCOUNT, 0, 0);
3296 if(count != CB_ERR)
3298 WORD paperword = pagesetup_get_papersize(data);
3299 for(i = 0; i < count; i++)
3301 if(SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, i, 0) == paperword) {
3302 SendDlgItemMessageW(hDlg, cmb2, CB_SETCURSEL, i, 0);
3303 break;
3309 /********************************************************************************
3310 * pagesetup_wm_command
3311 * process WM_COMMAND message for PageSetupDlg
3313 * PARAMS
3314 * hDlg [in] Main dialog HANDLE
3315 * wParam [in] WM_COMMAND wParam
3316 * lParam [in] WM_COMMAND lParam
3317 * pda [in/out] ptr to PageSetupDataA
3320 static BOOL pagesetup_wm_command(HWND hDlg, WPARAM wParam, LPARAM lParam, pagesetup_data *data)
3322 WORD msg = HIWORD(wParam);
3323 WORD id = LOWORD(wParam);
3325 TRACE("loword (lparam) %d, wparam 0x%Ix, lparam %08Ix\n",
3326 LOWORD(lParam),wParam,lParam);
3327 switch (id) {
3328 case IDOK:
3329 EndDialog(hDlg, TRUE);
3330 return TRUE ;
3332 case IDCANCEL:
3333 EndDialog(hDlg, FALSE);
3334 return FALSE ;
3336 case psh3: /* Printer... */
3337 pagesetup_change_printer_dialog(hDlg, data);
3338 return TRUE;
3340 case rad1: /* Portrait */
3341 case rad2: /* Landscape */
3342 if((id == rad1 && pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE) ||
3343 (id == rad2 && pagesetup_get_orientation(data) == DMORIENT_PORTRAIT))
3345 pagesetup_set_orientation(data, (id == rad1) ? DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE);
3346 pagesetup_update_papersize(data);
3347 rotate_rect(pagesetup_get_margin_rect(data), (id == rad2));
3348 update_margin_edits(hDlg, data, 0);
3349 pagesetup_change_preview(data);
3351 break;
3352 case cmb1: /* Printer combo */
3353 if(msg == CBN_SELCHANGE)
3355 WCHAR *name;
3356 INT index = SendDlgItemMessageW(hDlg, id, CB_GETCURSEL, 0, 0);
3357 INT length = SendDlgItemMessageW(hDlg, id, CB_GETLBTEXTLEN, index, 0);
3358 name = malloc(sizeof(WCHAR) * (length + 1));
3359 SendDlgItemMessageW(hDlg, id, CB_GETLBTEXT, index, (LPARAM)name);
3360 pagesetup_change_printer(name, data);
3361 pagesetup_init_combos(hDlg, data);
3362 free(name);
3364 break;
3365 case cmb2: /* Paper combo */
3366 if(msg == CBN_SELCHANGE)
3368 DWORD paperword = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA,
3369 SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0), 0);
3370 if (paperword != CB_ERR)
3372 pagesetup_set_papersize(data, paperword);
3373 pagesetup_update_papersize(data);
3374 pagesetup_change_preview(data);
3375 } else
3376 FIXME("could not get dialog text for papersize cmbbox?\n");
3378 break;
3379 case cmb3: /* Paper Source */
3380 if(msg == CBN_SELCHANGE)
3382 WORD source = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA,
3383 SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0), 0);
3384 pagesetup_set_defaultsource(data, source);
3386 break;
3387 case psh2: /* Printer Properties button */
3388 pagesetup_printer_properties(hDlg, data);
3389 break;
3390 case edt4:
3391 case edt5:
3392 case edt6:
3393 case edt7:
3394 margin_edit_notification(hDlg, data, msg, id);
3395 break;
3397 InvalidateRect(GetDlgItem(hDlg, rct1), NULL, TRUE);
3398 return FALSE;
3401 /***********************************************************************
3402 * default_page_paint_hook
3403 * Default hook paint procedure that receives WM_PSD_* messages from the dialog box
3404 * whenever the sample page is redrawn.
3406 static UINT_PTR default_page_paint_hook(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam,
3407 const pagesetup_data *data)
3409 LPRECT lprc = (LPRECT) lParam;
3410 HDC hdc = (HDC) wParam;
3411 HPEN hpen, holdpen;
3412 LOGFONTW lf;
3413 HFONT hfont, holdfont;
3414 INT oldbkmode;
3415 TRACE("uMsg: WM_USER+%d\n",uMsg-WM_USER);
3416 /* Call user paint hook if enable */
3417 if (pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK)
3418 if (pagesetup_get_hook(data, page_paint_hook)(hwndDlg, uMsg, wParam, lParam))
3419 return TRUE;
3421 switch (uMsg) {
3422 /* LPPAGESETUPDLG in lParam */
3423 case WM_PSD_PAGESETUPDLG:
3424 /* Inform about the sample page rectangle */
3425 case WM_PSD_FULLPAGERECT:
3426 /* Inform about the margin rectangle */
3427 case WM_PSD_MINMARGINRECT:
3428 return FALSE;
3430 /* Draw dashed rectangle showing margins */
3431 case WM_PSD_MARGINRECT:
3432 hpen = CreatePen(PS_DASH, 1, GetSysColor(COLOR_3DSHADOW));
3433 holdpen = SelectObject(hdc, hpen);
3434 Rectangle(hdc, lprc->left, lprc->top, lprc->right, lprc->bottom);
3435 DeleteObject(SelectObject(hdc, holdpen));
3436 return TRUE;
3437 /* Draw the fake document */
3438 case WM_PSD_GREEKTEXTRECT:
3439 /* select a nice scalable font, because we want the text really small */
3440 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
3441 lf.lfHeight = 6; /* value chosen based on visual effect */
3442 hfont = CreateFontIndirectW(&lf);
3443 holdfont = SelectObject(hdc, hfont);
3445 /* if text not loaded, then do so now */
3446 if (wszFakeDocumentText[0] == '\0')
3447 LoadStringW(COMDLG32_hInstance,
3448 IDS_FAKEDOCTEXT,
3449 wszFakeDocumentText,
3450 ARRAY_SIZE(wszFakeDocumentText));
3452 oldbkmode = SetBkMode(hdc, TRANSPARENT);
3453 DrawTextW(hdc, wszFakeDocumentText, -1, lprc, DT_TOP|DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
3454 SetBkMode(hdc, oldbkmode);
3456 DeleteObject(SelectObject(hdc, holdfont));
3457 return TRUE;
3459 /* Envelope stamp */
3460 case WM_PSD_ENVSTAMPRECT:
3461 /* Return address */
3462 case WM_PSD_YAFULLPAGERECT:
3463 FIXME("envelope/stamp is not implemented\n");
3464 return FALSE;
3465 default:
3466 FIXME("Unknown message %x\n",uMsg);
3467 return FALSE;
3469 return TRUE;
3472 /***********************************************************************
3473 * PagePaintProc
3474 * The main paint procedure for the PageSetupDlg function.
3475 * The Page Setup dialog box includes an image of a sample page that shows how
3476 * the user's selections affect the appearance of the printed output.
3477 * The image consists of a rectangle that represents the selected paper
3478 * or envelope type, with a dotted-line rectangle representing
3479 * the current margins, and partial (Greek text) characters
3480 * to show how text looks on the printed page.
3482 * The following messages in the order sends to user hook procedure:
3483 * WM_PSD_PAGESETUPDLG Draw the contents of the sample page
3484 * WM_PSD_FULLPAGERECT Inform about the bounding rectangle
3485 * WM_PSD_MINMARGINRECT Inform about the margin rectangle (min margin?)
3486 * WM_PSD_MARGINRECT Draw the margin rectangle
3487 * WM_PSD_GREEKTEXTRECT Draw the Greek text inside the margin rectangle
3488 * If any of first three messages returns TRUE, painting done.
3490 * PARAMS:
3491 * hWnd [in] Handle to the Page Setup dialog box
3492 * uMsg [in] Received message
3494 * TODO:
3495 * WM_PSD_ENVSTAMPRECT Draw in the envelope-stamp rectangle (for envelopes only)
3496 * WM_PSD_YAFULLPAGERECT Draw the return address portion (for envelopes and other paper sizes)
3498 * RETURNS:
3499 * FALSE if all done correctly
3504 static LRESULT CALLBACK
3505 PRINTDLG_PagePaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3507 PAINTSTRUCT ps;
3508 RECT rcClient, rcMargin;
3509 HPEN hpen, holdpen;
3510 HDC hdc;
3511 HBRUSH hbrush, holdbrush;
3512 pagesetup_data *data;
3513 int papersize=0, orientation=0; /* FIXME: set these values for the user paint hook */
3514 double scalx, scaly;
3516 if (uMsg != WM_PAINT)
3517 return CallWindowProcA(lpfnStaticWndProc, hWnd, uMsg, wParam, lParam);
3519 /* Processing WM_PAINT message */
3520 data = GetPropW(hWnd, pagesetupdlg_prop);
3521 if (!data) {
3522 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3523 return FALSE;
3525 if (default_page_paint_hook(hWnd, WM_PSD_PAGESETUPDLG, MAKELONG(papersize, orientation),
3526 pagesetup_get_dlg_struct(data), data))
3527 return FALSE;
3529 hdc = BeginPaint(hWnd, &ps);
3530 GetClientRect(hWnd, &rcClient);
3532 scalx = rcClient.right / (double)pagesetup_get_papersize_pt(data)->x;
3533 scaly = rcClient.bottom / (double)pagesetup_get_papersize_pt(data)->y;
3534 rcMargin = rcClient;
3536 rcMargin.left += pagesetup_get_margin_rect(data)->left * scalx;
3537 rcMargin.top += pagesetup_get_margin_rect(data)->top * scaly;
3538 rcMargin.right -= pagesetup_get_margin_rect(data)->right * scalx;
3539 rcMargin.bottom -= pagesetup_get_margin_rect(data)->bottom * scaly;
3541 /* if the space is too small then we make sure to not draw anything */
3542 rcMargin.left = min(rcMargin.left, rcMargin.right);
3543 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3545 if (!default_page_paint_hook(hWnd, WM_PSD_FULLPAGERECT, (WPARAM)hdc, (LPARAM)&rcClient, data) &&
3546 !default_page_paint_hook(hWnd, WM_PSD_MINMARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data) )
3548 /* fill background */
3549 hbrush = GetSysColorBrush(COLOR_3DHIGHLIGHT);
3550 FillRect(hdc, &rcClient, hbrush);
3551 holdbrush = SelectObject(hdc, hbrush);
3553 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
3554 holdpen = SelectObject(hdc, hpen);
3556 /* paint left edge */
3557 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3558 LineTo(hdc, rcClient.left, rcClient.bottom-1);
3560 /* paint top edge */
3561 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3562 LineTo(hdc, rcClient.right, rcClient.top);
3564 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
3565 DeleteObject(SelectObject(hdc, hpen));
3567 /* paint right edge */
3568 MoveToEx(hdc, rcClient.right-1, rcClient.top, NULL);
3569 LineTo(hdc, rcClient.right-1, rcClient.bottom);
3571 /* paint bottom edge */
3572 MoveToEx(hdc, rcClient.left, rcClient.bottom-1, NULL);
3573 LineTo(hdc, rcClient.right, rcClient.bottom-1);
3575 DeleteObject(SelectObject(hdc, holdpen));
3576 DeleteObject(SelectObject(hdc, holdbrush));
3578 default_page_paint_hook(hWnd, WM_PSD_MARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3580 /* give text a bit of a space from the frame */
3581 InflateRect(&rcMargin, -2, -2);
3583 /* if the space is too small then we make sure to not draw anything */
3584 rcMargin.left = min(rcMargin.left, rcMargin.right);
3585 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3587 default_page_paint_hook(hWnd, WM_PSD_GREEKTEXTRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3590 EndPaint(hWnd, &ps);
3591 return FALSE;
3594 /*******************************************************
3595 * The margin edit controls are subclassed to filter
3596 * anything other than numbers and the decimal separator.
3598 static LRESULT CALLBACK pagesetup_margin_editproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3600 if (msg == WM_CHAR)
3602 WCHAR decimal = get_decimal_sep();
3603 WCHAR wc = (WCHAR)wparam;
3604 if(!iswdigit(wc) && wc != decimal && wc != VK_BACK) return 0;
3606 return CallWindowProcW(edit_wndproc, hwnd, msg, wparam, lparam);
3609 static void subclass_margin_edits(HWND hDlg)
3611 int id;
3612 WNDPROC old_proc;
3614 for(id = edt4; id <= edt7; id++)
3616 old_proc = (WNDPROC)SetWindowLongPtrW(GetDlgItem(hDlg, id),
3617 GWLP_WNDPROC,
3618 (ULONG_PTR)pagesetup_margin_editproc);
3619 InterlockedCompareExchangePointer((void**)&edit_wndproc, old_proc, NULL);
3623 /***********************************************************************
3624 * pagesetup_dlg_proc
3626 * Message handler for PageSetupDlg
3628 static INT_PTR CALLBACK pagesetup_dlg_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
3630 pagesetup_data *data;
3631 INT_PTR res = FALSE;
3632 HWND hDrawWnd;
3634 if (uMsg == WM_INITDIALOG) { /*Init dialog*/
3635 data = (pagesetup_data *)lParam;
3636 data->hDlg = hDlg;
3638 hDrawWnd = GetDlgItem(hDlg, rct1);
3639 TRACE("set property to %p\n", data);
3640 SetPropW(hDlg, pagesetupdlg_prop, data);
3641 SetPropW(hDrawWnd, pagesetupdlg_prop, data);
3642 GetWindowRect(hDrawWnd, &data->rtDrawRect); /* Calculating rect in client coordinates where paper draws */
3643 MapWindowPoints( 0, hDlg, (LPPOINT)&data->rtDrawRect, 2 );
3644 lpfnStaticWndProc = (WNDPROC)SetWindowLongPtrW(
3645 hDrawWnd,
3646 GWLP_WNDPROC,
3647 (ULONG_PTR)PRINTDLG_PagePaintProc);
3649 /* FIXME: Paint hook. Must it be at begin of initialization or at end? */
3650 res = TRUE;
3651 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3653 if (!pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam,
3654 pagesetup_get_dlg_struct(data)))
3655 FIXME("Setup page hook failed?\n");
3658 /* if printer button disabled */
3659 if (pagesetup_get_flags(data) & PSD_DISABLEPRINTER)
3660 EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
3661 /* if margin edit boxes disabled */
3662 if (pagesetup_get_flags(data) & PSD_DISABLEMARGINS)
3664 EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
3665 EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
3666 EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
3667 EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
3670 /* Set orientation radiobuttons properly */
3671 pagesetup_update_orientation_buttons(hDlg, data);
3673 /* if orientation disabled */
3674 if (pagesetup_get_flags(data) & PSD_DISABLEORIENTATION)
3676 EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
3677 EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
3680 /* We fill them out enabled or not */
3681 if (!(pagesetup_get_flags(data) & PSD_MARGINS))
3683 /* default is 1 inch */
3684 LONG size = thousandths_inch_to_size(data, 1000);
3685 SetRect(pagesetup_get_margin_rect(data), size, size, size, size);
3687 update_margin_edits(hDlg, data, 0);
3688 subclass_margin_edits(hDlg);
3689 set_margin_groupbox_title(hDlg, data);
3691 /* if paper disabled */
3692 if (pagesetup_get_flags(data) & PSD_DISABLEPAPER)
3694 EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
3695 EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
3698 /* filling combos: printer, paper, source. selecting current printer (from DEVMODEA) */
3699 pagesetup_init_combos(hDlg, data);
3700 pagesetup_update_papersize(data);
3701 pagesetup_set_defaultsource(data, DMBIN_FORMSOURCE); /* FIXME: This is the auto select bin. Is this correct? */
3703 /* Drawing paper prev */
3704 pagesetup_change_preview(data);
3705 return TRUE;
3706 } else {
3707 data = GetPropW(hDlg, pagesetupdlg_prop);
3708 if (!data)
3710 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3711 return FALSE;
3713 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3715 res = pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam, lParam);
3716 if (res) return res;
3719 switch (uMsg) {
3720 case WM_COMMAND:
3721 return pagesetup_wm_command(hDlg, wParam, lParam, data);
3723 return FALSE;
3726 static WCHAR *get_default_printer(void)
3728 WCHAR *name = NULL;
3729 DWORD len = 0;
3731 GetDefaultPrinterW(NULL, &len);
3732 if(len)
3734 name = malloc(len * sizeof(WCHAR));
3735 GetDefaultPrinterW(name, &len);
3737 return name;
3740 static void pagesetup_dump_dlg_struct(const pagesetup_data *data)
3742 if(TRACE_ON(commdlg))
3744 char flagstr[1000] = "";
3745 const struct pd_flags *pflag = psd_flags;
3746 for( ; pflag->name; pflag++)
3748 if(pagesetup_get_flags(data) & pflag->flag)
3750 strcat(flagstr, pflag->name);
3751 strcat(flagstr, "|");
3754 TRACE("%s: (%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
3755 "hinst %p, flags %08lx (%s)\n",
3756 data->unicode ? "unicode" : "ansi",
3757 data->u.dlgw, data->u.dlgw->hwndOwner, data->u.dlgw->hDevMode,
3758 data->u.dlgw->hDevNames, data->u.dlgw->hInstance,
3759 pagesetup_get_flags(data), flagstr);
3763 static void *pagesetup_get_template(pagesetup_data *data)
3765 HRSRC res;
3766 HGLOBAL tmpl_handle;
3768 if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATEHANDLE)
3770 tmpl_handle = data->u.dlgw->hPageSetupTemplate;
3772 else if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATE)
3774 if(data->unicode)
3775 res = FindResourceW(data->u.dlgw->hInstance,
3776 data->u.dlgw->lpPageSetupTemplateName, (LPWSTR)RT_DIALOG);
3777 else
3778 res = FindResourceA(data->u.dlga->hInstance,
3779 data->u.dlga->lpPageSetupTemplateName, (LPSTR)RT_DIALOG);
3780 tmpl_handle = LoadResource(data->u.dlgw->hInstance, res);
3782 else
3784 res = FindResourceW(COMDLG32_hInstance, MAKEINTRESOURCEW(PAGESETUPDLGORD),
3785 (LPWSTR)RT_DIALOG);
3786 tmpl_handle = LoadResource(COMDLG32_hInstance, res);
3788 return LockResource(tmpl_handle);
3791 static BOOL pagesetup_common(pagesetup_data *data)
3793 BOOL ret;
3794 void *tmpl;
3796 if(!pagesetup_get_dlg_struct(data))
3798 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
3799 return FALSE;
3802 pagesetup_dump_dlg_struct(data);
3804 if(data->u.dlgw->lStructSize != sizeof(PAGESETUPDLGW))
3806 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
3807 return FALSE;
3810 if ((pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK) &&
3811 (pagesetup_get_hook(data, page_paint_hook) == NULL))
3813 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
3814 return FALSE;
3817 if(!(pagesetup_get_flags(data) & (PSD_INTHOUSANDTHSOFINCHES | PSD_INHUNDREDTHSOFMILLIMETERS)))
3818 data->u.dlgw->Flags |= is_default_metric() ?
3819 PSD_INHUNDREDTHSOFMILLIMETERS : PSD_INTHOUSANDTHSOFINCHES;
3821 if (!data->u.dlgw->hDevMode || !data->u.dlgw->hDevNames)
3823 WCHAR *def = get_default_printer();
3824 if(!def)
3826 if (!(pagesetup_get_flags(data) & PSD_NOWARNING))
3828 WCHAR errstr[256];
3829 LoadStringW(COMDLG32_hInstance, PD32_NO_DEFAULT_PRINTER, errstr, 255);
3830 MessageBoxW(data->u.dlgw->hwndOwner, errstr, 0, MB_OK | MB_ICONERROR);
3832 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3833 return FALSE;
3835 pagesetup_change_printer(def, data);
3836 free(def);
3839 if (pagesetup_get_flags(data) & PSD_RETURNDEFAULT)
3841 pagesetup_update_papersize(data);
3842 return TRUE;
3845 tmpl = pagesetup_get_template(data);
3847 ret = DialogBoxIndirectParamW(data->u.dlgw->hInstance, tmpl,
3848 data->u.dlgw->hwndOwner,
3849 pagesetup_dlg_proc, (LPARAM)data) > 0;
3850 return ret;
3853 /***********************************************************************
3854 * PageSetupDlgA (COMDLG32.@)
3856 * Displays the PAGE SETUP dialog box, which enables the user to specify
3857 * specific properties of a printed page such as
3858 * size, source, orientation and the width of the page margins.
3860 * PARAMS
3861 * setupdlg [IO] PAGESETUPDLGA struct
3863 * RETURNS
3864 * TRUE if the user pressed the OK button
3865 * FALSE if the user cancelled the window or an error occurred
3867 * NOTES
3868 * The values of hDevMode and hDevNames are filled on output and can be
3869 * changed in PAGESETUPDLG when they are passed in PageSetupDlg.
3872 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg)
3874 pagesetup_data data;
3876 data.unicode = FALSE;
3877 data.u.dlga = setupdlg;
3879 return pagesetup_common(&data);
3882 /***********************************************************************
3883 * PageSetupDlgW (COMDLG32.@)
3885 * See PageSetupDlgA.
3887 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg)
3889 pagesetup_data data;
3891 data.unicode = TRUE;
3892 data.u.dlgw = setupdlg;
3894 return pagesetup_common(&data);
3897 static void pdlgex_to_pdlg(const PRINTDLGEXW *pdlgex, PRINTDLGW *pdlg)
3899 pdlg->lStructSize = sizeof(*pdlg);
3900 pdlg->hwndOwner = pdlgex->hwndOwner;
3901 pdlg->hDevMode = pdlgex->hDevMode;
3902 pdlg->hDevNames = pdlgex->hDevNames;
3903 pdlg->hDC = pdlgex->hDC;
3904 pdlg->Flags = pdlgex->Flags;
3905 if ((pdlgex->Flags & PD_NOPAGENUMS) || !pdlgex->nPageRanges || !pdlgex->lpPageRanges)
3907 pdlg->nFromPage = 0;
3908 pdlg->nToPage = 65534;
3910 else
3912 pdlg->nFromPage = pdlgex->lpPageRanges[0].nFromPage;
3913 pdlg->nToPage = pdlgex->lpPageRanges[0].nToPage;
3915 pdlg->nMinPage = pdlgex->nMinPage;
3916 pdlg->nMaxPage = pdlgex->nMaxPage;
3917 pdlg->nCopies = pdlgex->nCopies;
3918 pdlg->hInstance = pdlgex->hInstance;
3919 pdlg->lCustData = 0;
3920 pdlg->lpfnPrintHook = NULL;
3921 pdlg->lpfnSetupHook = NULL;
3922 pdlg->lpPrintTemplateName = pdlgex->lpPrintTemplateName;
3923 pdlg->lpSetupTemplateName = NULL;
3924 pdlg->hPrintTemplate = NULL;
3925 pdlg->hSetupTemplate = NULL;
3928 /* Only copy fields that are supposed to be changed. */
3929 static void pdlg_to_pdlgex(const PRINTDLGW *pdlg, PRINTDLGEXW *pdlgex)
3931 pdlgex->hDevMode = pdlg->hDevMode;
3932 pdlgex->hDevNames = pdlg->hDevNames;
3933 pdlgex->hDC = pdlg->hDC;
3934 if (!(pdlgex->Flags & PD_NOPAGENUMS) && pdlgex->nPageRanges && pdlgex->lpPageRanges)
3936 pdlgex->lpPageRanges[0].nFromPage = pdlg->nFromPage;
3937 pdlgex->lpPageRanges[0].nToPage = pdlg->nToPage;
3939 pdlgex->nMinPage = pdlg->nMinPage;
3940 pdlgex->nMaxPage = pdlg->nMaxPage;
3941 pdlgex->nCopies = pdlg->nCopies;
3944 struct callback_data
3946 IPrintDialogCallback *callback;
3947 IObjectWithSite *object;
3950 static UINT_PTR CALLBACK pdlgex_hook_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
3952 if (msg == WM_INITDIALOG)
3954 PRINTDLGW *pd = (PRINTDLGW *)lp;
3955 struct callback_data *cb = (struct callback_data *)pd->lCustData;
3957 if (cb->callback)
3959 cb->callback->lpVtbl->SelectionChange(cb->callback);
3960 cb->callback->lpVtbl->InitDone(cb->callback);
3963 else
3965 /* FIXME: store interface pointer somewhere in window properties and call it
3966 HRESULT hres;
3967 cb->callback->lpVtbl->HandleMessage(cb->callback, hwnd, msg, wp, lp, &hres);
3971 return 0;
3974 /***********************************************************************
3975 * PrintDlgExA (COMDLG32.@)
3977 * See PrintDlgExW.
3979 * BUGS
3980 * Only a Stub
3983 HRESULT WINAPI PrintDlgExA(LPPRINTDLGEXA lppd)
3985 PRINTER_INFO_2A *pbuf;
3986 DRIVER_INFO_3A *dbuf;
3987 DEVMODEA *dm;
3988 HRESULT hr = S_OK;
3989 HANDLE hprn;
3991 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXA)))
3992 return E_INVALIDARG;
3994 if (!IsWindow(lppd->hwndOwner))
3995 return E_HANDLE;
3997 if (lppd->nStartPage != START_PAGE_GENERAL)
3999 if (!lppd->nPropertyPages)
4000 return E_INVALIDARG;
4002 FIXME("custom property sheets (%ld at %p) not supported\n", lppd->nPropertyPages, lppd->lphPropertyPages);
4005 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
4006 if (!(lppd->Flags & PD_NOPAGENUMS) && (!lppd->nMaxPageRanges || !lppd->lpPageRanges))
4008 return E_INVALIDARG;
4011 if (lppd->Flags & PD_RETURNDEFAULT)
4013 if (lppd->hDevMode || lppd->hDevNames)
4015 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
4016 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4017 return E_INVALIDARG;
4019 if (!PRINTDLG_OpenDefaultPrinter(&hprn))
4021 WARN("Can't find default printer\n");
4022 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
4023 return E_FAIL;
4026 pbuf = get_printer_infoA(hprn);
4027 if (!pbuf)
4029 ClosePrinter(hprn);
4030 return E_FAIL;
4033 dbuf = get_driver_infoA(hprn);
4034 if (!dbuf)
4036 free(pbuf);
4037 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4038 ClosePrinter(hprn);
4039 return E_FAIL;
4041 dm = pbuf->pDevMode;
4043 else
4045 PRINTDLGA pdlg;
4046 struct callback_data cb_data = { 0 };
4048 FIXME("(%p) semi-stub\n", lppd);
4050 if (lppd->lpCallback)
4052 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IPrintDialogCallback, (void **)&cb_data.callback);
4053 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IObjectWithSite, (void **)&cb_data.object);
4057 * PRINTDLGEXA/W and PRINTDLGA/W layout is the same for A and W variants.
4059 pdlgex_to_pdlg((const PRINTDLGEXW *)lppd, (PRINTDLGW *)&pdlg);
4060 pdlg.Flags |= PD_ENABLEPRINTHOOK;
4061 pdlg.lpfnPrintHook = pdlgex_hook_proc;
4062 pdlg.lCustData = (LPARAM)&cb_data;
4064 if (PrintDlgA(&pdlg))
4066 pdlg_to_pdlgex((const PRINTDLGW *)&pdlg, (PRINTDLGEXW *)lppd);
4067 lppd->dwResultAction = PD_RESULT_PRINT;
4069 else
4070 lppd->dwResultAction = PD_RESULT_CANCEL;
4072 if (cb_data.callback)
4073 cb_data.callback->lpVtbl->Release(cb_data.callback);
4074 if (cb_data.object)
4075 cb_data.object->lpVtbl->Release(cb_data.object);
4077 return S_OK;
4080 ClosePrinter(hprn);
4082 PRINTDLG_CreateDevNames(&(lppd->hDevNames), dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName);
4083 if (!lppd->hDevNames)
4084 hr = E_FAIL;
4086 lppd->hDevMode = update_devmode_handleA(lppd->hDevMode, dm);
4087 if (hr == S_OK && lppd->hDevMode) {
4088 if (lppd->Flags & PD_RETURNDC) {
4089 lppd->hDC = CreateDCA(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4090 if (!lppd->hDC)
4091 hr = E_FAIL;
4093 else if (lppd->Flags & PD_RETURNIC) {
4094 lppd->hDC = CreateICA(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4095 if (!lppd->hDC)
4096 hr = E_FAIL;
4099 else
4100 hr = E_FAIL;
4102 free(pbuf);
4103 free(dbuf);
4105 return hr;
4108 /***********************************************************************
4109 * PrintDlgExW (COMDLG32.@)
4111 * Display the property sheet style PRINT dialog box
4113 * PARAMS
4114 * lppd [IO] ptr to PRINTDLGEX struct
4116 * RETURNS
4117 * Success: S_OK
4118 * Failure: One of the following COM error codes:
4119 * E_OUTOFMEMORY Insufficient memory.
4120 * E_INVALIDARG One or more arguments are invalid.
4121 * E_POINTER Invalid pointer.
4122 * E_HANDLE Invalid handle.
4123 * E_FAIL Unspecified error.
4125 * NOTES
4126 * This Dialog enables the user to specify specific properties of the print job.
4127 * The property sheet can also have additional application-specific and
4128 * driver-specific property pages.
4130 * BUGS
4131 * Not fully implemented
4134 HRESULT WINAPI PrintDlgExW(LPPRINTDLGEXW lppd)
4136 PRINTER_INFO_2W *pbuf;
4137 DRIVER_INFO_3W *dbuf;
4138 DEVMODEW *dm;
4139 HRESULT hr = S_OK;
4140 HANDLE hprn;
4142 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXW))) {
4143 return E_INVALIDARG;
4146 if (!IsWindow(lppd->hwndOwner)) {
4147 return E_HANDLE;
4150 if (lppd->nStartPage != START_PAGE_GENERAL)
4152 if (!lppd->nPropertyPages)
4153 return E_INVALIDARG;
4155 FIXME("custom property sheets (%ld at %p) not supported\n", lppd->nPropertyPages, lppd->lphPropertyPages);
4158 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
4159 if (!(lppd->Flags & PD_NOPAGENUMS) && (!lppd->nMaxPageRanges || !lppd->lpPageRanges))
4161 return E_INVALIDARG;
4164 if (lppd->Flags & PD_RETURNDEFAULT) {
4166 if (lppd->hDevMode || lppd->hDevNames) {
4167 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
4168 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4169 return E_INVALIDARG;
4171 if (!PRINTDLG_OpenDefaultPrinter(&hprn)) {
4172 WARN("Can't find default printer\n");
4173 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
4174 return E_FAIL;
4177 pbuf = get_printer_infoW(hprn);
4178 if (!pbuf)
4180 ClosePrinter(hprn);
4181 return E_FAIL;
4184 dbuf = get_driver_infoW(hprn);
4185 if (!dbuf)
4187 free(pbuf);
4188 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
4189 ClosePrinter(hprn);
4190 return E_FAIL;
4192 dm = pbuf->pDevMode;
4194 else
4196 PRINTDLGW pdlg;
4197 struct callback_data cb_data = { 0 };
4199 FIXME("(%p) semi-stub\n", lppd);
4201 if (lppd->lpCallback)
4203 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IPrintDialogCallback, (void **)&cb_data.callback);
4204 IUnknown_QueryInterface((IUnknown *)lppd->lpCallback, &IID_IObjectWithSite, (void **)&cb_data.object);
4207 pdlgex_to_pdlg(lppd, &pdlg);
4208 pdlg.Flags |= PD_ENABLEPRINTHOOK;
4209 pdlg.lpfnPrintHook = pdlgex_hook_proc;
4210 pdlg.lCustData = (LPARAM)&cb_data;
4212 if (PrintDlgW(&pdlg))
4214 pdlg_to_pdlgex(&pdlg, lppd);
4215 lppd->dwResultAction = PD_RESULT_PRINT;
4217 else
4218 lppd->dwResultAction = PD_RESULT_CANCEL;
4220 if (cb_data.callback)
4221 cb_data.callback->lpVtbl->Release(cb_data.callback);
4222 if (cb_data.object)
4223 cb_data.object->lpVtbl->Release(cb_data.object);
4225 return S_OK;
4228 ClosePrinter(hprn);
4230 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames), dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName);
4231 if (!lppd->hDevNames)
4232 hr = E_FAIL;
4234 lppd->hDevMode = update_devmode_handleW(lppd->hDevMode, dm);
4235 if (hr == S_OK && lppd->hDevMode) {
4236 if (lppd->Flags & PD_RETURNDC) {
4237 lppd->hDC = CreateDCW(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4238 if (!lppd->hDC)
4239 hr = E_FAIL;
4241 else if (lppd->Flags & PD_RETURNIC) {
4242 lppd->hDC = CreateICW(dbuf->pDriverPath, pbuf->pPrinterName, pbuf->pPortName, dm);
4243 if (!lppd->hDC)
4244 hr = E_FAIL;
4247 else
4248 hr = E_FAIL;
4250 free(pbuf);
4251 free(dbuf);
4253 return hr;