push eb25bf65c4616aa55a810ed5c29198b1a080b208
[wine/hacks.git] / dlls / comdlg32 / printdlg.c
blob6726c6a9cba44147e9a71a3549728ff5039e9038
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winspool.h"
37 #include "winerror.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 #include "commdlg.h"
43 #include "dlgs.h"
44 #include "cderr.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
48 #include "cdlg.h"
49 #include "printdlg.h"
51 /* Yes these constants are the same, but we're just copying win98 */
52 #define UPDOWN_ID 0x270f
53 #define MAX_COPIES 9999
55 /* Debugging info */
56 static const struct pd_flags psd_flags[] = {
57 {PSD_MINMARGINS,"PSD_MINMARGINS"},
58 {PSD_MARGINS,"PSD_MARGINS"},
59 {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
60 {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
61 {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
62 {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
63 {PSD_NOWARNING,"PSD_NOWARNING"},
64 {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
65 {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
66 {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
67 {PSD_SHOWHELP,"PSD_SHOWHELP"},
68 {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
69 {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
70 {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
71 {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
72 {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
73 {-1, NULL}
76 /* address of wndproc for subclassed Static control */
77 static WNDPROC lpfnStaticWndProc;
78 static WNDPROC edit_wndproc;
79 /* the text of the fake document to render for the Page Setup dialog */
80 static WCHAR wszFakeDocumentText[1024];
81 static const WCHAR pd32_collateW[] = { 'P', 'D', '3', '2', '_', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
82 static const WCHAR pd32_nocollateW[] = { 'P', 'D', '3', '2', '_', 'N', 'O', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
83 static const WCHAR pd32_portraitW[] = { 'P', 'D', '3', '2', '_', 'P', 'O', 'R', 'T', 'R', 'A', 'I', 'T', 0 };
84 static const WCHAR pd32_landscapeW[] = { 'P', 'D', '3', '2', '_', 'L', 'A', 'N', 'D', 'S', 'C', 'A', 'P', 'E', 0 };
85 static const WCHAR printdlg_prop[] = {'_','_','W','I','N','E','_','P','R','I','N','T','D','L','G','D','A','T','A',0};
86 static const WCHAR pagesetupdlg_prop[] = { '_', '_', 'W', 'I', 'N', 'E', '_', 'P', 'A', 'G', 'E',
87 'S', 'E', 'T', 'U', 'P', 'D', 'L', 'G', 'D', 'A', 'T', 'A', 0 };
90 static LPWSTR strdupW(LPCWSTR p)
92 LPWSTR ret;
93 DWORD len;
95 if(!p) return NULL;
96 len = (strlenW(p) + 1) * sizeof(WCHAR);
97 ret = HeapAlloc(GetProcessHeap(), 0, len);
98 memcpy(ret, p, len);
99 return ret;
102 /***********************************************************
103 * convert_to_devmodeA
105 * Creates an ansi copy of supplied devmode
107 static DEVMODEA *convert_to_devmodeA(const DEVMODEW *dmW)
109 DEVMODEA *dmA;
110 DWORD size;
112 if (!dmW) return NULL;
113 size = dmW->dmSize - CCHDEVICENAME -
114 ((dmW->dmSize > FIELD_OFFSET(DEVMODEW, dmFormName)) ? CCHFORMNAME : 0);
116 dmA = HeapAlloc(GetProcessHeap(), 0, size + dmW->dmDriverExtra);
117 if (!dmA) return NULL;
119 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1,
120 (LPSTR)dmA->dmDeviceName, CCHDEVICENAME, NULL, NULL);
122 if (FIELD_OFFSET(DEVMODEW, dmFormName) >= dmW->dmSize)
124 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
125 dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
127 else
129 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
130 FIELD_OFFSET(DEVMODEW, dmFormName) - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
131 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1,
132 (LPSTR)dmA->dmFormName, CCHFORMNAME, NULL, NULL);
134 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmLogPixels));
137 dmA->dmSize = size;
138 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize, dmW->dmDriverExtra);
139 return dmA;
142 /***********************************************************************
143 * PRINTDLG_OpenDefaultPrinter
145 * Returns a winspool printer handle to the default printer in *hprn
146 * Caller must call ClosePrinter on the handle
148 * Returns TRUE on success else FALSE
150 BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
152 WCHAR buf[260];
153 DWORD dwBufLen = sizeof(buf) / sizeof(buf[0]);
154 BOOL res;
155 if(!GetDefaultPrinterW(buf, &dwBufLen))
156 return FALSE;
157 res = OpenPrinterW(buf, hprn, NULL);
158 if (!res)
159 WARN("Could not open printer %s\n", debugstr_w(buf));
160 return res;
163 /***********************************************************************
164 * PRINTDLG_SetUpPrinterListCombo
166 * Initializes printer list combox.
167 * hDlg: HWND of dialog
168 * id: Control id of combo
169 * name: Name of printer to select
171 * Initializes combo with list of available printers. Selects printer 'name'
172 * If name is NULL or does not exist select the default printer.
174 * Returns number of printers added to list.
176 INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
178 DWORD needed, num;
179 INT i;
180 LPPRINTER_INFO_2A pi;
181 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
182 pi = HeapAlloc(GetProcessHeap(), 0, needed);
183 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
184 &num);
186 SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
188 for(i = 0; i < num; i++) {
189 SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
190 (LPARAM)pi[i].pPrinterName );
192 HeapFree(GetProcessHeap(), 0, pi);
193 if(!name ||
194 (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
195 (LPARAM)name)) == CB_ERR) {
197 char buf[260];
198 DWORD dwBufLen = sizeof(buf);
199 if (name != NULL)
200 WARN("Can't find %s in printer list so trying to find default\n",
201 debugstr_a(name));
202 if(!GetDefaultPrinterA(buf, &dwBufLen))
203 return num;
204 i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
205 if(i == CB_ERR)
206 FIXME("Can't find default printer in printer list\n");
208 SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
209 return num;
212 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
214 DWORD needed, num;
215 INT i;
216 LPPRINTER_INFO_2W pi;
217 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
218 pi = HeapAlloc(GetProcessHeap(), 0, needed);
219 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
220 &num);
222 for(i = 0; i < num; i++) {
223 SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
224 (LPARAM)pi[i].pPrinterName );
226 HeapFree(GetProcessHeap(), 0, pi);
227 if(!name ||
228 (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
229 (LPARAM)name)) == CB_ERR) {
230 WCHAR buf[260];
231 DWORD dwBufLen = sizeof(buf)/sizeof(buf[0]);
232 if (name != NULL)
233 WARN("Can't find %s in printer list so trying to find default\n",
234 debugstr_w(name));
235 if(!GetDefaultPrinterW(buf, &dwBufLen))
236 return num;
237 i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
238 if(i == CB_ERR)
239 TRACE("Can't find default printer in printer list\n");
241 SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
242 return num;
245 /***********************************************************************
246 * PRINTDLG_CreateDevNames [internal]
249 * creates a DevNames structure.
251 * (NB. when we handle unicode the offsets will be in wchars).
253 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
254 const char* DeviceName, const char* OutputPort)
256 long size;
257 char* pDevNamesSpace;
258 char* pTempPtr;
259 LPDEVNAMES lpDevNames;
260 char buf[260];
261 DWORD dwBufLen = sizeof(buf);
263 size = strlen(DeviceDriverName) + 1
264 + strlen(DeviceName) + 1
265 + strlen(OutputPort) + 1
266 + sizeof(DEVNAMES);
268 if(*hmem)
269 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
270 else
271 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
272 if (*hmem == 0)
273 return FALSE;
275 pDevNamesSpace = GlobalLock(*hmem);
276 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
278 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
279 strcpy(pTempPtr, DeviceDriverName);
280 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
282 pTempPtr += strlen(DeviceDriverName) + 1;
283 strcpy(pTempPtr, DeviceName);
284 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
286 pTempPtr += strlen(DeviceName) + 1;
287 strcpy(pTempPtr, OutputPort);
288 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
290 GetDefaultPrinterA(buf, &dwBufLen);
291 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
292 GlobalUnlock(*hmem);
293 return TRUE;
296 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
297 LPCWSTR DeviceName, LPCWSTR OutputPort)
299 long size;
300 LPWSTR pDevNamesSpace;
301 LPWSTR pTempPtr;
302 LPDEVNAMES lpDevNames;
303 WCHAR bufW[260];
304 DWORD dwBufLen = sizeof(bufW) / sizeof(WCHAR);
306 size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
307 + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
308 + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
309 + sizeof(DEVNAMES);
311 if(*hmem)
312 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
313 else
314 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
315 if (*hmem == 0)
316 return FALSE;
318 pDevNamesSpace = GlobalLock(*hmem);
319 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
321 pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
322 lstrcpyW(pTempPtr, DeviceDriverName);
323 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
325 pTempPtr += lstrlenW(DeviceDriverName) + 1;
326 lstrcpyW(pTempPtr, DeviceName);
327 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
329 pTempPtr += lstrlenW(DeviceName) + 1;
330 lstrcpyW(pTempPtr, OutputPort);
331 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
333 GetDefaultPrinterW(bufW, &dwBufLen);
334 lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
335 GlobalUnlock(*hmem);
336 return TRUE;
339 /***********************************************************************
340 * PRINTDLG_UpdatePrintDlg [internal]
343 * updates the PrintDlg structure for return values.
345 * RETURNS
346 * FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
347 * TRUE if successful.
349 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
350 PRINT_PTRA* PrintStructures)
352 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
353 PDEVMODEA lpdm = PrintStructures->lpDevMode;
354 LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
357 if(!lpdm) {
358 FIXME("No lpdm ptr?\n");
359 return FALSE;
363 if(!(lppd->Flags & PD_PRINTSETUP)) {
364 /* check whether nFromPage and nToPage are within range defined by
365 * nMinPage and nMaxPage
367 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
368 WORD nToPage;
369 WORD nFromPage;
370 BOOL translated;
371 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
372 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
374 /* if no ToPage value is entered, use the FromPage value */
375 if(!translated) nToPage = nFromPage;
377 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
378 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
379 WCHAR resourcestr[256];
380 WCHAR resultstr[256];
381 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
382 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
383 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
384 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
385 return FALSE;
387 lppd->nFromPage = nFromPage;
388 lppd->nToPage = nToPage;
389 lppd->Flags |= PD_PAGENUMS;
391 else
392 lppd->Flags &= ~PD_PAGENUMS;
394 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
395 lppd->Flags |= PD_SELECTION;
396 else
397 lppd->Flags &= ~PD_SELECTION;
399 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
400 static char file[] = "FILE:";
401 lppd->Flags |= PD_PRINTTOFILE;
402 pi->pPortName = file;
405 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
406 FIXME("Collate lppd not yet implemented as output\n");
409 /* set PD_Collate and nCopies */
410 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
411 /* The application doesn't support multiple copies or collate...
413 lppd->Flags &= ~PD_COLLATE;
414 lppd->nCopies = 1;
415 /* if the printer driver supports it... store info there
416 * otherwise no collate & multiple copies !
418 if (lpdm->dmFields & DM_COLLATE)
419 lpdm->dmCollate =
420 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
421 if (lpdm->dmFields & DM_COPIES)
422 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
423 } else {
424 /* Application is responsible for multiple copies */
425 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
426 lppd->Flags |= PD_COLLATE;
427 else
428 lppd->Flags &= ~PD_COLLATE;
429 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
430 /* multiple copies already included in the document. Driver must print only one copy */
431 lpdm->u1.s1.dmCopies = 1;
434 /* Print quality, PrintDlg16 */
435 if(GetDlgItem(hDlg, cmb1))
437 HWND hQuality = GetDlgItem(hDlg, cmb1);
438 int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
440 if(Sel != CB_ERR)
442 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
443 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
444 lpdm->u1.s1.dmPrintQuality = LOWORD(dpi);
445 lpdm->dmYResolution = HIWORD(dpi);
449 return TRUE;
452 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
453 PRINT_PTRW* PrintStructures)
455 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
456 PDEVMODEW lpdm = PrintStructures->lpDevMode;
457 LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
460 if(!lpdm) {
461 FIXME("No lpdm ptr?\n");
462 return FALSE;
466 if(!(lppd->Flags & PD_PRINTSETUP)) {
467 /* check whether nFromPage and nToPage are within range defined by
468 * nMinPage and nMaxPage
470 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
471 WORD nToPage;
472 WORD nFromPage;
473 BOOL translated;
474 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
475 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
477 /* if no ToPage value is entered, use the FromPage value */
478 if(!translated) nToPage = nFromPage;
480 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
481 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
482 WCHAR resourcestr[256];
483 WCHAR resultstr[256];
484 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
485 resourcestr, 255);
486 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
487 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
488 resourcestr, 255);
489 MessageBoxW(hDlg, resultstr, resourcestr,
490 MB_OK | MB_ICONWARNING);
491 return FALSE;
493 lppd->nFromPage = nFromPage;
494 lppd->nToPage = nToPage;
495 lppd->Flags |= PD_PAGENUMS;
497 else
498 lppd->Flags &= ~PD_PAGENUMS;
500 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
501 lppd->Flags |= PD_SELECTION;
502 else
503 lppd->Flags &= ~PD_SELECTION;
505 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
506 static WCHAR file[] = {'F','I','L','E',':',0};
507 lppd->Flags |= PD_PRINTTOFILE;
508 pi->pPortName = file;
511 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
512 FIXME("Collate lppd not yet implemented as output\n");
515 /* set PD_Collate and nCopies */
516 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
517 /* The application doesn't support multiple copies or collate...
519 lppd->Flags &= ~PD_COLLATE;
520 lppd->nCopies = 1;
521 /* if the printer driver supports it... store info there
522 * otherwise no collate & multiple copies !
524 if (lpdm->dmFields & DM_COLLATE)
525 lpdm->dmCollate =
526 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
527 if (lpdm->dmFields & DM_COPIES)
528 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
529 } else {
530 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
531 lppd->Flags |= PD_COLLATE;
532 else
533 lppd->Flags &= ~PD_COLLATE;
534 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
537 return TRUE;
540 /************************************************************************
541 * PRINTDLG_SetUpPaperComboBox
543 * Initialize either the papersize or inputslot combos of the Printer Setup
544 * dialog. We store the associated word (eg DMPAPER_A4) as the item data.
545 * We also try to re-select the old selection.
547 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
548 int nIDComboBox,
549 char* PrinterName,
550 char* PortName,
551 LPDEVMODEA dm)
553 int i;
554 int NrOfEntries;
555 char* Names;
556 WORD* Words;
557 DWORD Sel;
558 WORD oldWord = 0;
559 int NamesSize;
560 int fwCapability_Names;
561 int fwCapability_Words;
563 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
565 /* query the dialog box for the current selected value */
566 Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
567 if(Sel != CB_ERR) {
568 /* we enter here only if a different printer is selected after
569 * the Print Setup dialog is opened. The current settings are
570 * stored into the newly selected printer.
572 oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
573 Sel, 0);
574 if (dm) {
575 if (nIDComboBox == cmb2)
576 dm->u1.s1.dmPaperSize = oldWord;
577 else
578 dm->u1.s1.dmDefaultSource = oldWord;
581 else {
582 /* we enter here only when the Print setup dialog is initially
583 * opened. In this case the settings are restored from when
584 * the dialog was last closed.
586 if (dm) {
587 if (nIDComboBox == cmb2)
588 oldWord = dm->u1.s1.dmPaperSize;
589 else
590 oldWord = dm->u1.s1.dmDefaultSource;
594 if (nIDComboBox == cmb2) {
595 NamesSize = 64;
596 fwCapability_Names = DC_PAPERNAMES;
597 fwCapability_Words = DC_PAPERS;
598 } else {
599 nIDComboBox = cmb3;
600 NamesSize = 24;
601 fwCapability_Names = DC_BINNAMES;
602 fwCapability_Words = DC_BINS;
605 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
606 fwCapability_Names, NULL, dm);
607 if (NrOfEntries == 0)
608 WARN("no Name Entries found!\n");
609 else if (NrOfEntries < 0)
610 return FALSE;
612 if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
613 != NrOfEntries) {
614 ERR("Number of caps is different\n");
615 NrOfEntries = 0;
618 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
619 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
620 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
621 fwCapability_Names, Names, dm);
622 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
623 fwCapability_Words, (LPSTR)Words, dm);
625 /* reset any current content in the combobox */
626 SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
628 /* store new content */
629 for (i = 0; i < NrOfEntries; i++) {
630 DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
631 (LPARAM)(&Names[i*NamesSize]) );
632 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
633 Words[i]);
636 /* Look for old selection - can't do this is previous loop since
637 item order will change as more items are added */
638 Sel = 0;
639 for (i = 0; i < NrOfEntries; i++) {
640 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
641 oldWord) {
642 Sel = i;
643 break;
646 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
648 HeapFree(GetProcessHeap(),0,Words);
649 HeapFree(GetProcessHeap(),0,Names);
650 return TRUE;
653 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
654 int nIDComboBox,
655 const WCHAR* PrinterName,
656 const WCHAR* PortName,
657 LPDEVMODEW dm)
659 int i;
660 int NrOfEntries;
661 WCHAR* Names;
662 WORD* Words;
663 DWORD Sel;
664 WORD oldWord = 0;
665 int NamesSize;
666 int fwCapability_Names;
667 int fwCapability_Words;
669 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
671 /* query the dialog box for the current selected value */
672 Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
673 if(Sel != CB_ERR) {
674 /* we enter here only if a different printer is selected after
675 * the Print Setup dialog is opened. The current settings are
676 * stored into the newly selected printer.
678 oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
679 Sel, 0);
680 if (dm) {
681 if (nIDComboBox == cmb2)
682 dm->u1.s1.dmPaperSize = oldWord;
683 else
684 dm->u1.s1.dmDefaultSource = oldWord;
687 else {
688 /* we enter here only when the Print setup dialog is initially
689 * opened. In this case the settings are restored from when
690 * the dialog was last closed.
692 if (dm) {
693 if (nIDComboBox == cmb2)
694 oldWord = dm->u1.s1.dmPaperSize;
695 else
696 oldWord = dm->u1.s1.dmDefaultSource;
700 if (nIDComboBox == cmb2) {
701 NamesSize = 64;
702 fwCapability_Names = DC_PAPERNAMES;
703 fwCapability_Words = DC_PAPERS;
704 } else {
705 nIDComboBox = cmb3;
706 NamesSize = 24;
707 fwCapability_Names = DC_BINNAMES;
708 fwCapability_Words = DC_BINS;
711 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
712 fwCapability_Names, NULL, dm);
713 if (NrOfEntries == 0)
714 WARN("no Name Entries found!\n");
715 else if (NrOfEntries < 0)
716 return FALSE;
718 if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
719 != NrOfEntries) {
720 ERR("Number of caps is different\n");
721 NrOfEntries = 0;
724 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
725 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
726 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
727 fwCapability_Names, Names, dm);
728 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
729 fwCapability_Words, Words, dm);
731 /* reset any current content in the combobox */
732 SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
734 /* store new content */
735 for (i = 0; i < NrOfEntries; i++) {
736 DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
737 (LPARAM)(&Names[i*NamesSize]) );
738 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
739 Words[i]);
742 /* Look for old selection - can't do this is previous loop since
743 item order will change as more items are added */
744 Sel = 0;
745 for (i = 0; i < NrOfEntries; i++) {
746 if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
747 oldWord) {
748 Sel = i;
749 break;
752 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
754 HeapFree(GetProcessHeap(),0,Words);
755 HeapFree(GetProcessHeap(),0,Names);
756 return TRUE;
760 /***********************************************************************
761 * PRINTDLG_UpdatePrinterInfoTexts [internal]
763 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
765 char StatusMsg[256];
766 char ResourceString[256];
767 int i;
769 /* Status Message */
770 StatusMsg[0]='\0';
772 /* add all status messages */
773 for (i = 0; i < 25; i++) {
774 if (pi->Status & (1<<i)) {
775 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
776 ResourceString, 255);
777 strcat(StatusMsg,ResourceString);
780 /* append "ready" */
781 /* FIXME: status==ready must only be appended if really so.
782 but how to detect? */
783 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
784 ResourceString, 255);
785 strcat(StatusMsg,ResourceString);
786 SetDlgItemTextA(hDlg, stc12, StatusMsg);
788 /* set all other printer info texts */
789 SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
791 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
792 SetDlgItemTextA(hDlg, stc14, pi->pLocation);
793 else
794 SetDlgItemTextA(hDlg, stc14, pi->pPortName);
795 SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
796 return;
799 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
801 WCHAR StatusMsg[256];
802 WCHAR ResourceString[256];
803 static const WCHAR emptyW[] = {0};
804 int i;
806 /* Status Message */
807 StatusMsg[0]='\0';
809 /* add all status messages */
810 for (i = 0; i < 25; i++) {
811 if (pi->Status & (1<<i)) {
812 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
813 ResourceString, 255);
814 lstrcatW(StatusMsg,ResourceString);
817 /* append "ready" */
818 /* FIXME: status==ready must only be appended if really so.
819 but how to detect? */
820 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
821 ResourceString, 255);
822 lstrcatW(StatusMsg,ResourceString);
823 SetDlgItemTextW(hDlg, stc12, StatusMsg);
825 /* set all other printer info texts */
826 SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
827 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
828 SetDlgItemTextW(hDlg, stc14, pi->pLocation);
829 else
830 SetDlgItemTextW(hDlg, stc14, pi->pPortName);
831 SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : emptyW);
835 /*******************************************************************
837 * PRINTDLG_ChangePrinter
840 BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name,
841 PRINT_PTRA *PrintStructures)
843 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
844 LPDEVMODEA lpdm = NULL;
845 LONG dmSize;
846 DWORD needed;
847 HANDLE hprn;
849 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
850 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
851 if(!OpenPrinterA(name, &hprn, NULL)) {
852 ERR("Can't open printer %s\n", name);
853 return FALSE;
855 GetPrinterA(hprn, 2, NULL, 0, &needed);
856 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
857 GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
858 &needed);
859 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
860 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
861 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
862 needed, &needed)) {
863 ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
864 return FALSE;
866 ClosePrinter(hprn);
868 PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
870 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
871 PrintStructures->lpDevMode = NULL;
873 dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
874 if(dmSize == -1) {
875 ERR("DocumentProperties fails on %s\n", debugstr_a(name));
876 return FALSE;
878 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
879 dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
880 DM_OUT_BUFFER);
881 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
882 !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
883 (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
884 /* Supplied devicemode matches current printer so try to use it */
885 DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
886 DM_OUT_BUFFER | DM_IN_BUFFER);
888 if(lpdm)
889 GlobalUnlock(lppd->hDevMode);
891 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
893 if(!(lppd->Flags & PD_PRINTSETUP)) {
894 /* Print range (All/Range/Selection) */
895 if(lppd->nFromPage != 0xffff)
896 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
897 if(lppd->nToPage != 0xffff)
898 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
900 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
901 if (lppd->Flags & PD_NOSELECTION)
902 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
903 else
904 if (lppd->Flags & PD_SELECTION)
905 CheckRadioButton(hDlg, rad1, rad3, rad2);
906 if (lppd->Flags & PD_NOPAGENUMS) {
907 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
908 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
909 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
910 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
911 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
912 } else {
913 if (lppd->Flags & PD_PAGENUMS)
914 CheckRadioButton(hDlg, rad1, rad3, rad3);
917 /* Collate pages
919 * FIXME: The ico3 is not displayed for some reason. I don't know why.
921 if (lppd->Flags & PD_COLLATE) {
922 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
923 (LPARAM)PrintStructures->hCollateIcon);
924 CheckDlgButton(hDlg, chx2, 1);
925 } else {
926 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
927 (LPARAM)PrintStructures->hNoCollateIcon);
928 CheckDlgButton(hDlg, chx2, 0);
931 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
932 /* if printer doesn't support it: no Collate */
933 if (!(lpdm->dmFields & DM_COLLATE)) {
934 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
935 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
939 /* nCopies */
941 INT copies;
942 if (lppd->hDevMode == 0)
943 copies = lppd->nCopies;
944 else
945 copies = lpdm->u1.s1.dmCopies;
946 if(copies == 0) copies = 1;
947 else if(copies < 0) copies = MAX_COPIES;
948 SetDlgItemInt(hDlg, edt3, copies, FALSE);
951 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
952 /* if printer doesn't support it: no nCopies */
953 if (!(lpdm->dmFields & DM_COPIES)) {
954 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
955 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
959 /* print to file */
960 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
961 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
962 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
963 if (lppd->Flags & PD_HIDEPRINTTOFILE)
964 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
966 /* Fill print quality combo, PrintDlg16 */
967 if(GetDlgItem(hDlg, cmb1))
969 DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
970 PrintStructures->lpPrinterInfo->pPortName,
971 DC_ENUMRESOLUTIONS, NULL, lpdm);
973 if(numResolutions != -1)
975 HWND hQuality = GetDlgItem(hDlg, cmb1);
976 LONG* Resolutions;
977 char buf[255];
978 DWORD i;
979 int dpiX, dpiY;
980 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
981 PrintStructures->lpPrinterInfo->pPrinterName,
982 0, lpdm);
984 Resolutions = HeapAlloc(GetProcessHeap(), 0, numResolutions*sizeof(LONG)*2);
985 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
986 PrintStructures->lpPrinterInfo->pPortName,
987 DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
989 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
990 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
991 DeleteDC(hPrinterDC);
993 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
994 for(i = 0; i < (numResolutions * 2); i += 2)
996 BOOL IsDefault = FALSE;
997 LRESULT Index;
999 if(Resolutions[i] == Resolutions[i+1])
1001 if(dpiX == Resolutions[i])
1002 IsDefault = TRUE;
1003 sprintf(buf, "%d dpi", Resolutions[i]);
1004 } else
1006 if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1007 IsDefault = TRUE;
1008 sprintf(buf, "%d dpi x %d dpi", Resolutions[i], Resolutions[i+1]);
1011 Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1013 if(IsDefault)
1014 SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1016 SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1018 HeapFree(GetProcessHeap(), 0, Resolutions);
1021 } else { /* PD_PRINTSETUP */
1022 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1024 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1025 PrintStructures->lpPrinterInfo->pPrinterName,
1026 PrintStructures->lpPrinterInfo->pPortName,
1027 lpdm);
1028 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1029 PrintStructures->lpPrinterInfo->pPrinterName,
1030 PrintStructures->lpPrinterInfo->pPortName,
1031 lpdm);
1032 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1033 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1034 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1035 PrintStructures->hLandscapeIcon));
1039 /* help button */
1040 if ((lppd->Flags & PD_SHOWHELP)==0) {
1041 /* hide if PD_SHOWHELP not specified */
1042 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1044 return TRUE;
1047 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1048 PRINT_PTRW *PrintStructures)
1050 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1051 LPDEVMODEW lpdm = NULL;
1052 LONG dmSize;
1053 DWORD needed;
1054 HANDLE hprn;
1056 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1057 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1058 if(!OpenPrinterW(name, &hprn, NULL)) {
1059 ERR("Can't open printer %s\n", debugstr_w(name));
1060 return FALSE;
1062 GetPrinterW(hprn, 2, NULL, 0, &needed);
1063 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
1064 GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1065 &needed);
1066 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1067 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
1068 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1069 needed, &needed)) {
1070 ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1071 return FALSE;
1073 ClosePrinter(hprn);
1075 PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1077 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1078 PrintStructures->lpDevMode = NULL;
1080 dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1081 if(dmSize == -1) {
1082 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1083 return FALSE;
1085 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1086 dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1087 DM_OUT_BUFFER);
1088 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1089 !lstrcmpW(lpdm->dmDeviceName,
1090 PrintStructures->lpDevMode->dmDeviceName)) {
1091 /* Supplied devicemode matches current printer so try to use it */
1092 DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1093 DM_OUT_BUFFER | DM_IN_BUFFER);
1095 if(lpdm)
1096 GlobalUnlock(lppd->hDevMode);
1098 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1100 if(!(lppd->Flags & PD_PRINTSETUP)) {
1101 /* Print range (All/Range/Selection) */
1102 if(lppd->nFromPage != 0xffff)
1103 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1104 if(lppd->nToPage != 0xffff)
1105 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1107 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1108 if (lppd->Flags & PD_NOSELECTION)
1109 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1110 else
1111 if (lppd->Flags & PD_SELECTION)
1112 CheckRadioButton(hDlg, rad1, rad3, rad2);
1113 if (lppd->Flags & PD_NOPAGENUMS) {
1114 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1115 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1116 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1117 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1118 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1119 } else {
1120 if (lppd->Flags & PD_PAGENUMS)
1121 CheckRadioButton(hDlg, rad1, rad3, rad3);
1124 /* Collate pages
1126 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1128 if (lppd->Flags & PD_COLLATE) {
1129 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1130 (LPARAM)PrintStructures->hCollateIcon);
1131 CheckDlgButton(hDlg, chx2, 1);
1132 } else {
1133 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1134 (LPARAM)PrintStructures->hNoCollateIcon);
1135 CheckDlgButton(hDlg, chx2, 0);
1138 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1139 /* if printer doesn't support it: no Collate */
1140 if (!(lpdm->dmFields & DM_COLLATE)) {
1141 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1142 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1146 /* nCopies */
1148 INT copies;
1149 if (lppd->hDevMode == 0)
1150 copies = lppd->nCopies;
1151 else
1152 copies = lpdm->u1.s1.dmCopies;
1153 if(copies == 0) copies = 1;
1154 else if(copies < 0) copies = MAX_COPIES;
1155 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1158 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1159 /* if printer doesn't support it: no nCopies */
1160 if (!(lpdm->dmFields & DM_COPIES)) {
1161 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1162 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1166 /* print to file */
1167 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1168 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1169 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1170 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1171 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1173 } else { /* PD_PRINTSETUP */
1174 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1176 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1177 PrintStructures->lpPrinterInfo->pPrinterName,
1178 PrintStructures->lpPrinterInfo->pPortName,
1179 lpdm);
1180 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1181 PrintStructures->lpPrinterInfo->pPrinterName,
1182 PrintStructures->lpPrinterInfo->pPortName,
1183 lpdm);
1184 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1185 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1186 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1187 PrintStructures->hLandscapeIcon));
1191 /* help button */
1192 if ((lppd->Flags & PD_SHOWHELP)==0) {
1193 /* hide if PD_SHOWHELP not specified */
1194 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1196 return TRUE;
1199 /***********************************************************************
1200 * check_printer_setup [internal]
1202 static LRESULT check_printer_setup(HWND hDlg)
1204 DWORD needed,num;
1205 WCHAR resourcestr[256],resultstr[256];
1207 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1208 if(needed == 0)
1210 EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1212 if(needed > 0)
1213 return TRUE;
1214 else
1216 LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1217 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1218 MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1219 return FALSE;
1223 /***********************************************************************
1224 * PRINTDLG_WMInitDialog [internal]
1226 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
1227 PRINT_PTRA* PrintStructures)
1229 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1230 DEVNAMES *pdn;
1231 DEVMODEA *pdm;
1232 char *name = NULL;
1233 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1235 /* load Collate ICONs */
1236 /* We load these with LoadImage because they are not a standard
1237 size and we don't want them rescaled */
1238 PrintStructures->hCollateIcon =
1239 LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1240 PrintStructures->hNoCollateIcon =
1241 LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1243 /* These can be done with LoadIcon */
1244 PrintStructures->hPortraitIcon =
1245 LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1246 PrintStructures->hLandscapeIcon =
1247 LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1249 /* display the collate/no_collate icon */
1250 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1251 (LPARAM)PrintStructures->hNoCollateIcon);
1253 if(PrintStructures->hCollateIcon == 0 ||
1254 PrintStructures->hNoCollateIcon == 0 ||
1255 PrintStructures->hPortraitIcon == 0 ||
1256 PrintStructures->hLandscapeIcon == 0) {
1257 ERR("no icon in resourcefile\n");
1258 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1259 EndDialog(hDlg, FALSE);
1263 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1264 * must be registered and the Help button must be shown.
1266 if (lppd->Flags & PD_SHOWHELP) {
1267 if((PrintStructures->HelpMessageID =
1268 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1269 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1270 return FALSE;
1272 } else
1273 PrintStructures->HelpMessageID = 0;
1275 if(!(lppd->Flags &PD_PRINTSETUP)) {
1276 PrintStructures->hwndUpDown =
1277 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1278 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1279 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1280 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1281 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1284 /* FIXME: I allow more freedom than either Win95 or WinNT,
1285 * which do not agree to what errors should be thrown or not
1286 * in case nToPage or nFromPage is out-of-range.
1288 if (lppd->nMaxPage < lppd->nMinPage)
1289 lppd->nMaxPage = lppd->nMinPage;
1290 if (lppd->nMinPage == lppd->nMaxPage)
1291 lppd->Flags |= PD_NOPAGENUMS;
1292 if (lppd->nToPage < lppd->nMinPage)
1293 lppd->nToPage = lppd->nMinPage;
1294 if (lppd->nToPage > lppd->nMaxPage)
1295 lppd->nToPage = lppd->nMaxPage;
1296 if (lppd->nFromPage < lppd->nMinPage)
1297 lppd->nFromPage = lppd->nMinPage;
1298 if (lppd->nFromPage > lppd->nMaxPage)
1299 lppd->nFromPage = lppd->nMaxPage;
1301 /* if we have the combo box, fill it */
1302 if (GetDlgItem(hDlg,comboID)) {
1303 /* Fill Combobox
1305 pdn = GlobalLock(lppd->hDevNames);
1306 pdm = GlobalLock(lppd->hDevMode);
1307 if(pdn)
1308 name = (char*)pdn + pdn->wDeviceOffset;
1309 else if(pdm)
1310 name = (char*)pdm->dmDeviceName;
1311 PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1312 if(pdm) GlobalUnlock(lppd->hDevMode);
1313 if(pdn) GlobalUnlock(lppd->hDevNames);
1315 /* Now find selected printer and update rest of dlg */
1316 name = HeapAlloc(GetProcessHeap(),0,256);
1317 if (GetDlgItemTextA(hDlg, comboID, name, 255))
1318 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1319 HeapFree(GetProcessHeap(),0,name);
1320 } else {
1321 /* else use default printer */
1322 char name[200];
1323 DWORD dwBufLen = sizeof(name);
1324 BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1326 if (ret)
1327 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1328 else
1329 FIXME("No default printer found, expect problems!\n");
1331 return TRUE;
1334 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg, WPARAM wParam,
1335 PRINT_PTRW* PrintStructures)
1337 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1338 DEVNAMES *pdn;
1339 DEVMODEW *pdm;
1340 WCHAR *name = NULL;
1341 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1343 /* load Collate ICONs */
1344 /* We load these with LoadImage because they are not a standard
1345 size and we don't want them rescaled */
1346 PrintStructures->hCollateIcon =
1347 LoadImageW(COMDLG32_hInstance, pd32_collateW, IMAGE_ICON, 0, 0, 0);
1348 PrintStructures->hNoCollateIcon =
1349 LoadImageW(COMDLG32_hInstance, pd32_nocollateW, IMAGE_ICON, 0, 0, 0);
1351 /* These can be done with LoadIcon */
1352 PrintStructures->hPortraitIcon =
1353 LoadIconW(COMDLG32_hInstance, pd32_portraitW);
1354 PrintStructures->hLandscapeIcon =
1355 LoadIconW(COMDLG32_hInstance, pd32_landscapeW);
1357 /* display the collate/no_collate icon */
1358 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1359 (LPARAM)PrintStructures->hNoCollateIcon);
1361 if(PrintStructures->hCollateIcon == 0 ||
1362 PrintStructures->hNoCollateIcon == 0 ||
1363 PrintStructures->hPortraitIcon == 0 ||
1364 PrintStructures->hLandscapeIcon == 0) {
1365 ERR("no icon in resourcefile\n");
1366 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1367 EndDialog(hDlg, FALSE);
1371 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1372 * must be registered and the Help button must be shown.
1374 if (lppd->Flags & PD_SHOWHELP) {
1375 if((PrintStructures->HelpMessageID =
1376 RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1377 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1378 return FALSE;
1380 } else
1381 PrintStructures->HelpMessageID = 0;
1383 if(!(lppd->Flags &PD_PRINTSETUP)) {
1384 PrintStructures->hwndUpDown =
1385 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1386 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1387 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1388 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1389 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1392 /* FIXME: I allow more freedom than either Win95 or WinNT,
1393 * which do not agree to what errors should be thrown or not
1394 * in case nToPage or nFromPage is out-of-range.
1396 if (lppd->nMaxPage < lppd->nMinPage)
1397 lppd->nMaxPage = lppd->nMinPage;
1398 if (lppd->nMinPage == lppd->nMaxPage)
1399 lppd->Flags |= PD_NOPAGENUMS;
1400 if (lppd->nToPage < lppd->nMinPage)
1401 lppd->nToPage = lppd->nMinPage;
1402 if (lppd->nToPage > lppd->nMaxPage)
1403 lppd->nToPage = lppd->nMaxPage;
1404 if (lppd->nFromPage < lppd->nMinPage)
1405 lppd->nFromPage = lppd->nMinPage;
1406 if (lppd->nFromPage > lppd->nMaxPage)
1407 lppd->nFromPage = lppd->nMaxPage;
1409 /* if we have the combo box, fill it */
1410 if (GetDlgItem(hDlg,comboID)) {
1411 /* Fill Combobox
1413 pdn = GlobalLock(lppd->hDevNames);
1414 pdm = GlobalLock(lppd->hDevMode);
1415 if(pdn)
1416 name = (WCHAR*)pdn + pdn->wDeviceOffset;
1417 else if(pdm)
1418 name = pdm->dmDeviceName;
1419 PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1420 if(pdm) GlobalUnlock(lppd->hDevMode);
1421 if(pdn) GlobalUnlock(lppd->hDevNames);
1423 /* Now find selected printer and update rest of dlg */
1424 /* ansi is ok here */
1425 name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1426 if (GetDlgItemTextW(hDlg, comboID, name, 255))
1427 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1428 HeapFree(GetProcessHeap(),0,name);
1429 } else {
1430 /* else use default printer */
1431 WCHAR name[200];
1432 DWORD dwBufLen = sizeof(name) / sizeof(WCHAR);
1433 BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1435 if (ret)
1436 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1437 else
1438 FIXME("No default printer found, expect problems!\n");
1440 return TRUE;
1443 /***********************************************************************
1444 * PRINTDLG_WMCommand [internal]
1446 LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1447 LPARAM lParam, PRINT_PTRA* PrintStructures)
1449 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1450 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1451 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1453 switch (LOWORD(wParam)) {
1454 case IDOK:
1455 TRACE(" OK button was hit\n");
1456 if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1457 FIXME("Update printdlg was not successful!\n");
1458 return(FALSE);
1460 EndDialog(hDlg, TRUE);
1461 return(TRUE);
1463 case IDCANCEL:
1464 TRACE(" CANCEL button was hit\n");
1465 EndDialog(hDlg, FALSE);
1466 return(FALSE);
1468 case pshHelp:
1469 TRACE(" HELP button was hit\n");
1470 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1471 (WPARAM) hDlg, (LPARAM) lppd);
1472 break;
1474 case chx2: /* collate pages checkbox */
1475 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1476 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1477 (LPARAM)PrintStructures->hCollateIcon);
1478 else
1479 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1480 (LPARAM)PrintStructures->hNoCollateIcon);
1481 break;
1482 case edt1: /* from page nr editbox */
1483 case edt2: /* to page nr editbox */
1484 if (HIWORD(wParam)==EN_CHANGE) {
1485 WORD nToPage;
1486 WORD nFromPage;
1487 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1488 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1489 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1490 CheckRadioButton(hDlg, rad1, rad3, rad3);
1492 break;
1494 case edt3:
1495 if(HIWORD(wParam) == EN_CHANGE) {
1496 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1497 if(copies <= 1)
1498 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1499 else
1500 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1502 break;
1504 #if 0
1505 case psh1: /* Print Setup */
1507 PRINTDLG16 pdlg;
1509 if (!PrintStructures->dlg.lpPrintDlg16) {
1510 FIXME("The 32bit print dialog does not have this button!?\n");
1511 break;
1514 memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1515 pdlg.Flags |= PD_PRINTSETUP;
1516 pdlg.hwndOwner = HWND_16(hDlg);
1517 if (!PrintDlg16(&pdlg))
1518 break;
1520 break;
1521 #endif
1522 case psh2: /* Properties button */
1524 HANDLE hPrinter;
1525 char PrinterName[256];
1527 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1528 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1529 FIXME(" Call to OpenPrinter did not succeed!\n");
1530 break;
1532 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1533 PrintStructures->lpDevMode,
1534 PrintStructures->lpDevMode,
1535 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1536 ClosePrinter(hPrinter);
1537 break;
1540 case rad1: /* Paperorientation */
1541 if (lppd->Flags & PD_PRINTSETUP)
1543 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1544 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1545 (LPARAM)(PrintStructures->hPortraitIcon));
1547 break;
1549 case rad2: /* Paperorientation */
1550 if (lppd->Flags & PD_PRINTSETUP)
1552 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1553 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1554 (LPARAM)(PrintStructures->hLandscapeIcon));
1556 break;
1558 case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1559 if (PrinterComboID != LOWORD(wParam)) {
1560 break;
1562 /* FALLTHROUGH */
1563 case cmb4: /* Printer combobox */
1564 if (HIWORD(wParam)==CBN_SELCHANGE) {
1565 char PrinterName[256];
1566 GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1567 PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1569 break;
1571 case cmb2: /* Papersize */
1573 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1574 if(Sel != CB_ERR)
1575 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1576 CB_GETITEMDATA,
1577 Sel, 0);
1579 break;
1581 case cmb3: /* Bin */
1583 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1584 if(Sel != CB_ERR)
1585 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1586 CB_GETITEMDATA, Sel,
1589 break;
1591 if(lppd->Flags & PD_PRINTSETUP) {
1592 switch (LOWORD(wParam)) {
1593 case rad1: /* orientation */
1594 case rad2:
1595 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1596 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1597 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1598 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1599 (WPARAM)IMAGE_ICON,
1600 (LPARAM)PrintStructures->hPortraitIcon);
1601 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1602 (WPARAM)IMAGE_ICON,
1603 (LPARAM)PrintStructures->hPortraitIcon);
1605 } else {
1606 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1607 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1608 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1609 (WPARAM)IMAGE_ICON,
1610 (LPARAM)PrintStructures->hLandscapeIcon);
1611 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1612 (WPARAM)IMAGE_ICON,
1613 (LPARAM)PrintStructures->hLandscapeIcon);
1616 break;
1619 return FALSE;
1622 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1623 LPARAM lParam, PRINT_PTRW* PrintStructures)
1625 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1626 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1627 LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1629 switch (LOWORD(wParam)) {
1630 case IDOK:
1631 TRACE(" OK button was hit\n");
1632 if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1633 FIXME("Update printdlg was not successful!\n");
1634 return(FALSE);
1636 EndDialog(hDlg, TRUE);
1637 return(TRUE);
1639 case IDCANCEL:
1640 TRACE(" CANCEL button was hit\n");
1641 EndDialog(hDlg, FALSE);
1642 return(FALSE);
1644 case pshHelp:
1645 TRACE(" HELP button was hit\n");
1646 SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1647 (WPARAM) hDlg, (LPARAM) lppd);
1648 break;
1650 case chx2: /* collate pages checkbox */
1651 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1652 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1653 (LPARAM)PrintStructures->hCollateIcon);
1654 else
1655 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1656 (LPARAM)PrintStructures->hNoCollateIcon);
1657 break;
1658 case edt1: /* from page nr editbox */
1659 case edt2: /* to page nr editbox */
1660 if (HIWORD(wParam)==EN_CHANGE) {
1661 WORD nToPage;
1662 WORD nFromPage;
1663 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1664 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1665 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1666 CheckRadioButton(hDlg, rad1, rad3, rad3);
1668 break;
1670 case edt3:
1671 if(HIWORD(wParam) == EN_CHANGE) {
1672 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1673 if(copies <= 1)
1674 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1675 else
1676 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1678 break;
1680 case psh1: /* Print Setup */
1682 ERR("psh1 is called from 16bit code only, we should not get here.\n");
1684 break;
1685 case psh2: /* Properties button */
1687 HANDLE hPrinter;
1688 WCHAR PrinterName[256];
1690 if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1691 if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1692 FIXME(" Call to OpenPrinter did not succeed!\n");
1693 break;
1695 DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1696 PrintStructures->lpDevMode,
1697 PrintStructures->lpDevMode,
1698 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1699 ClosePrinter(hPrinter);
1700 break;
1703 case rad1: /* Paperorientation */
1704 if (lppd->Flags & PD_PRINTSETUP)
1706 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1707 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1708 (LPARAM)(PrintStructures->hPortraitIcon));
1710 break;
1712 case rad2: /* Paperorientation */
1713 if (lppd->Flags & PD_PRINTSETUP)
1715 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1716 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1717 (LPARAM)(PrintStructures->hLandscapeIcon));
1719 break;
1721 case cmb1: /* Printer Combobox in PRINT SETUP */
1722 /* FALLTHROUGH */
1723 case cmb4: /* Printer combobox */
1724 if (HIWORD(wParam)==CBN_SELCHANGE) {
1725 WCHAR PrinterName[256];
1726 GetDlgItemTextW(hDlg, LOWORD(wParam), PrinterName, 255);
1727 PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1729 break;
1731 case cmb2: /* Papersize */
1733 DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1734 if(Sel != CB_ERR)
1735 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1736 CB_GETITEMDATA,
1737 Sel, 0);
1739 break;
1741 case cmb3: /* Bin */
1743 DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1744 if(Sel != CB_ERR)
1745 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1746 CB_GETITEMDATA, Sel,
1749 break;
1751 if(lppd->Flags & PD_PRINTSETUP) {
1752 switch (LOWORD(wParam)) {
1753 case rad1: /* orientation */
1754 case rad2:
1755 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1756 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1757 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1758 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1759 (WPARAM)IMAGE_ICON,
1760 (LPARAM)PrintStructures->hPortraitIcon);
1761 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1762 (WPARAM)IMAGE_ICON,
1763 (LPARAM)PrintStructures->hPortraitIcon);
1765 } else {
1766 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1767 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1768 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1769 (WPARAM)IMAGE_ICON,
1770 (LPARAM)PrintStructures->hLandscapeIcon);
1771 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1772 (WPARAM)IMAGE_ICON,
1773 (LPARAM)PrintStructures->hLandscapeIcon);
1776 break;
1779 return FALSE;
1782 /***********************************************************************
1783 * PrintDlgProcA [internal]
1785 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1786 LPARAM lParam)
1788 PRINT_PTRA* PrintStructures;
1789 INT_PTR res = FALSE;
1791 if (uMsg!=WM_INITDIALOG) {
1792 PrintStructures = GetPropW(hDlg, printdlg_prop);
1793 if (!PrintStructures)
1794 return FALSE;
1795 } else {
1796 PrintStructures = (PRINT_PTRA*) lParam;
1797 SetPropW(hDlg, printdlg_prop, PrintStructures);
1798 if(!check_printer_setup(hDlg))
1800 EndDialog(hDlg,FALSE);
1801 return FALSE;
1803 res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1805 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1806 res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1807 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1809 return res;
1812 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1813 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1814 lParam);
1815 if(res) return res;
1818 switch (uMsg) {
1819 case WM_COMMAND:
1820 return PRINTDLG_WMCommandA(hDlg, wParam, lParam, PrintStructures);
1822 case WM_DESTROY:
1823 DestroyIcon(PrintStructures->hCollateIcon);
1824 DestroyIcon(PrintStructures->hNoCollateIcon);
1825 DestroyIcon(PrintStructures->hPortraitIcon);
1826 DestroyIcon(PrintStructures->hLandscapeIcon);
1827 if(PrintStructures->hwndUpDown)
1828 DestroyWindow(PrintStructures->hwndUpDown);
1829 return FALSE;
1831 return res;
1834 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
1835 LPARAM lParam)
1837 PRINT_PTRW* PrintStructures;
1838 INT_PTR res = FALSE;
1840 if (uMsg!=WM_INITDIALOG) {
1841 PrintStructures = GetPropW(hDlg, printdlg_prop);
1842 if (!PrintStructures)
1843 return FALSE;
1844 } else {
1845 PrintStructures = (PRINT_PTRW*) lParam;
1846 SetPropW(hDlg, printdlg_prop, PrintStructures);
1847 if(!check_printer_setup(hDlg))
1849 EndDialog(hDlg,FALSE);
1850 return FALSE;
1852 res = PRINTDLG_WMInitDialogW(hDlg, wParam, PrintStructures);
1854 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1855 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
1856 return res;
1859 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1860 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
1861 if(res) return res;
1864 switch (uMsg) {
1865 case WM_COMMAND:
1866 return PRINTDLG_WMCommandW(hDlg, wParam, lParam, PrintStructures);
1868 case WM_DESTROY:
1869 DestroyIcon(PrintStructures->hCollateIcon);
1870 DestroyIcon(PrintStructures->hNoCollateIcon);
1871 DestroyIcon(PrintStructures->hPortraitIcon);
1872 DestroyIcon(PrintStructures->hLandscapeIcon);
1873 if(PrintStructures->hwndUpDown)
1874 DestroyWindow(PrintStructures->hwndUpDown);
1875 return FALSE;
1877 return res;
1880 /************************************************************
1882 * PRINTDLG_GetDlgTemplate
1885 static HGLOBAL PRINTDLG_GetDlgTemplateA(const PRINTDLGA *lppd)
1887 HRSRC hResInfo;
1888 HGLOBAL hDlgTmpl;
1890 if (lppd->Flags & PD_PRINTSETUP) {
1891 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1892 hDlgTmpl = lppd->hSetupTemplate;
1893 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1894 hResInfo = FindResourceA(lppd->hInstance,
1895 lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
1896 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1897 } else {
1898 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1899 (LPSTR)RT_DIALOG);
1900 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1902 } else {
1903 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1904 hDlgTmpl = lppd->hPrintTemplate;
1905 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1906 hResInfo = FindResourceA(lppd->hInstance,
1907 lppd->lpPrintTemplateName,
1908 (LPSTR)RT_DIALOG);
1909 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1910 } else {
1911 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1912 (LPSTR)RT_DIALOG);
1913 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1916 return hDlgTmpl;
1919 static HGLOBAL PRINTDLG_GetDlgTemplateW(const PRINTDLGW *lppd)
1921 HRSRC hResInfo;
1922 HGLOBAL hDlgTmpl;
1923 static const WCHAR xpsetup[] = { 'P','R','I','N','T','3','2','_','S','E','T','U','P',0};
1924 static const WCHAR xprint[] = { 'P','R','I','N','T','3','2',0};
1926 if (lppd->Flags & PD_PRINTSETUP) {
1927 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1928 hDlgTmpl = lppd->hSetupTemplate;
1929 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1930 hResInfo = FindResourceW(lppd->hInstance,
1931 lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
1932 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1933 } else {
1934 hResInfo = FindResourceW(COMDLG32_hInstance, xpsetup, (LPWSTR)RT_DIALOG);
1935 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1937 } else {
1938 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1939 hDlgTmpl = lppd->hPrintTemplate;
1940 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1941 hResInfo = FindResourceW(lppd->hInstance,
1942 lppd->lpPrintTemplateName,
1943 (LPWSTR)RT_DIALOG);
1944 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1945 } else {
1946 hResInfo = FindResourceW(COMDLG32_hInstance, xprint, (LPWSTR)RT_DIALOG);
1947 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1950 return hDlgTmpl;
1953 /***********************************************************************
1955 * PRINTDLG_CreateDC
1958 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
1960 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1961 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1963 if(lppd->Flags & PD_RETURNDC) {
1964 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1965 (char*)pdn + pdn->wDeviceOffset,
1966 (char*)pdn + pdn->wOutputOffset,
1967 pdm );
1968 } else if(lppd->Flags & PD_RETURNIC) {
1969 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1970 (char*)pdn + pdn->wDeviceOffset,
1971 (char*)pdn + pdn->wOutputOffset,
1972 pdm );
1974 GlobalUnlock(lppd->hDevNames);
1975 GlobalUnlock(lppd->hDevMode);
1976 return lppd->hDC ? TRUE : FALSE;
1979 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
1981 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1982 DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
1984 if(lppd->Flags & PD_RETURNDC) {
1985 lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
1986 (WCHAR*)pdn + pdn->wDeviceOffset,
1987 (WCHAR*)pdn + pdn->wOutputOffset,
1988 pdm );
1989 } else if(lppd->Flags & PD_RETURNIC) {
1990 lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
1991 (WCHAR*)pdn + pdn->wDeviceOffset,
1992 (WCHAR*)pdn + pdn->wOutputOffset,
1993 pdm );
1995 GlobalUnlock(lppd->hDevNames);
1996 GlobalUnlock(lppd->hDevMode);
1997 return lppd->hDC ? TRUE : FALSE;
2000 /***********************************************************************
2001 * PrintDlgA (COMDLG32.@)
2003 * Displays the PRINT dialog box, which enables the user to specify
2004 * specific properties of the print job.
2006 * PARAMS
2007 * lppd [IO] ptr to PRINTDLG32 struct
2009 * RETURNS
2010 * nonzero if the user pressed the OK button
2011 * zero if the user cancelled the window or an error occurred
2013 * BUGS
2014 * PrintDlg:
2015 * * The Collate Icons do not display, even though they are in the code.
2016 * * The Properties Button(s) should call DocumentPropertiesA().
2019 BOOL WINAPI PrintDlgA(LPPRINTDLGA lppd)
2021 BOOL bRet = FALSE;
2022 LPVOID ptr;
2023 HINSTANCE hInst;
2025 if (!lppd)
2027 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2028 return FALSE;
2031 hInst = (HINSTANCE)GetWindowLongPtrA( lppd->hwndOwner, GWLP_HINSTANCE );
2032 if(TRACE_ON(commdlg)) {
2033 char flagstr[1000] = "";
2034 const struct pd_flags *pflag = pd_flags;
2035 for( ; pflag->name; pflag++) {
2036 if(lppd->Flags & pflag->flag)
2037 strcat(flagstr, pflag->name);
2039 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2040 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2041 "flags %08x (%s)\n",
2042 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2043 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2044 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2047 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2048 WARN("structure size failure !!!\n");
2049 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2050 return FALSE;
2053 if(lppd->Flags & PD_RETURNDEFAULT) {
2054 PRINTER_INFO_2A *pbuf;
2055 DRIVER_INFO_3A *dbuf;
2056 HANDLE hprn;
2057 DWORD needed;
2059 if(lppd->hDevMode || lppd->hDevNames) {
2060 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2061 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2062 return FALSE;
2064 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2065 WARN("Can't find default printer\n");
2066 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2067 return FALSE;
2070 GetPrinterA(hprn, 2, NULL, 0, &needed);
2071 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2072 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2074 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2075 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2076 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2077 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2078 GetLastError(),pbuf->pPrinterName);
2079 HeapFree(GetProcessHeap(), 0, dbuf);
2080 HeapFree(GetProcessHeap(), 0, pbuf);
2081 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2082 return FALSE;
2084 ClosePrinter(hprn);
2086 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2087 dbuf->pDriverPath,
2088 pbuf->pPrinterName,
2089 pbuf->pPortName);
2090 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2091 pbuf->pDevMode->dmDriverExtra);
2092 ptr = GlobalLock(lppd->hDevMode);
2093 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2094 pbuf->pDevMode->dmDriverExtra);
2095 GlobalUnlock(lppd->hDevMode);
2096 HeapFree(GetProcessHeap(), 0, pbuf);
2097 HeapFree(GetProcessHeap(), 0, dbuf);
2098 bRet = TRUE;
2099 } else {
2100 HGLOBAL hDlgTmpl;
2101 PRINT_PTRA *PrintStructures;
2103 /* load Dialog resources,
2104 * depending on Flags indicates Print32 or Print32_setup dialog
2106 hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2107 if (!hDlgTmpl) {
2108 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2109 return FALSE;
2111 ptr = LockResource( hDlgTmpl );
2112 if (!ptr) {
2113 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2114 return FALSE;
2117 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2118 sizeof(PRINT_PTRA));
2119 PrintStructures->lpPrintDlg = lppd;
2121 /* and create & process the dialog .
2122 * -1 is failure, 0 is broken hwnd, everything else is ok.
2124 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2125 PrintDlgProcA,
2126 (LPARAM)PrintStructures));
2128 if(bRet) {
2129 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2130 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2131 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2133 if (lppd->hDevMode == 0) {
2134 TRACE(" No hDevMode yet... Need to create my own\n");
2135 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2136 lpdm->dmSize + lpdm->dmDriverExtra);
2137 } else {
2138 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2139 lpdm->dmSize + lpdm->dmDriverExtra,
2140 GMEM_MOVEABLE);
2142 lpdmReturn = GlobalLock(lppd->hDevMode);
2143 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2145 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2146 di->pDriverPath,
2147 pi->pPrinterName,
2148 pi->pPortName
2150 GlobalUnlock(lppd->hDevMode);
2152 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2153 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2154 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2155 HeapFree(GetProcessHeap(), 0, PrintStructures);
2157 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2158 bRet = PRINTDLG_CreateDCA(lppd);
2160 TRACE("exit! (%d)\n", bRet);
2161 return bRet;
2164 /***********************************************************************
2165 * PrintDlgW (COMDLG32.@)
2167 * See PrintDlgA.
2169 BOOL WINAPI PrintDlgW(LPPRINTDLGW lppd)
2171 BOOL bRet = FALSE;
2172 LPVOID ptr;
2173 HINSTANCE hInst;
2175 if (!lppd)
2177 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2178 return FALSE;
2181 hInst = (HINSTANCE)GetWindowLongPtrW( lppd->hwndOwner, GWLP_HINSTANCE );
2182 if(TRACE_ON(commdlg)) {
2183 char flagstr[1000] = "";
2184 const struct pd_flags *pflag = pd_flags;
2185 for( ; pflag->name; pflag++) {
2186 if(lppd->Flags & pflag->flag)
2187 strcat(flagstr, pflag->name);
2189 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2190 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2191 "flags %08x (%s)\n",
2192 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2193 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2194 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2197 if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2198 WARN("structure size failure !!!\n");
2199 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2200 return FALSE;
2203 if(lppd->Flags & PD_RETURNDEFAULT) {
2204 PRINTER_INFO_2W *pbuf;
2205 DRIVER_INFO_3W *dbuf;
2206 HANDLE hprn;
2207 DWORD needed;
2209 if(lppd->hDevMode || lppd->hDevNames) {
2210 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2211 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2212 return FALSE;
2214 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2215 WARN("Can't find default printer\n");
2216 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2217 return FALSE;
2220 GetPrinterW(hprn, 2, NULL, 0, &needed);
2221 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2222 GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2224 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2225 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2226 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2227 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2228 GetLastError(),debugstr_w(pbuf->pPrinterName));
2229 HeapFree(GetProcessHeap(), 0, dbuf);
2230 HeapFree(GetProcessHeap(), 0, pbuf);
2231 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2232 return FALSE;
2234 ClosePrinter(hprn);
2236 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2237 dbuf->pDriverPath,
2238 pbuf->pPrinterName,
2239 pbuf->pPortName);
2240 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2241 pbuf->pDevMode->dmDriverExtra);
2242 ptr = GlobalLock(lppd->hDevMode);
2243 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2244 pbuf->pDevMode->dmDriverExtra);
2245 GlobalUnlock(lppd->hDevMode);
2246 HeapFree(GetProcessHeap(), 0, pbuf);
2247 HeapFree(GetProcessHeap(), 0, dbuf);
2248 bRet = TRUE;
2249 } else {
2250 HGLOBAL hDlgTmpl;
2251 PRINT_PTRW *PrintStructures;
2253 /* load Dialog resources,
2254 * depending on Flags indicates Print32 or Print32_setup dialog
2256 hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2257 if (!hDlgTmpl) {
2258 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2259 return FALSE;
2261 ptr = LockResource( hDlgTmpl );
2262 if (!ptr) {
2263 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2264 return FALSE;
2267 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2268 sizeof(PRINT_PTRW));
2269 PrintStructures->lpPrintDlg = lppd;
2271 /* and create & process the dialog .
2272 * -1 is failure, 0 is broken hwnd, everything else is ok.
2274 bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2275 PrintDlgProcW,
2276 (LPARAM)PrintStructures));
2278 if(bRet) {
2279 DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2280 PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2281 DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2283 if (lppd->hDevMode == 0) {
2284 TRACE(" No hDevMode yet... Need to create my own\n");
2285 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2286 lpdm->dmSize + lpdm->dmDriverExtra);
2287 } else {
2288 WORD locks;
2289 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2290 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2291 while(locks--) {
2292 GlobalUnlock(lppd->hDevMode);
2293 TRACE("Now got %d locks\n", locks);
2296 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2297 lpdm->dmSize + lpdm->dmDriverExtra,
2298 GMEM_MOVEABLE);
2300 lpdmReturn = GlobalLock(lppd->hDevMode);
2301 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2303 if (lppd->hDevNames != 0) {
2304 WORD locks;
2305 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2306 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2307 while(locks--)
2308 GlobalUnlock(lppd->hDevNames);
2311 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2312 di->pDriverPath,
2313 pi->pPrinterName,
2314 pi->pPortName
2316 GlobalUnlock(lppd->hDevMode);
2318 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2319 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2320 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2321 HeapFree(GetProcessHeap(), 0, PrintStructures);
2323 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2324 bRet = PRINTDLG_CreateDCW(lppd);
2326 TRACE("exit! (%d)\n", bRet);
2327 return bRet;
2330 /***********************************************************************
2332 * PageSetupDlg
2333 * rad1 - portrait
2334 * rad2 - landscape
2335 * cmb1 - printer select (not in standard dialog template)
2336 * cmb2 - paper size
2337 * cmb3 - source (tray?)
2338 * edt4 - border left
2339 * edt5 - border top
2340 * edt6 - border right
2341 * edt7 - border bottom
2342 * psh3 - "Printer..."
2345 typedef struct
2347 BOOL unicode;
2348 union
2350 LPPAGESETUPDLGA dlga;
2351 LPPAGESETUPDLGW dlgw;
2352 } u;
2353 HWND hDlg; /* Page Setup dialog handle */
2354 RECT rtDrawRect; /* Drawing rect for page */
2355 } pagesetup_data;
2357 static inline DWORD pagesetup_get_flags(const pagesetup_data *data)
2359 return data->u.dlgw->Flags;
2362 static inline BOOL is_metric(const pagesetup_data *data)
2364 return pagesetup_get_flags(data) & PSD_INHUNDREDTHSOFMILLIMETERS;
2367 static inline LONG tenths_mm_to_size(pagesetup_data *data, LONG size)
2369 if (is_metric(data))
2370 return 10 * size;
2371 else
2372 return 10 * size * 100 / 254;
2375 static inline LONG thousandths_inch_to_size(pagesetup_data *data, LONG size)
2377 if (is_metric(data))
2378 return size * 254 / 100;
2379 else
2380 return size;
2383 static WCHAR get_decimal_sep(void)
2385 static WCHAR sep;
2387 if(!sep)
2389 WCHAR buf[2] = {'.',0};
2390 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, sizeof(buf) / sizeof(buf[0]));
2391 sep = buf[0];
2393 return sep;
2396 static void size2str(const pagesetup_data *data, DWORD size, LPWSTR strout)
2398 WCHAR integer_fmt[] = {'%','d',0};
2399 WCHAR hundredths_fmt[] = {'%','d','%','c','%','0','2','d',0};
2400 WCHAR thousandths_fmt[] = {'%','d','%','c','%','0','3','d',0};
2402 /* FIXME use LOCALE_SDECIMAL when the edit parsing code can cope */
2404 if (is_metric(data))
2406 if(size % 100)
2407 wsprintfW(strout, hundredths_fmt, size / 100, get_decimal_sep(), size % 100);
2408 else
2409 wsprintfW(strout, integer_fmt, size / 100);
2411 else
2413 if(size % 1000)
2414 wsprintfW(strout, thousandths_fmt, size / 1000, get_decimal_sep(), size % 1000);
2415 else
2416 wsprintfW(strout, integer_fmt, size / 1000);
2421 static inline BOOL is_default_metric(void)
2423 DWORD system;
2424 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
2425 (LPWSTR)&system, sizeof(system));
2426 return system == 0;
2429 /**********************************************
2430 * rotate_rect
2431 * Cyclically permute the four members of rc
2432 * If sense is TRUE l -> t -> r -> b
2433 * otherwise l <- t <- r <- b
2435 static inline void rotate_rect(RECT *rc, BOOL sense)
2437 INT tmp;
2438 if(sense)
2440 tmp = rc->bottom;
2441 rc->bottom = rc->right;
2442 rc->right = rc->top;
2443 rc->top = rc->left;
2444 rc->left = tmp;
2446 else
2448 tmp = rc->left;
2449 rc->left = rc->top;
2450 rc->top = rc->right;
2451 rc->right = rc->bottom;
2452 rc->bottom = tmp;
2456 static void pagesetup_set_orientation(pagesetup_data *data, WORD orient)
2458 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2460 assert(orient == DMORIENT_PORTRAIT || orient == DMORIENT_LANDSCAPE);
2462 if(data->unicode)
2463 dm->u1.s1.dmOrientation = orient;
2464 else
2466 DEVMODEA *dmA = (DEVMODEA *)dm;
2467 dmA->u1.s1.dmOrientation = orient;
2469 GlobalUnlock(data->u.dlgw->hDevMode);
2472 static WORD pagesetup_get_orientation(const pagesetup_data *data)
2474 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2475 WORD orient;
2477 if(data->unicode)
2478 orient = dm->u1.s1.dmOrientation;
2479 else
2481 DEVMODEA *dmA = (DEVMODEA *)dm;
2482 orient = dmA->u1.s1.dmOrientation;
2484 GlobalUnlock(data->u.dlgw->hDevMode);
2485 return orient;
2488 static void pagesetup_set_papersize(pagesetup_data *data, WORD paper)
2490 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2492 if(data->unicode)
2493 dm->u1.s1.dmPaperSize = paper;
2494 else
2496 DEVMODEA *dmA = (DEVMODEA *)dm;
2497 dmA->u1.s1.dmPaperSize = paper;
2499 GlobalUnlock(data->u.dlgw->hDevMode);
2502 static WORD pagesetup_get_papersize(const pagesetup_data *data)
2504 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2505 WORD paper;
2507 if(data->unicode)
2508 paper = dm->u1.s1.dmPaperSize;
2509 else
2511 DEVMODEA *dmA = (DEVMODEA *)dm;
2512 paper = dmA->u1.s1.dmPaperSize;
2514 GlobalUnlock(data->u.dlgw->hDevMode);
2515 return paper;
2518 static void pagesetup_set_defaultsource(pagesetup_data *data, WORD source)
2520 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2522 if(data->unicode)
2523 dm->u1.s1.dmDefaultSource = source;
2524 else
2526 DEVMODEA *dmA = (DEVMODEA *)dm;
2527 dmA->u1.s1.dmDefaultSource = source;
2529 GlobalUnlock(data->u.dlgw->hDevMode);
2532 typedef enum
2534 devnames_driver_name,
2535 devnames_device_name,
2536 devnames_output_name
2537 } devnames_name;
2540 static inline WORD get_devname_offset(DEVNAMES *dn, devnames_name which)
2542 switch(which)
2544 case devnames_driver_name: return dn->wDriverOffset;
2545 case devnames_device_name: return dn->wDeviceOffset;
2546 case devnames_output_name: return dn->wOutputOffset;
2548 ERR("Souldn't be here\n");
2549 return 0;
2552 static WCHAR *pagesetup_get_a_devname(const pagesetup_data *data, devnames_name which)
2554 DEVNAMES *dn;
2555 WCHAR *name;
2557 dn = GlobalLock(data->u.dlgw->hDevNames);
2558 if(data->unicode)
2559 name = strdupW((WCHAR *)dn + get_devname_offset(dn, which));
2560 else
2562 int len = MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, NULL, 0);
2563 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2564 MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, name, len);
2566 GlobalUnlock(data->u.dlgw->hDevNames);
2567 return name;
2570 static WCHAR *pagesetup_get_drvname(const pagesetup_data *data)
2572 return pagesetup_get_a_devname(data, devnames_driver_name);
2575 static WCHAR *pagesetup_get_devname(const pagesetup_data *data)
2577 return pagesetup_get_a_devname(data, devnames_device_name);
2580 static WCHAR *pagesetup_get_portname(const pagesetup_data *data)
2582 return pagesetup_get_a_devname(data, devnames_output_name);
2585 static void pagesetup_release_a_devname(const pagesetup_data *data, WCHAR *name)
2587 HeapFree(GetProcessHeap(), 0, name);
2590 static void pagesetup_set_devnames(pagesetup_data *data, LPCWSTR drv, LPCWSTR devname, LPCWSTR port)
2592 DEVNAMES *dn;
2593 WCHAR def[256];
2594 DWORD len = sizeof(DEVNAMES), drv_len, dev_len, port_len;
2596 if(data->unicode)
2598 drv_len = (strlenW(drv) + 1) * sizeof(WCHAR);
2599 dev_len = (strlenW(devname) + 1) * sizeof(WCHAR);
2600 port_len = (strlenW(port) + 1) * sizeof(WCHAR);
2602 else
2604 drv_len = WideCharToMultiByte(CP_ACP, 0, drv, -1, NULL, 0, NULL, NULL);
2605 dev_len = WideCharToMultiByte(CP_ACP, 0, devname, -1, NULL, 0, NULL, NULL);
2606 port_len = WideCharToMultiByte(CP_ACP, 0, port, -1, NULL, 0, NULL, NULL);
2608 len += drv_len + dev_len + port_len;
2610 if(data->u.dlgw->hDevNames)
2611 data->u.dlgw->hDevNames = GlobalReAlloc(data->u.dlgw->hDevNames, len, GMEM_MOVEABLE);
2612 else
2613 data->u.dlgw->hDevNames = GlobalAlloc(GMEM_MOVEABLE, len);
2615 dn = GlobalLock(data->u.dlgw->hDevNames);
2617 if(data->unicode)
2619 WCHAR *ptr = (WCHAR *)(dn + 1);
2620 len = sizeof(DEVNAMES) / sizeof(WCHAR);
2621 dn->wDriverOffset = len;
2622 strcpyW(ptr, drv);
2623 ptr += drv_len / sizeof(WCHAR);
2624 len += drv_len / sizeof(WCHAR);
2625 dn->wDeviceOffset = len;
2626 strcpyW(ptr, devname);
2627 ptr += dev_len / sizeof(WCHAR);
2628 len += dev_len / sizeof(WCHAR);
2629 dn->wOutputOffset = len;
2630 strcpyW(ptr, port);
2632 else
2634 char *ptr = (char *)(dn + 1);
2635 len = sizeof(DEVNAMES);
2636 dn->wDriverOffset = len;
2637 WideCharToMultiByte(CP_ACP, 0, drv, -1, ptr, drv_len, NULL, NULL);
2638 ptr += drv_len;
2639 len += drv_len;
2640 dn->wDeviceOffset = len;
2641 WideCharToMultiByte(CP_ACP, 0, devname, -1, ptr, dev_len, NULL, NULL);
2642 ptr += dev_len;
2643 len += dev_len;
2644 dn->wOutputOffset = len;
2645 WideCharToMultiByte(CP_ACP, 0, port, -1, ptr, port_len, NULL, NULL);
2648 dn->wDefault = 0;
2649 len = sizeof(def) / sizeof(def[0]);
2650 GetDefaultPrinterW(def, &len);
2651 if(!lstrcmpW(def, devname))
2652 dn->wDefault = 1;
2654 GlobalUnlock(data->u.dlgw->hDevNames);
2657 static DEVMODEW *pagesetup_get_devmode(const pagesetup_data *data)
2659 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2660 DEVMODEW *ret;
2662 if(data->unicode)
2664 /* We make a copy even in the unicode case because the ptr
2665 may get passed back to us in pagesetup_set_devmode. */
2666 ret = HeapAlloc(GetProcessHeap(), 0, dm->dmSize + dm->dmDriverExtra);
2667 memcpy(ret, dm, dm->dmSize + dm->dmDriverExtra);
2669 else
2670 ret = GdiConvertToDevmodeW((DEVMODEA *)dm);
2672 GlobalUnlock(data->u.dlgw->hDevMode);
2673 return ret;
2676 static void pagesetup_release_devmode(const pagesetup_data *data, DEVMODEW *dm)
2678 HeapFree(GetProcessHeap(), 0, dm);
2681 static void pagesetup_set_devmode(pagesetup_data *data, DEVMODEW *dm)
2683 DEVMODEA *dmA = NULL;
2684 void *src, *dst;
2685 DWORD size;
2687 if(data->unicode)
2689 size = dm->dmSize + dm->dmDriverExtra;
2690 src = dm;
2692 else
2694 dmA = convert_to_devmodeA(dm);
2695 size = dmA->dmSize + dmA->dmDriverExtra;
2696 src = dmA;
2699 if(data->u.dlgw->hDevMode)
2700 data->u.dlgw->hDevMode = GlobalReAlloc(data->u.dlgw->hDevMode, size,
2701 GMEM_MOVEABLE);
2702 else
2703 data->u.dlgw->hDevMode = GlobalAlloc(GMEM_MOVEABLE, size);
2705 dst = GlobalLock(data->u.dlgw->hDevMode);
2706 memcpy(dst, src, size);
2707 GlobalUnlock(data->u.dlgw->hDevMode);
2708 HeapFree(GetProcessHeap(), 0, dmA);
2711 static inline POINT *pagesetup_get_papersize_pt(const pagesetup_data *data)
2713 return &data->u.dlgw->ptPaperSize;
2716 static inline RECT *pagesetup_get_margin_rect(const pagesetup_data *data)
2718 return &data->u.dlgw->rtMargin;
2721 typedef enum
2723 page_setup_hook,
2724 page_paint_hook
2725 } hook_type;
2727 static inline LPPAGESETUPHOOK pagesetup_get_hook(const pagesetup_data *data, hook_type which)
2729 switch(which)
2731 case page_setup_hook: return data->u.dlgw->lpfnPageSetupHook;
2732 case page_paint_hook: return data->u.dlgw->lpfnPagePaintHook;
2734 return NULL;
2737 /* This should only be used in calls to hook procs so we return the ptr
2738 already cast to LPARAM */
2739 static inline LPARAM pagesetup_get_dlg_struct(const pagesetup_data *data)
2741 return (LPARAM)data->u.dlgw;
2744 static inline void swap_point(POINT *pt)
2746 LONG tmp = pt->x;
2747 pt->x = pt->y;
2748 pt->y = tmp;
2751 static BOOL pagesetup_update_papersize(pagesetup_data *data)
2753 DEVMODEW *dm;
2754 LPWSTR devname, portname;
2755 int i, num;
2756 WORD *words = NULL, paperword;
2757 POINT *points = NULL;
2758 BOOL retval = FALSE;
2760 dm = pagesetup_get_devmode(data);
2761 devname = pagesetup_get_devname(data);
2762 portname = pagesetup_get_portname(data);
2764 num = DeviceCapabilitiesW(devname, portname, DC_PAPERS, NULL, dm);
2765 if (num <= 0)
2767 FIXME("No papernames found for %s/%s\n", debugstr_w(devname), debugstr_w(portname));
2768 goto end;
2771 words = HeapAlloc(GetProcessHeap(), 0, num * sizeof(WORD));
2772 points = HeapAlloc(GetProcessHeap(), 0, num * sizeof(POINT));
2774 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERS, (LPWSTR)words, dm))
2776 FIXME("Number of returned words is not %d\n", num);
2777 goto end;
2780 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERSIZE, (LPWSTR)points, dm))
2782 FIXME("Number of returned sizes is not %d\n", num);
2783 goto end;
2786 paperword = pagesetup_get_papersize(data);
2788 for (i = 0; i < num; i++)
2789 if (words[i] == paperword)
2790 break;
2792 if (i == num)
2794 FIXME("Papersize %d not found in list?\n", paperword);
2795 goto end;
2798 /* this is _10ths_ of a millimeter */
2799 pagesetup_get_papersize_pt(data)->x = tenths_mm_to_size(data, points[i].x);
2800 pagesetup_get_papersize_pt(data)->y = tenths_mm_to_size(data, points[i].y);
2802 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2803 swap_point(pagesetup_get_papersize_pt(data));
2805 retval = TRUE;
2807 end:
2808 HeapFree(GetProcessHeap(), 0, words);
2809 HeapFree(GetProcessHeap(), 0, points);
2810 pagesetup_release_a_devname(data, portname);
2811 pagesetup_release_a_devname(data, devname);
2812 pagesetup_release_devmode(data, dm);
2814 return retval;
2817 /**********************************************************************************************
2818 * pagesetup_change_printer
2820 * Redefines hDevMode and hDevNames HANDLES and initialises it.
2823 static BOOL pagesetup_change_printer(LPWSTR name, pagesetup_data *data)
2825 HANDLE hprn;
2826 DWORD needed;
2827 PRINTER_INFO_2W *prn_info = NULL;
2828 DRIVER_INFO_3W *drv_info = NULL;
2829 DEVMODEW *dm = NULL;
2830 BOOL retval = FALSE;
2832 if(!OpenPrinterW(name, &hprn, NULL))
2834 ERR("Can't open printer %s\n", debugstr_w(name));
2835 goto end;
2838 GetPrinterW(hprn, 2, NULL, 0, &needed);
2839 prn_info = HeapAlloc(GetProcessHeap(), 0, needed);
2840 GetPrinterW(hprn, 2, (LPBYTE)prn_info, needed, &needed);
2841 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2842 drv_info = HeapAlloc(GetProcessHeap(), 0, needed);
2843 if(!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)drv_info, needed, &needed))
2845 ERR("GetPrinterDriverA failed for %s, fix your config!\n", debugstr_w(prn_info->pPrinterName));
2846 goto end;
2848 ClosePrinter(hprn);
2850 needed = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
2851 if(needed == -1)
2853 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
2854 goto end;
2857 dm = HeapAlloc(GetProcessHeap(), 0, needed);
2858 DocumentPropertiesW(0, 0, name, dm, NULL, DM_OUT_BUFFER);
2860 pagesetup_set_devmode(data, dm);
2861 pagesetup_set_devnames(data, drv_info->pDriverPath, prn_info->pPrinterName,
2862 prn_info->pPortName);
2864 retval = TRUE;
2865 end:
2866 HeapFree(GetProcessHeap(), 0, dm);
2867 HeapFree(GetProcessHeap(), 0, prn_info);
2868 HeapFree(GetProcessHeap(), 0, drv_info);
2869 return retval;
2872 /****************************************************************************************
2873 * pagesetup_init_combos
2875 * Fills Printers, Paper and Source combos
2878 static void pagesetup_init_combos(HWND hDlg, pagesetup_data *data)
2880 DEVMODEW *dm;
2881 LPWSTR devname, portname;
2883 dm = pagesetup_get_devmode(data);
2884 devname = pagesetup_get_devname(data);
2885 portname = pagesetup_get_portname(data);
2887 PRINTDLG_SetUpPrinterListComboW(hDlg, cmb1, devname);
2888 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2, devname, portname, dm);
2889 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3, devname, portname, dm);
2891 pagesetup_release_a_devname(data, portname);
2892 pagesetup_release_a_devname(data, devname);
2893 pagesetup_release_devmode(data, dm);
2897 /****************************************************************************************
2898 * pagesetup_change_printer_dialog
2900 * Pops up another dialog that lets the user pick another printer.
2902 * For now we display the PrintDlg, this should display a striped down version of it.
2904 static void pagesetup_change_printer_dialog(HWND hDlg, pagesetup_data *data)
2906 PRINTDLGW prnt;
2907 LPWSTR drvname, devname, portname;
2908 DEVMODEW *tmp_dm, *dm;
2910 memset(&prnt, 0, sizeof(prnt));
2911 prnt.lStructSize = sizeof(prnt);
2912 prnt.Flags = 0;
2913 prnt.hwndOwner = hDlg;
2915 drvname = pagesetup_get_drvname(data);
2916 devname = pagesetup_get_devname(data);
2917 portname = pagesetup_get_portname(data);
2918 prnt.hDevNames = 0;
2919 PRINTDLG_CreateDevNamesW(&prnt.hDevNames, drvname, devname, portname);
2920 pagesetup_release_a_devname(data, portname);
2921 pagesetup_release_a_devname(data, devname);
2922 pagesetup_release_a_devname(data, drvname);
2924 tmp_dm = pagesetup_get_devmode(data);
2925 prnt.hDevMode = GlobalAlloc(GMEM_MOVEABLE, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2926 dm = GlobalLock(prnt.hDevMode);
2927 memcpy(dm, tmp_dm, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2928 GlobalUnlock(prnt.hDevMode);
2929 pagesetup_release_devmode(data, tmp_dm);
2931 if (PrintDlgW(&prnt))
2933 DEVMODEW *dm = GlobalLock(prnt.hDevMode);
2934 DEVNAMES *dn = GlobalLock(prnt.hDevNames);
2936 pagesetup_set_devnames(data, (WCHAR*)dn + dn->wDriverOffset,
2937 (WCHAR*)dn + dn->wDeviceOffset, (WCHAR *)dn + dn->wOutputOffset);
2938 pagesetup_set_devmode(data, dm);
2939 GlobalUnlock(prnt.hDevNames);
2940 GlobalUnlock(prnt.hDevMode);
2941 pagesetup_init_combos(hDlg, data);
2944 GlobalFree(prnt.hDevMode);
2945 GlobalFree(prnt.hDevNames);
2949 /******************************************************************************************
2950 * pagesetup_change_preview
2952 * Changes paper preview size / position
2955 static void pagesetup_change_preview(const pagesetup_data *data)
2957 LONG width, height, x, y;
2958 RECT tmp;
2959 const int shadow = 4;
2961 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2963 width = data->rtDrawRect.right - data->rtDrawRect.left;
2964 height = pagesetup_get_papersize_pt(data)->y * width / pagesetup_get_papersize_pt(data)->x;
2966 else
2968 height = data->rtDrawRect.bottom - data->rtDrawRect.top;
2969 width = pagesetup_get_papersize_pt(data)->x * height / pagesetup_get_papersize_pt(data)->y;
2971 x = (data->rtDrawRect.right + data->rtDrawRect.left - width) / 2;
2972 y = (data->rtDrawRect.bottom + data->rtDrawRect.top - height) / 2;
2973 TRACE("draw rect %s x=%d, y=%d, w=%d, h=%d\n",
2974 wine_dbgstr_rect(&data->rtDrawRect), x, y, width, height);
2976 MoveWindow(GetDlgItem(data->hDlg, rct2), x + width, y + shadow, shadow, height, FALSE);
2977 MoveWindow(GetDlgItem(data->hDlg, rct3), x + shadow, y + height, width, shadow, FALSE);
2978 MoveWindow(GetDlgItem(data->hDlg, rct1), x, y, width, height, FALSE);
2980 tmp = data->rtDrawRect;
2981 tmp.right += shadow;
2982 tmp.bottom += shadow;
2983 InvalidateRect(data->hDlg, &tmp, TRUE);
2986 static inline LONG *element_from_margin_id(RECT *rc, WORD id)
2988 switch(id)
2990 case edt4: return &rc->left;
2991 case edt5: return &rc->top;
2992 case edt6: return &rc->right;
2993 case edt7: return &rc->bottom;
2995 return NULL;
2998 static void update_margin_edits(HWND hDlg, const pagesetup_data *data, WORD id)
3000 WCHAR str[100];
3001 WORD idx;
3003 for(idx = edt4; idx <= edt7; idx++)
3005 if(id == 0 || id == idx)
3007 size2str(data, *element_from_margin_id(pagesetup_get_margin_rect(data), idx), str);
3008 SetDlgItemTextW(hDlg, idx, str);
3013 static void margin_edit_notification(HWND hDlg, pagesetup_data *data, WORD msg, WORD id)
3015 switch (msg)
3017 case EN_CHANGE:
3019 WCHAR buf[10];
3020 LONG val = 0;
3021 LONG *value = element_from_margin_id(pagesetup_get_margin_rect(data), id);
3023 if (GetDlgItemTextW(hDlg, id, buf, sizeof(buf) / sizeof(buf[0])) != 0)
3025 WCHAR *end;
3026 WCHAR decimal = get_decimal_sep();
3028 val = strtolW(buf, &end, 10);
3029 if(end != buf || *end == decimal)
3031 int mult = is_metric(data) ? 100 : 1000;
3032 val *= mult;
3033 if(*end == decimal)
3035 while(mult > 1)
3037 end++;
3038 mult /= 10;
3039 if(isdigitW(*end))
3040 val += (*end - '0') * mult;
3041 else
3042 break;
3047 *value = val;
3048 return;
3051 case EN_KILLFOCUS:
3052 update_margin_edits(hDlg, data, id);
3053 return;
3057 static void set_margin_groupbox_title(HWND hDlg, const pagesetup_data *data)
3059 WCHAR title[256];
3061 if(LoadStringW(COMDLG32_hInstance, is_metric(data) ? PD32_MARGINS_IN_MILLIMETERS : PD32_MARGINS_IN_INCHES,
3062 title, sizeof(title)/sizeof(title[0])))
3063 SetDlgItemTextW(hDlg, grp4, title);
3066 static void pagesetup_update_orientation_buttons(HWND hDlg, const pagesetup_data *data)
3068 if (pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3069 CheckRadioButton(hDlg, rad1, rad2, rad2);
3070 else
3071 CheckRadioButton(hDlg, rad1, rad2, rad1);
3074 /****************************************************************************************
3075 * pagesetup_printer_properties
3077 * Handle invocation of the 'Properties' button (not present in the default template).
3079 static void pagesetup_printer_properties(HWND hDlg, pagesetup_data *data)
3081 HANDLE hprn;
3082 LPWSTR devname;
3083 DEVMODEW *dm;
3084 LRESULT count;
3085 int i;
3087 devname = pagesetup_get_devname(data);
3089 if (!OpenPrinterW(devname, &hprn, NULL))
3091 FIXME("Call to OpenPrinter did not succeed!\n");
3092 pagesetup_release_a_devname(data, devname);
3093 return;
3096 dm = pagesetup_get_devmode(data);
3097 DocumentPropertiesW(hDlg, hprn, devname, dm, dm, DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
3098 pagesetup_set_devmode(data, dm);
3099 pagesetup_release_devmode(data, dm);
3100 pagesetup_release_a_devname(data, devname);
3101 ClosePrinter(hprn);
3103 /* Changing paper */
3104 pagesetup_update_papersize(data);
3105 pagesetup_update_orientation_buttons(hDlg, data);
3107 /* Changing paper preview */
3108 pagesetup_change_preview(data);
3110 /* Selecting paper in combo */
3111 count = SendDlgItemMessageW(hDlg, cmb2, CB_GETCOUNT, 0, 0);
3112 if(count != CB_ERR)
3114 WORD paperword = pagesetup_get_papersize(data);
3115 for(i = 0; i < count; i++)
3117 if(SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, i, 0) == paperword) {
3118 SendDlgItemMessageW(hDlg, cmb2, CB_SETCURSEL, i, 0);
3119 break;
3125 /********************************************************************************
3126 * pagesetup_wm_command
3127 * process WM_COMMAND message for PageSetupDlg
3129 * PARAMS
3130 * hDlg [in] Main dialog HANDLE
3131 * wParam [in] WM_COMMAND wParam
3132 * lParam [in] WM_COMMAND lParam
3133 * pda [in/out] ptr to PageSetupDataA
3136 static BOOL pagesetup_wm_command(HWND hDlg, WPARAM wParam, LPARAM lParam, pagesetup_data *data)
3138 WORD msg = HIWORD(wParam);
3139 WORD id = LOWORD(wParam);
3141 TRACE("loword (lparam) %d, wparam 0x%lx, lparam %08lx\n",
3142 LOWORD(lParam),wParam,lParam);
3143 switch (id) {
3144 case IDOK:
3145 EndDialog(hDlg, TRUE);
3146 return TRUE ;
3148 case IDCANCEL:
3149 EndDialog(hDlg, FALSE);
3150 return FALSE ;
3152 case psh3: /* Printer... */
3153 pagesetup_change_printer_dialog(hDlg, data);
3154 return TRUE;
3156 case rad1: /* Portrait */
3157 case rad2: /* Landscape */
3158 if((id == rad1 && pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE) ||
3159 (id == rad2 && pagesetup_get_orientation(data) == DMORIENT_PORTRAIT))
3161 pagesetup_set_orientation(data, (id == rad1) ? DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE);
3162 pagesetup_update_papersize(data);
3163 rotate_rect(pagesetup_get_margin_rect(data), (id == rad2));
3164 update_margin_edits(hDlg, data, 0);
3165 pagesetup_change_preview(data);
3167 break;
3168 case cmb1: /* Printer combo */
3169 if(msg == CBN_SELCHANGE)
3171 WCHAR name[256];
3172 GetDlgItemTextW(hDlg, id, name, sizeof(name) / sizeof(name[0]));
3173 pagesetup_change_printer(name, data);
3174 pagesetup_init_combos(hDlg, data);
3176 break;
3177 case cmb2: /* Paper combo */
3178 if(msg == CBN_SELCHANGE)
3180 DWORD paperword = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA,
3181 SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0), 0);
3182 if (paperword != CB_ERR)
3184 pagesetup_set_papersize(data, paperword);
3185 pagesetup_update_papersize(data);
3186 pagesetup_change_preview(data);
3187 } else
3188 FIXME("could not get dialog text for papersize cmbbox?\n");
3190 break;
3191 case cmb3: /* Paper Source */
3192 if(msg == CBN_SELCHANGE)
3194 WORD source = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA,
3195 SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0), 0);
3196 pagesetup_set_defaultsource(data, source);
3198 break;
3199 case psh2: /* Printer Properties button */
3200 pagesetup_printer_properties(hDlg, data);
3201 break;
3202 case edt4:
3203 case edt5:
3204 case edt6:
3205 case edt7:
3206 margin_edit_notification(hDlg, data, msg, id);
3207 break;
3209 InvalidateRect(GetDlgItem(hDlg, rct1), NULL, TRUE);
3210 return FALSE;
3213 /***********************************************************************
3214 * default_page_paint_hook
3215 * Default hook paint procedure that receives WM_PSD_* messages from the dialog box
3216 * whenever the sample page is redrawn.
3218 static UINT_PTR default_page_paint_hook(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam,
3219 const pagesetup_data *data)
3221 LPRECT lprc = (LPRECT) lParam;
3222 HDC hdc = (HDC) wParam;
3223 HPEN hpen, holdpen;
3224 LOGFONTW lf;
3225 HFONT hfont, holdfont;
3226 INT oldbkmode;
3227 TRACE("uMsg: WM_USER+%d\n",uMsg-WM_USER);
3228 /* Call user paint hook if enable */
3229 if (pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK)
3230 if (pagesetup_get_hook(data, page_paint_hook)(hwndDlg, uMsg, wParam, lParam))
3231 return TRUE;
3233 switch (uMsg) {
3234 /* LPPAGESETUPDLG in lParam */
3235 case WM_PSD_PAGESETUPDLG:
3236 /* Inform about the sample page rectangle */
3237 case WM_PSD_FULLPAGERECT:
3238 /* Inform about the margin rectangle */
3239 case WM_PSD_MINMARGINRECT:
3240 return FALSE;
3242 /* Draw dashed rectangle showing margins */
3243 case WM_PSD_MARGINRECT:
3244 hpen = CreatePen(PS_DASH, 1, GetSysColor(COLOR_3DSHADOW));
3245 holdpen = SelectObject(hdc, hpen);
3246 Rectangle(hdc, lprc->left, lprc->top, lprc->right, lprc->bottom);
3247 DeleteObject(SelectObject(hdc, holdpen));
3248 return TRUE;
3249 /* Draw the fake document */
3250 case WM_PSD_GREEKTEXTRECT:
3251 /* select a nice scalable font, because we want the text really small */
3252 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
3253 lf.lfHeight = 6; /* value chosen based on visual effect */
3254 hfont = CreateFontIndirectW(&lf);
3255 holdfont = SelectObject(hdc, hfont);
3257 /* if text not loaded, then do so now */
3258 if (wszFakeDocumentText[0] == '\0')
3259 LoadStringW(COMDLG32_hInstance,
3260 IDS_FAKEDOCTEXT,
3261 wszFakeDocumentText,
3262 sizeof(wszFakeDocumentText)/sizeof(wszFakeDocumentText[0]));
3264 oldbkmode = SetBkMode(hdc, TRANSPARENT);
3265 DrawTextW(hdc, wszFakeDocumentText, -1, lprc, DT_TOP|DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
3266 SetBkMode(hdc, oldbkmode);
3268 DeleteObject(SelectObject(hdc, holdfont));
3269 return TRUE;
3271 /* Envelope stamp */
3272 case WM_PSD_ENVSTAMPRECT:
3273 /* Return address */
3274 case WM_PSD_YAFULLPAGERECT:
3275 FIXME("envelope/stamp is not implemented\n");
3276 return FALSE;
3277 default:
3278 FIXME("Unknown message %x\n",uMsg);
3279 return FALSE;
3281 return TRUE;
3284 /***********************************************************************
3285 * PagePaintProc
3286 * The main paint procedure for the PageSetupDlg function.
3287 * The Page Setup dialog box includes an image of a sample page that shows how
3288 * the user's selections affect the appearance of the printed output.
3289 * The image consists of a rectangle that represents the selected paper
3290 * or envelope type, with a dotted-line rectangle representing
3291 * the current margins, and partial (Greek text) characters
3292 * to show how text looks on the printed page.
3294 * The following messages in the order sends to user hook procedure:
3295 * WM_PSD_PAGESETUPDLG Draw the contents of the sample page
3296 * WM_PSD_FULLPAGERECT Inform about the bounding rectangle
3297 * WM_PSD_MINMARGINRECT Inform about the margin rectangle (min margin?)
3298 * WM_PSD_MARGINRECT Draw the margin rectangle
3299 * WM_PSD_GREEKTEXTRECT Draw the Greek text inside the margin rectangle
3300 * If any of first three messages returns TRUE, painting done.
3302 * PARAMS:
3303 * hWnd [in] Handle to the Page Setup dialog box
3304 * uMsg [in] Received message
3306 * TODO:
3307 * WM_PSD_ENVSTAMPRECT Draw in the envelope-stamp rectangle (for envelopes only)
3308 * WM_PSD_YAFULLPAGERECT Draw the return address portion (for envelopes and other paper sizes)
3310 * RETURNS:
3311 * FALSE if all done correctly
3316 static LRESULT CALLBACK
3317 PRINTDLG_PagePaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3319 PAINTSTRUCT ps;
3320 RECT rcClient, rcMargin;
3321 HPEN hpen, holdpen;
3322 HDC hdc;
3323 HBRUSH hbrush, holdbrush;
3324 pagesetup_data *data;
3325 int papersize=0, orientation=0; /* FIXME: set this values for user paint hook */
3326 double scalx, scaly;
3328 if (uMsg != WM_PAINT)
3329 return CallWindowProcA(lpfnStaticWndProc, hWnd, uMsg, wParam, lParam);
3331 /* Processing WM_PAINT message */
3332 data = GetPropW(hWnd, pagesetupdlg_prop);
3333 if (!data) {
3334 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3335 return FALSE;
3337 if (default_page_paint_hook(hWnd, WM_PSD_PAGESETUPDLG, MAKELONG(papersize, orientation),
3338 pagesetup_get_dlg_struct(data), data))
3339 return FALSE;
3341 hdc = BeginPaint(hWnd, &ps);
3342 GetClientRect(hWnd, &rcClient);
3344 scalx = rcClient.right / (double)pagesetup_get_papersize_pt(data)->x;
3345 scaly = rcClient.bottom / (double)pagesetup_get_papersize_pt(data)->y;
3346 rcMargin = rcClient;
3348 rcMargin.left += pagesetup_get_margin_rect(data)->left * scalx;
3349 rcMargin.top += pagesetup_get_margin_rect(data)->top * scaly;
3350 rcMargin.right -= pagesetup_get_margin_rect(data)->right * scalx;
3351 rcMargin.bottom -= pagesetup_get_margin_rect(data)->bottom * scaly;
3353 /* if the space is too small then we make sure to not draw anything */
3354 rcMargin.left = min(rcMargin.left, rcMargin.right);
3355 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3357 if (!default_page_paint_hook(hWnd, WM_PSD_FULLPAGERECT, (WPARAM)hdc, (LPARAM)&rcClient, data) &&
3358 !default_page_paint_hook(hWnd, WM_PSD_MINMARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data) )
3360 /* fill background */
3361 hbrush = GetSysColorBrush(COLOR_3DHIGHLIGHT);
3362 FillRect(hdc, &rcClient, hbrush);
3363 holdbrush = SelectObject(hdc, hbrush);
3365 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
3366 holdpen = SelectObject(hdc, hpen);
3368 /* paint left edge */
3369 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3370 LineTo(hdc, rcClient.left, rcClient.bottom-1);
3372 /* paint top edge */
3373 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3374 LineTo(hdc, rcClient.right, rcClient.top);
3376 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
3377 DeleteObject(SelectObject(hdc, hpen));
3379 /* paint right edge */
3380 MoveToEx(hdc, rcClient.right-1, rcClient.top, NULL);
3381 LineTo(hdc, rcClient.right-1, rcClient.bottom);
3383 /* paint bottom edge */
3384 MoveToEx(hdc, rcClient.left, rcClient.bottom-1, NULL);
3385 LineTo(hdc, rcClient.right, rcClient.bottom-1);
3387 DeleteObject(SelectObject(hdc, holdpen));
3388 DeleteObject(SelectObject(hdc, holdbrush));
3390 default_page_paint_hook(hWnd, WM_PSD_MARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3392 /* give text a bit of a space from the frame */
3393 rcMargin.left += 2;
3394 rcMargin.top += 2;
3395 rcMargin.right -= 2;
3396 rcMargin.bottom -= 2;
3398 /* if the space is too small then we make sure to not draw anything */
3399 rcMargin.left = min(rcMargin.left, rcMargin.right);
3400 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3402 default_page_paint_hook(hWnd, WM_PSD_GREEKTEXTRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data);
3405 EndPaint(hWnd, &ps);
3406 return FALSE;
3409 /*******************************************************
3410 * The margin edit controls are subclassed to filter
3411 * anything other than numbers and the decimal separator.
3413 static LRESULT CALLBACK pagesetup_margin_editproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3415 if (msg == WM_CHAR)
3417 WCHAR decimal = get_decimal_sep();
3418 WCHAR wc = (WCHAR)wparam;
3419 if(!isdigitW(wc) && wc != decimal && wc != VK_BACK) return 0;
3421 return CallWindowProcW(edit_wndproc, hwnd, msg, wparam, lparam);
3424 static void subclass_margin_edits(HWND hDlg)
3426 int id;
3427 WNDPROC old_proc;
3429 for(id = edt4; id <= edt7; id++)
3431 old_proc = (WNDPROC)SetWindowLongPtrW(GetDlgItem(hDlg, id),
3432 GWLP_WNDPROC,
3433 (ULONG_PTR)pagesetup_margin_editproc);
3434 InterlockedCompareExchangePointer((void**)&edit_wndproc, old_proc, NULL);
3438 /***********************************************************************
3439 * pagesetup_dlg_proc
3441 * Message handler for PageSetupDlg
3443 static INT_PTR CALLBACK pagesetup_dlg_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
3445 pagesetup_data *data;
3446 INT_PTR res = FALSE;
3447 HWND hDrawWnd;
3449 if (uMsg == WM_INITDIALOG) { /*Init dialog*/
3450 data = (pagesetup_data *)lParam;
3451 data->hDlg = hDlg;
3453 hDrawWnd = GetDlgItem(hDlg, rct1);
3454 TRACE("set property to %p\n", data);
3455 SetPropW(hDlg, pagesetupdlg_prop, data);
3456 SetPropW(hDrawWnd, pagesetupdlg_prop, data);
3457 GetWindowRect(hDrawWnd, &data->rtDrawRect); /* Calculating rect in client coordinates where paper draws */
3458 ScreenToClient(hDlg, (LPPOINT)&data->rtDrawRect);
3459 ScreenToClient(hDlg, (LPPOINT)(&data->rtDrawRect.right));
3460 lpfnStaticWndProc = (WNDPROC)SetWindowLongPtrW(
3461 hDrawWnd,
3462 GWLP_WNDPROC,
3463 (ULONG_PTR)PRINTDLG_PagePaintProc);
3465 /* FIXME: Paint hook. Must it be at begin of initialization or at end? */
3466 res = TRUE;
3467 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3469 if (!pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam,
3470 pagesetup_get_dlg_struct(data)))
3471 FIXME("Setup page hook failed?\n");
3474 /* if printer button disabled */
3475 if (pagesetup_get_flags(data) & PSD_DISABLEPRINTER)
3476 EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
3477 /* if margin edit boxes disabled */
3478 if (pagesetup_get_flags(data) & PSD_DISABLEMARGINS)
3480 EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
3481 EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
3482 EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
3483 EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
3486 /* Set orientation radiobuttons properly */
3487 pagesetup_update_orientation_buttons(hDlg, data);
3489 /* if orientation disabled */
3490 if (pagesetup_get_flags(data) & PSD_DISABLEORIENTATION)
3492 EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
3493 EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
3496 /* We fill them out enabled or not */
3497 if (!(pagesetup_get_flags(data) & PSD_MARGINS))
3499 /* default is 1 inch */
3500 LONG size = thousandths_inch_to_size(data, 1000);
3501 SetRect(pagesetup_get_margin_rect(data), size, size, size, size);
3503 update_margin_edits(hDlg, data, 0);
3504 subclass_margin_edits(hDlg);
3505 set_margin_groupbox_title(hDlg, data);
3507 /* if paper disabled */
3508 if (pagesetup_get_flags(data) & PSD_DISABLEPAPER)
3510 EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
3511 EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
3514 /* filling combos: printer, paper, source. selecting current printer (from DEVMODEA) */
3515 pagesetup_init_combos(hDlg, data);
3516 pagesetup_update_papersize(data);
3517 pagesetup_set_defaultsource(data, DMBIN_FORMSOURCE); /* FIXME: This is the auto select bin. Is this correct? */
3519 /* Drawing paper prev */
3520 pagesetup_change_preview(data);
3521 return TRUE;
3522 } else {
3523 data = GetPropW(hDlg, pagesetupdlg_prop);
3524 if (!data)
3526 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3527 return FALSE;
3529 if (pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPHOOK)
3531 res = pagesetup_get_hook(data, page_setup_hook)(hDlg, uMsg, wParam, lParam);
3532 if (res) return res;
3535 switch (uMsg) {
3536 case WM_COMMAND:
3537 return pagesetup_wm_command(hDlg, wParam, lParam, data);
3539 return FALSE;
3542 static WCHAR *get_default_printer(void)
3544 WCHAR *name = NULL;
3545 DWORD len = 0;
3547 GetDefaultPrinterW(NULL, &len);
3548 if(len)
3550 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3551 GetDefaultPrinterW(name, &len);
3553 return name;
3556 static void pagesetup_dump_dlg_struct(pagesetup_data *data)
3558 if(TRACE_ON(commdlg))
3560 char flagstr[1000] = "";
3561 const struct pd_flags *pflag = psd_flags;
3562 for( ; pflag->name; pflag++)
3564 if(pagesetup_get_flags(data) & pflag->flag)
3566 strcat(flagstr, pflag->name);
3567 strcat(flagstr, "|");
3570 TRACE("%s: (%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
3571 "hinst %p, flags %08x (%s)\n",
3572 data->unicode ? "unicode" : "ansi",
3573 data->u.dlgw, data->u.dlgw->hwndOwner, data->u.dlgw->hDevMode,
3574 data->u.dlgw->hDevNames, data->u.dlgw->hInstance,
3575 pagesetup_get_flags(data), flagstr);
3579 static void *pagesetup_get_template(pagesetup_data *data)
3581 HRSRC res;
3582 HGLOBAL tmpl_handle;
3584 if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATEHANDLE)
3586 tmpl_handle = data->u.dlgw->hPageSetupTemplate;
3588 else if(pagesetup_get_flags(data) & PSD_ENABLEPAGESETUPTEMPLATE)
3590 if(data->unicode)
3591 res = FindResourceW(data->u.dlgw->hInstance,
3592 data->u.dlgw->lpPageSetupTemplateName, MAKEINTRESOURCEW(RT_DIALOG));
3593 else
3594 res = FindResourceA(data->u.dlga->hInstance,
3595 data->u.dlga->lpPageSetupTemplateName, MAKEINTRESOURCEA(RT_DIALOG));
3596 tmpl_handle = LoadResource(data->u.dlgw->hInstance, res);
3598 else
3600 res = FindResourceW(COMDLG32_hInstance, MAKEINTRESOURCEW(PAGESETUPDLGORD),
3601 MAKEINTRESOURCEW(RT_DIALOG));
3602 tmpl_handle = LoadResource(COMDLG32_hInstance, res);
3604 return LockResource(tmpl_handle);
3607 static BOOL pagesetup_common(pagesetup_data *data)
3609 BOOL ret;
3610 void *tmpl;
3612 if(!pagesetup_get_dlg_struct(data))
3614 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
3615 return FALSE;
3618 pagesetup_dump_dlg_struct(data);
3620 if(data->u.dlgw->lStructSize != sizeof(PAGESETUPDLGW))
3622 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
3623 return FALSE;
3626 if ((pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK) &&
3627 (pagesetup_get_hook(data, page_paint_hook) == NULL))
3629 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
3630 return FALSE;
3633 if(!(pagesetup_get_flags(data) & (PSD_INTHOUSANDTHSOFINCHES | PSD_INHUNDREDTHSOFMILLIMETERS)))
3634 data->u.dlgw->Flags |= is_default_metric() ?
3635 PSD_INHUNDREDTHSOFMILLIMETERS : PSD_INTHOUSANDTHSOFINCHES;
3637 if (!data->u.dlgw->hDevMode || !data->u.dlgw->hDevNames)
3639 WCHAR *def = get_default_printer();
3640 if(!def)
3642 if (!(pagesetup_get_flags(data) & PSD_NOWARNING))
3644 WCHAR errstr[256];
3645 LoadStringW(COMDLG32_hInstance, PD32_NO_DEFAULT_PRINTER, errstr, 255);
3646 MessageBoxW(data->u.dlgw->hwndOwner, errstr, 0, MB_OK | MB_ICONERROR);
3648 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3649 return FALSE;
3651 pagesetup_change_printer(def, data);
3652 HeapFree(GetProcessHeap(), 0, def);
3655 if (pagesetup_get_flags(data) & PSD_RETURNDEFAULT)
3657 pagesetup_update_papersize(data);
3658 return TRUE;
3661 tmpl = pagesetup_get_template(data);
3663 ret = DialogBoxIndirectParamW(data->u.dlgw->hInstance, tmpl,
3664 data->u.dlgw->hwndOwner,
3665 pagesetup_dlg_proc, (LPARAM)data) > 0;
3666 return ret;
3669 /***********************************************************************
3670 * PageSetupDlgA (COMDLG32.@)
3672 * Displays the PAGE SETUP dialog box, which enables the user to specify
3673 * specific properties of a printed page such as
3674 * size, source, orientation and the width of the page margins.
3676 * PARAMS
3677 * setupdlg [IO] PAGESETUPDLGA struct
3679 * RETURNS
3680 * TRUE if the user pressed the OK button
3681 * FALSE if the user cancelled the window or an error occurred
3683 * NOTES
3684 * The values of hDevMode and hDevNames are filled on output and can be
3685 * changed in PAGESETUPDLG when they are passed in PageSetupDlg.
3688 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg)
3690 pagesetup_data data;
3692 data.unicode = FALSE;
3693 data.u.dlga = setupdlg;
3695 return pagesetup_common(&data);
3698 /***********************************************************************
3699 * PageSetupDlgW (COMDLG32.@)
3701 * See PageSetupDlgA.
3703 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg)
3705 pagesetup_data data;
3707 data.unicode = TRUE;
3708 data.u.dlgw = setupdlg;
3710 return pagesetup_common(&data);
3713 /***********************************************************************
3714 * PrintDlgExA (COMDLG32.@)
3716 * See PrintDlgExW.
3718 * BUGS
3719 * Only a Stub
3722 HRESULT WINAPI PrintDlgExA(LPPRINTDLGEXA lppd)
3725 FIXME("(%p) stub\n", lppd);
3726 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXA))) {
3727 return E_INVALIDARG;
3730 if (!IsWindow(lppd->hwndOwner)) {
3731 return E_HANDLE;
3734 return E_NOTIMPL;
3737 /***********************************************************************
3738 * PrintDlgExW (COMDLG32.@)
3740 * Display the property sheet style PRINT dialog box
3742 * PARAMS
3743 * lppd [IO] ptr to PRINTDLGEX struct
3745 * RETURNS
3746 * Success: S_OK
3747 * Failure: One of the following COM error codes:
3748 * E_OUTOFMEMORY Insufficient memory.
3749 * E_INVALIDARG One or more arguments are invalid.
3750 * E_POINTER Invalid pointer.
3751 * E_HANDLE Invalid handle.
3752 * E_FAIL Unspecified error.
3754 * NOTES
3755 * This Dialog enables the user to specify specific properties of the print job.
3756 * The property sheet can also have additional application-specific and
3757 * driver-specific property pages.
3759 * BUGS
3760 * Not fully implemented
3763 HRESULT WINAPI PrintDlgExW(LPPRINTDLGEXW lppd)
3765 DWORD ret = E_FAIL;
3766 LPVOID ptr;
3768 FIXME("(%p) not fully implemented\n", lppd);
3770 if ((lppd == NULL) || (lppd->lStructSize != sizeof(PRINTDLGEXW))) {
3771 return E_INVALIDARG;
3774 if (!IsWindow(lppd->hwndOwner)) {
3775 return E_HANDLE;
3778 if (lppd->Flags & PD_RETURNDEFAULT) {
3779 PRINTER_INFO_2W *pbuf;
3780 DRIVER_INFO_2W *dbuf;
3781 HANDLE hprn;
3782 DWORD needed = 1024;
3783 BOOL bRet;
3785 if (lppd->hDevMode || lppd->hDevNames) {
3786 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
3787 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3788 return E_INVALIDARG;
3790 if (!PRINTDLG_OpenDefaultPrinter(&hprn)) {
3791 WARN("Can't find default printer\n");
3792 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
3793 return E_FAIL;
3796 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3797 bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3798 if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3799 HeapFree(GetProcessHeap(), 0, pbuf);
3800 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3801 bRet = GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
3803 if (!bRet) {
3804 HeapFree(GetProcessHeap(), 0, pbuf);
3805 ClosePrinter(hprn);
3806 return E_FAIL;
3809 needed = 1024;
3810 dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3811 bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3812 if (!bRet && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
3813 HeapFree(GetProcessHeap(), 0, dbuf);
3814 dbuf = HeapAlloc(GetProcessHeap(), 0, needed);
3815 bRet = GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed);
3817 if (!bRet) {
3818 ERR("GetPrinterDriverW failed, last error %d, fix your config for printer %s!\n",
3819 GetLastError(), debugstr_w(pbuf->pPrinterName));
3820 HeapFree(GetProcessHeap(), 0, dbuf);
3821 HeapFree(GetProcessHeap(), 0, pbuf);
3822 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
3823 ClosePrinter(hprn);
3824 return E_FAIL;
3826 ClosePrinter(hprn);
3828 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
3829 dbuf->pDriverPath,
3830 pbuf->pPrinterName,
3831 pbuf->pPortName);
3832 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
3833 pbuf->pDevMode->dmDriverExtra);
3834 if (lppd->hDevMode) {
3835 ptr = GlobalLock(lppd->hDevMode);
3836 if (ptr) {
3837 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
3838 pbuf->pDevMode->dmDriverExtra);
3839 GlobalUnlock(lppd->hDevMode);
3840 ret = S_OK;
3843 HeapFree(GetProcessHeap(), 0, pbuf);
3844 HeapFree(GetProcessHeap(), 0, dbuf);
3846 return ret;
3849 return E_NOTIMPL;